<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="el-icon-delete" plain v-if="permission.present_delete"
          @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="isSendGoods" slot-scope="scope">
        <el-tag type="danger" v-if="scope.row.isSendGoods==0">不赠送</el-tag>
        <el-tag v-if="scope.row.isSendGoods==1">赠送</el-tag>
      </template>
      <template slot="isGiveMedal" slot-scope="scope">
        <el-tag type="danger" v-if="scope.row.isGiveMedal==0">不赠送</el-tag>
        <el-tag v-if="scope.row.isGiveMedal==1">赠送</el-tag>
      </template>

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


      <!-- 操作菜单 -->
      <template slot-scope="scope" slot="menu">
        <el-button type="text" size="small" icon="el-icon-view" plain class="none-border"
          @click.stop="handleView(scope.row)">查看
        </el-button>
        <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-delete" plain class="none-border"
          @click.stop="rowDel(scope.row,scope.index)">删除
        </el-button>
      </template>
    </avue-crud>

    <present-detail :form="detailData" ref="detailDialog"></present-detail>
    <present-add @submit="submit" :form1="data1" :form2="data2" :form3="data3"  ref="addDialog"></present-add>
  </basic-container>
</template>

<script>
  import presentDetail from "./comps/present-detail.vue"
  import presentAdd from "./comps/present-add.vue"
  import {
    getList,
    getDetail,
    add,
    update,
    remove
  } from "@/api/present/present";
  import {
    mapGetters
  } from "vuex";

  export default {
    components: {
      presentAdd,presentDetail
    },
    data() {
      return {
        //编辑
        data1: {
          id:'',
          name: '',
          price: '',
          hotValue: '',
          pointsValue: '',
          icon: '',
        },
        data2: {
          isGiveMedal: 0,
          giveMedalId: '',
        },
        data3: {
          isSendGoods: 0,
          goodsPostAmount: '',
          goodsName: '',
          goodsPic: '',
          donateMedalId: ''
        },
        //详情
        detailData:{},

        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: false,
          editBtn: false,
          addBtn: false,
          delBtn: false,
          selection: true,
          dialogClickModal: false,
          labelWidth: 150,
          column: [{
              label: "礼物图标",
              prop: "icon",
              type: "upload",
              propsHttp: {
                url: 'data'
              },
              listType: "picture-img",
              action: '/api/putObject',
              rules: [{
                required: true,
                message: "请输入礼物图标",
                trigger: "blur"
              }]
            },
            {
              label: "名称",
              prop: "name",
              rules: [{
                required: true,
                message: "请输入名称",
                trigger: "blur"
              }]
            },
            {
              label: "价格",
              prop: "price",
              rules: [{
                required: true,
                message: "请输入价格",
                trigger: "blur"
              }]
            },
            {
              label: "热力值",
              prop: "hotValue",
              rules: [{
                required: true,
                message: "请输入热力值",
                trigger: "blur"
              }]
            },
            {
              label: "积分值",
              prop: "pointsValue",
              rules: [{
                required: true,
                message: "请输入积分值",
                trigger: "blur"
              }]
            },
            {
              label: "打赏是否赠送实物",
              prop: "isSendGoods",
              slot: true,
              rules: [{
                required: true,
                message: "请输入是否赠送实物",
                trigger: "blur"
              }]
            },
            {
              label: "实物名称",
              hide: true,
              prop: "goodsName",
              rules: [{
                required: true,
                message: "请输入实物名称",
                trigger: "blur"
              }]
            },
            {
              label: "实物图片",
              hide: true,
              prop: "goodsPic",
              type: "upload",
              listType: "picture-img",
              action: '/api/putObject',
              propsHttp: {
                url: 'data'
              },
              rules: [{
                required: true,
                message: "请输入实物图片",
                trigger: "blur"
              }]
            },
            {
              label: "多少包邮",
              hide: true,
              prop: "goodsPostAmount",
              rules: [{
                required: true,
                message: "请输入多少包邮",
                trigger: "blur"
              }]
            },
            {
              label: "捐赠的勋章id",
              hide: true,
              prop: "donateMedalId",
              rules: [{
                required: true,
                message: "请输入捐赠的勋章id",
                trigger: "blur"
              }]
            },
            {
              label: "打赏是否赠送勋章。",
              prop: "isGiveMedal",
              slot: true,
              rules: [{
                required: true,
                message: "请输入是否赠送勋章",
                trigger: "blur"
              }]
            },
            {
              label: "赠送勋章的id",
              hide: true,
              prop: "giveMedalId",
              rules: [{
                required: true,
                message: "请输入赠送勋章的id",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.present_add, false),
          viewBtn: this.vaildData(this.permission.present_view, false),
          delBtn: this.vaildData(this.permission.present_delete, false),
          editBtn: this.vaildData(this.permission.present_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      handleView(item){
        getDetail(item.id).then(res=>{
          this.detailData=res.data.data
          this.$refs.detailDialog.open()
        })
      },
      showAdd(){
        if (!this.$isEmpty(this.data1.id)) {
          this.resetData()
        }
        this.$refs.addDialog.open()
      },
      //编辑
      handleEdit(item) {
        this.data1= {
          id:item.id,
          name: item.name,
          price: item.price,
          hotValue: item.hotValue,
          pointsValue: item.pointsValue,
          icon: item.icon,
        }
        this.data2= {
          isGiveMedal: item.isGiveMedal,
          giveMedalId: item.giveMedalId,
        }
        this.data3= {
          isSendGoods: item.isSendGoods,
          goodsPostAmount: item.goodsPostAmount,
          goodsName: item.goodsName,
          goodsPic: item.goodsPic,
          donateMedalId: item.donateMedalId
        },
        this.$refs.addDialog.open()
      },
      resetData(){
        this.data1= {
          id:'',
          name: '',
          price: '',
          hotValue: '',
          pointsValue: '',
          icon: '',
        }
        this.data2= {
          isGiveMedal: 0,
          giveMedalId: '',
        }
        this.data3= {
          isSendGoods: 0,
          goodsPostAmount: '',
          goodsName: '',
          goodsPic: '',
          donateMedalId: ''
        }
      },
      submit(item) {
        add(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) {
        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>
