<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-scope="scope" slot="content">
        <el-popover
          placement="top-start"
          title="全部内容"
          width="400"
          trigger="hover"
          :content="scope.row.content">
          <el-input
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 10}"
            ref="generateMenuJson"
            readonly
            v-model="scope.row.content">
          </el-input>
          <p slot="reference" style="overflow:hidden; text-overflow:ellipsis; white-space:nowrap">{{scope.row.content}}</p>
        </el-popover>
      </template>
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.helpgoods_delete"
                   @click="handleDelete">删 除
        </el-button>
        <el-button type="success"
                   size="small"
                   icon="el-icon-upload2"
                   plain
                   @click="goodsShelves">上 架
        </el-button>
        <el-button type="warning"
                   size="small"
                   icon="el-icon-download"
                   plain
                   @click="goodsUnShelves">下 架
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove,UnShelves,shelves} from "@/api/sing_goods/helpgoods";
  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,
          selection: true,
          labelWidth: 250,
          dialogClickModal: false,
          column: [
            {
              label: "活动",
              prop: "activeId",
              type: "select",
              dicUrl:"/api/sing_active/activerecord/list?current=1&size=999",
              search: true,
              props: {
                label: 'title',
                value: 'id',
                desc: 'id',
                res: "data.records"
              },
              rules: [{
                required: true,
                message: "请输入商品名称",
                trigger: "blur"
              }]
            },
            {
              label: "商品名称",
              prop: "name",
              rules: [{
                required: true,
                message: "请输入商品名称",
                trigger: "blur"
              }]
            },
            {
              label: "商品图片",
              prop: "imgUrl",
              type: 'upload',
              listType: 'picture-img',
              dataType: 'string',
              span: 12,
              propsHttp: {
                res: 'data',
                url: 'link'
              },
              action: '/api/blade-resource/oss/endpoint/put-file',
              rules: [{
                required: true,
                message: "请输入活动图片地址",
                trigger: "blur"
              }]
            },
            {
              label: "支付类型",
              prop: "payType",
              type: "radio",
              dicData:[
                {
                  label: "积分支付",
                  value: "POINT_EXCHANGE_GOODS"
                },
                {
                  label: "现金支付",
                  value: "CASH_EXCHANGE_GOODS"
                }
              ],
              value: "POINT_EXCHANGE_GOODS",
              control:(value)=>{
                if(value==="POINT_EXCHANGE_GOODS"){
                  return {
                    cmccGoodsId:{
                      display: true
                    },
                    point:{
                      display: true
                    },
                    amount:{
                      display: false
                    }
                  };
                }else{
                  return {
                    cmccGoodsId:{
                      display: false
                    },
                    point:{
                      display: false
                    },
                    amount:{
                      display: true
                    }
                  };
                }
              },
              rules: [{
                required: true,
                message: "请输入支付类型",
                trigger: "blur"
              }]
            },
            {
              label: "移动商品道具ID",
              prop: "cmccGoodsId",
              display: true,
              rules: [{
                required: false,
                message: "请输入移动商品道具ID",
                trigger: "blur"
              }]
            },
            {
              label: "购买积分",
              prop: "point",
              type: "number",
              display: true,
              rules: [{
                required: true,
                message: "请输入购买积分(需要和移动道具对应)",
                trigger: "blur"
              }]
            },
            {
              label: "支付金额(单位:元)",
              prop: "amount",
              type: "number",
              display: true,
              rules: [{
                required: true,
                message: "请输入购买需要支付金额(单位:元)",
                trigger: "blur"
              }]
            },
            {
              label: "商品可兑换次数/人/天",
              prop: "total",
              type: "number",
              labelTip: "设置每个用户每天可以兑换次数，全部所有作品共享次数",
              value: -1,
              rules: [{
                required: true,
                message: "商品可兑换次数/人/天(-1表示无限制)",
                trigger: "blur"
              }]
            },
            {
              label: "作品可投票数/作品/天/人",
              prop: "activeVote",
              type: "number",
              labelTip: "比如一个用户，允许每天可以对同一个作品投2次票，而且兑换一次道具可以得到2票，则应该设置数量为 2*2=4",
              value: 1,
              rules: [{
                required: true,
                message: "请输入作品可投票数/作品/天/人(-1表示无限制)",
                trigger: "blur"
              }]
            },
            {
              label: "兑换可得票数/个",
              prop: "votePointRate",
              labelTip: "每兑换一次该道具可获得得票数，并且兑换完成后该票数立即计入对该作品票数中",
              type: "number",
              rules: [{
                required: true,
                message: "请输入可获得票数/个",
                trigger: "blur"
              }]
            },
            {
              label: "道具状态",
              prop: "status",
              type: "switch",
              dicData:[
                {
                  label:"下架",
                  value:0
                },
                {
                  label:"上架",
                  value:1,
                }
              ],
              value: 1,
              rules: [{
                required: true,
                message: "请开启或关闭状态",
                trigger: "blur"
              }]
            },
            {
              label: "商品内容",
              prop: "content",
              type: "textarea",
              slot: true,
              span: 24,
              minRows: 3,
              rules: [{
                required: true,
                message: "请输入商品内容",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.helpgoods_add, false),
          viewBtn: this.vaildData(this.permission.helpgoods_view, false),
          delBtn: this.vaildData(this.permission.helpgoods_delete, false),
          editBtn: this.vaildData(this.permission.helpgoods_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      goodsShelves(){
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return shelves(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      goodsUnShelves(){
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return UnShelves(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            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;
        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();
        });
      }
    }
  };
</script>

<style>
</style>
