<template>
  <basic-container>
    <avue-crud :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"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
            <template slot="menuLeft">
              <el-button type="danger"
                         size="small"
                         icon="iconfont icon-yanjing"
                         plain
                         v-if="permission.activehelprecord_view"
                         @click="helpRecordHide"> 废 弃
              </el-button>
              <el-button type="success"
                         size="small"
                         icon="iconfont icon-yanjing1"
                         plain
                         v-if="permission.activehelprecord_view"
                         @click="helpRecordShow"> 恢 复
              </el-button>
              <el-button type="primary"
                         size="small"
                         icon="el-icon-download"
                         plain
                         v-if="permission.activehelprecord_view"
                         @click="handleDownload">导 出
              </el-button>
            </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail,setStatusHide,setStatusShow,recordExport} from "@/api/sing_active/activehelprecord";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        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,
          addBtn: false,
          delBtn: false,
          editBtn: false,
          selection: true,
          dialogClickModal: false,
          column: [
            // {
            //   label: "用户ID",
            //   prop: "userId",
            //   rules: [{
            //     required: true,
            //     message: "请输入用户ID",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "用户",
              prop: "userName",
              editDisabled: true,
              search: true,
              rules: [{
                required: true,
                message: "请输入用户ID",
                trigger: "blur"
              }]
            },
            {
              label: "活动",
              prop: "activeId",
              search: true,
              searchslot: true,
              /*control:(val,form)=>{
                return this.controlActiveField(val,form);
              },*/
              searchSpan: 12,
              searchOrder: 0,
              type: "select",
              dicUrl: "/api/sing_active/activerecord/list?current=1&size=999",
              props: {
                label: 'title',
                value: 'id',
                //desc: 'id',
                res: "data.records"
              },
              dicMethod: 'get',
              rules: [{
                required: true,
                message: "请输入活动Id",
                trigger: "blur"
              }]
            },
            // {
            //   label: "作品ID",
            //   prop: "productId",
            //   rules: [{
            //     required: true,
            //     message: "请输入作品ID",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "作品",
              prop: "productTitle",
              search: true,
              rules: [{
                required: true,
                message: "请输入作品",
                trigger: "blur"
              }]
            },
            {
              label: "IP地址",
              prop: "ip",
              search: true,
              rules: [{
                required: true,
                message: "请输入IP地址",
                trigger: "blur"
              }]
            },
            {
              label: "道具",
              prop: "helpGoodsName",
              search: true,
              rules: [{
                required: true,
                message: "请输入IP地址",
                trigger: "blur"
              }]
            },
            {
              label: "票数",
              prop: "voteCount",
              rules: [{
                required: true,
                message: "请输入票数",
                trigger: "blur"
              }]
            },
            {
              label: "创建时间",
              prop: "createTime",
              format: "yyyy-MM-dd HH:mm:ss",
              searchRange: true,
              type: "datetime",
              search: true,
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              rules: [{
                required: true,
                message: "请输入创建时间",
                trigger: "blur"
              }]
            },
            {
              label: "状态",
              prop: "status",
              type: 'select',
              search: true,
              dicData: [{
                label:'正常',
                value: 1
              },{
                label:'已废弃',
                value: 0
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.activehelprecord_add, false),
          viewBtn: this.vaildData(this.permission.activehelprecord_view, false),
          delBtn: this.vaildData(this.permission.activehelprecord_delete, false),
          editBtn: this.vaildData(this.permission.activehelprecord_edit, false),
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {

      handleDownload(){
        recordExport(this.query).then(res => {
          let list = res.data.data;
          this.$Export.excel({
            title: "助力情况",
            columns: this.option.column,
            data: list
          })
        });

      },
      helpRecordHide(){
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据废弃?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return setStatusHide(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      helpRecordShow(){
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据恢复?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return setStatusShow(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
              //reload: true
            });
            this.$refs.crud.toggleSelection();
          });
      },
      /*rowSave(row, done, loading) {
        add(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;
        Object.assign(params, this.query);
        if (params.createTime) {
          params.beginTime = params.createTime[0];
          params.lastTime = params.createTime[1];
          params.createTime = null;
        }
        getList(page.currentPage, page.pageSize, params).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
