<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.pointgoods_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 {add, getDetail, getList, remove, update,shelves,UnShelves} from "@/api/sing_goods/pointgoods";
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,
          dialogClickModal: false,
          labelWidth: 120,
          column: [
            // {
            //   label: "活动",
            //   prop: "activeId",
            //   type: "select",
            //   multiple: true,
            //   search: true,
            //   dataType: "string",
            //   dicUrl: "/api/sing_active/activerecord/list?current=1&size=999",
            //   props: {
            //     label: 'title',
            //     value: 'id',
            //     desc: 'id',
            //     res: "data.records"
            //   },
            //   dicMethod: 'get',
            //   rules: [{
            //     required: false,
            //     message: "请输入活动",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "商品名称",
              prop: "name",
              search: true,
              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: "point",
              rules: [{
                required: true,
                message: "请输入积分",
                trigger: "blur"
              }]
            },
            {
              label: "商品总数",
              prop: "total",
              rules: [{
                required: true,
                message: "请输入商品总数",
                trigger: "blur"
              }]
            },
            {
              label: "剩余商品数",
              prop: "remain",
              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: "isLimit",
              type: "switch",
              control:(val)=>{
                if(val===0){
                  return {
                    limitBuy:{
                      display:false
                    },
                  }
                }else{
                  return {
                    limitBuy:{
                      display:true
                    },
                  }
                }
              },
              dicData:[
                {
                  label:"否",
                  value:0
                },
                {
                  label:"是",
                  value:1,
                }
              ],
              value: 1,
              rules: [{
                required: true,
                message: "请开启或关闭状态",
                trigger: "blur"
              }]
            },
            {
              label: "限购数量",
              prop: "limitBuy",
              type: "number",
              value: 1,
              rules: [{
                required: true,
                message: "请输入限购数量",
                trigger: "blur"
              }]
            },
            {
              label: "商品类型",
              prop: "goodsType",
              rules: [{
                required: false,
                message: "请输入限购数量",
                trigger: "blur"
              }]
            },
            {
              label: "排序(数值越大排越后)",
              prop: "order",
              type: "number",
              value: 0,
              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.pointgoods_add, false),
          viewBtn: this.vaildData(this.permission.pointgoods_view, false),
          delBtn: this.vaildData(this.permission.pointgoods_delete, false),
          editBtn: this.vaildData(this.permission.pointgoods_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>
