<template>
  <basic-container>
    <avue-crud id="activity"  :option="option" :table-loading="loading" :data="data" :page.sync="page" :permission="permissionList"
      :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel"
      @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange"
      @row-click="rowClick"
      @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger" size="small" icon="el-icon-delete" plain v-if="permission.activity_delete"
          @click="handleDelete">删 除
        </el-button>
      </template>

      <template slot="menuLeft">
        <el-button type="primary" size="small" icon="el-icon-delete"  v-if="permission.activity_add"
          @click="showAdd">新增
        </el-button>
      </template>

      <template slot="enable" slot-scope="scope">
        <el-switch disabled v-model="scope.row.enable"   :active-value="1" :inactive-value="0"></el-switch>
      </template>

      <template slot-scope="scope" slot="activityTime">
        <div style="margin: 20px 0;">
          报名截止：{{scope.row.applyEndTime}}
        </div>
        <div style="margin: 20px 0;">
          活动开始：{{scope.row.beginTime}}
        </div>
        <div style="margin: 20px 0;">
          活动结束：{{scope.row.endTime}}
        </div>
      </template>

      <template slot-scope="scope" slot="cover">
        <el-image
            style="width: 100%; height: 100%"
            :src="scope.row.cover"
            :preview-src-list="[scope.row.cover]">
          </el-image>
      </template>

      <template slot-scope="scope" slot="poster">
        <el-image
            style="width: 100px; height: 120px;overflow-y: hidden;"
            :src="scope.row.poster"
            fit="cover"
            :preview-src-list="[scope.row.poster]">
          </el-image>
      </template>

      <template slot-scope="scope" slot="showData">
            参与人数：<div style="background-color: #3296fa;color: white; border-radius: 5px; margin:10px; padding:0 10px 0 10px ; display:inline;">{{scope.row.applyNum}}</div><br>
            礼物总值：<div style="background-color: #5bc0de;color: white; border-radius: 5px; margin:10px; padding:0 10px 0 10px ; display:inline;">{{ +scope.row.totalCash}}</div><br>
            打榜数量：<div style="background-color: #5bc0de;color: white; border-radius: 5px; margin:10px; padding:0 10px 0 10px ; display:inline;">{{scope.row.hitNum}}</div><br>
            打赏数量：<div style="background-color: #99cf99;color: white; border-radius: 5px; margin:10px; padding:0 10px 0 10px ; display:inline;">{{scope.row.rewardNum}}</div><br>
      </template>


      <!-- 操作菜单 -->
      <template slot-scope="scope" slot="menu">
        <el-button type="text" size="small" icon="el-icon-edit" plain class="none-border"
          @click.stop="handleEdit(scope.row)">编辑
        </el-button>
        <el-button type="text" size="small" icon="el-icon-view" plain class="none-border"
          @click.stop="view(scope.row.id,'查看选手')">查看选手
        </el-button>
        <el-button type="text" size="small" icon="el-icon-view" plain class="none-border"
          @click.stop="view(scope.row.id,'查看作品')">查看作品
        </el-button>
        <el-button type="text" size="small" icon="el-icon-view" plain class="none-border"
          @click.stop="view(scope.row.id,'打榜记录')">打榜记录
        </el-button>
        <el-button type="text" size="small" icon="el-icon-view" plain class="none-border"
          @click.stop="view(scope.row.id,'打赏记录')">打赏记录
        </el-button>
      </template>
    </avue-crud>

    <activity-add ref="addDialog"
      @submit="submit"
      :form1="data1" :form2="data2" :form3="data3" :form4="data4">
    </activity-add>
  </basic-container>
</template>

<script>
  import activityAdd from "./comps/activity-add.vue"
  import {getList, getDetail, createOrUpdate, update, remove} from "@/api/activity/activity";
  import {mapGetters} from "vuex";

  export default {
    components:{
      activityAdd
    },
    data() {
      return {
        data1:{
          id:'',
          name:'',
          beginTime:'',
          endTime:'',
          applyEndTime:'',
          description:'',
          visitBase:0,
          tagIds:[]
        },
        data2:{
          cover:'',
          poster:'',
          ads:''
        },
        data3:{
           bonusProportion:'',
           grossProfitRate:'',
           classification:'',
           personTitle:'',
           productionTitle:'',
           introductionTitle:'',
           ticketDays:1,
           ticketHotValue:1
        },
        data4:{
          presentIds:[]
        },

        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          editBtn:false,
          addBtn:false,
          selection: true,
          labelWidth: 150,
          size:"medium",
          dialogClickModal: false,
          column: [
            {
              label: "封面图",
              prop: "cover",
              width: 150,
              type: "upload",
              solt:true,
              listType: "picture-img",
              action: '/api/putObject',
              propsHttp: {
                url: 'data'
              },
              rules: [{
                required: true,
                message: "请输入活动封面图",
                trigger: "blur"
              }]
            },{
              label: "活动主题",
              prop: "name",
              rules: [{
                required: true,
                message: "请输入活动主题",
                trigger: "blur"
              }]
            },
            {
              label:"活动时间",
              slot:true,
              width: 200,
              editDisplay:false,
              addDisplay:false,
              prop:"activityTime"
            },
            {
              label:"数据",
              slot:true,
              width: 180,
              editDisplay:false,
              addDisplay:false,
              prop:"showData"
            },
            {
              label: "活动开始时间",
              width: 140,
              hide:true,
              prop: "beginTime",
              type: 'datetime',
              format: "yyyy-MM-dd hh:mm:ss",
              valueFormat: "yyyy-MM-dd hh:mm:ss",
              rules: [{
                required: true,
                message: "请输入活动开始时间",
                trigger: "blur"
              }]
            },
            {
              label: "活动结束时间",
              width: 140,
              hide:true,
              prop: "endTime",
              type: 'datetime',
              format: "yyyy-MM-dd hh:mm:ss",
              valueFormat: "yyyy-MM-dd hh:mm:ss",
              rules: [{
                required: true,
                message: "请输入活动结束时间",
                trigger: "blur"
              }]
            },
            {
              label: "报名结束时间",
              width: 140,
              hide:true,
              prop: "applyEndTime",
              type: 'datetime',
              format: "yyyy-MM-dd hh:mm:ss",
              valueFormat: "yyyy-MM-dd hh:mm:ss",
              rules: [{
                required: true,
                message: "请输入报名结束时间",
                trigger: "blur"
              }]
            },
            {
              hide: true,
              label: "活动简介",
              prop: "description",
              type: 'textarea',
              rules: [{
                required: true,
                message: "请输入活动简介",
                trigger: "blur"
              }]
            },
            {
              label: "活动标签",
              prop: "tagIds",
              type: "select",
              multiple: true,
              dicUrl: "/api/cyzh-tag/tag/list?size=500",
              props: {
                label: "name",
                value: "id"
              },
              dicFormatter: (res) => {
                return res.data.records
              }
            },
            {
              label: "活动海报",
              prop: "poster",
              slot:true,
              dataType: 'string',
              listType: 'picture-img',
              propsHttp: {
                url: 'data'
              },
              action: '/api/putObject',
              type: 'upload',
              rules: [{
                required: true,
                message: "请选择活动海报",
                trigger: "blur"
              }]
            },

            {
              label: "活动轮播图",
              prop: "ads",
              hide:true,
              dataType: 'string',
              listType: 'picture-card',
              propsHttp: {
                url: 'data'
              },
              action: '/api/putObject',
              type: 'upload',
              rules: [{
                required: true,
                message: "请选择活动轮播图",
                trigger: "blur"
              }]
            },
            {
              label: "是否启用",
              width: 100,
              prop: "enable",
              slot:true,
              type: 'switch',
              dicData: [{
                  label: '禁用',
                  value: 0
                },
                {
                  label: '启用',
                  value: 1
                }
              ]
            },

          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.activity_add, false),
          viewBtn: this.vaildData(this.permission.activity_view, false),
          delBtn: this.vaildData(this.permission.activity_delete, false),
          editBtn: this.vaildData(this.permission.activity_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowClick(row,event){
        if (event.property=='enable') {
          let param={
            id:row.id,
            enable:row.enable==1?0:1
          }
          update(param).then(()=>{
            this.onLoad(this.page);
          })
        }
      },
      showAdd(){
        if (!this.$isEmpty(this.data1.id)) {
          this.resetData()
        }
        this.$refs.addDialog.open()
      },
      resetData(){
        this.data1={
          id:'',
          name:'',
          beginTime:'',
          endTime:'',
          applyEndTime:'',
          description:'',
          visitBase:0,
          tagIds:[]
        }
        this.data2={
          cover:'',
          poster:'',
          ads:''
        }
        this.data3={
           bonusProportion:0,
           grossProfitRate:1,
           classification:'',
           personTitle:'',
           productionTitle:'',
           introductionTitle:'',
           ticketDays:1,
           ticketHotValue:1
        }
        this.data4={
          presentIds:[]
        }
      },
      //编辑
      handleEdit(item) {
        this.data1={
          id:item.id,
          name:item.name,
          beginTime:item.beginTime,
          endTime:item.endTime,
          applyEndTime:item.applyEndTime,
          description:item.description,
          visitBase:item.visitBase,
          tagIds:item.tagIds
        }
        this.data2={
          cover:item.cover,
          poster:item.poster,
          ads:item.ads
        }
        this.data3={
           grossProfitRate:item.grossProfitRate,
           bonusProportion:item.bonusProportion,
           classification:item.classification,
           personTitle:item.personTitle,
           productionTitle:item.productionTitle,
           introductionTitle:item.introductionTitle,
           ticketDays:item.ticketDays,
           ticketHotValue:item.ticketHotValue
        }
        this.data4={
          presentIds:item.presentIds
        }
        this.$refs.addDialog.open()
      },
      submit(item) {
        createOrUpdate(item).then(() => {
          this.onLoad(this.page);
          this.resetData()
          this.$refs.addDialog.close()
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
        });
      },
      rowSave(row, done, loading) {
        createOrUpdate(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
      view(id,name){
        this.$router.push({
          name,
          query: {
            id
          }
        })
      },
    }
  };
</script>

<style>
/* #activity
    .el-table__row{
      height: 200px;
    } */
</style>
