<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :page.sync="page"
      :permission="permissionList"
      @row-del="rowDel"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @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"
          v-if="permission.produce_delete"
          plain
          @click="handleDelete"
        >删 除</el-button>
        <el-button
          size="small"
          icon="el-icon-delete"
          @click="handleMenuSetting"
          v-if="permission.topmenu_setting"
          plain
        >菜单配置</el-button>
      </template>
    </avue-crud>
    <el-dialog title="下级菜单配置" append-to-body :visible.sync="box" width="345px">
      <el-tree
        :data="menuGrantList"
        show-checkbox
        node-key="id"
        ref="treeMenu"
        :default-checked-keys="menuTreeObj"
        :props="props"
      ></el-tree>

      <span slot="footer" class="dialog-footer">
        <el-button @click="box = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </basic-container>
</template>

<script>
import {
  getList,
  remove,
  update,
  add,
  grant,
  grantTree,
  getProduceTree
} from "@/api/system/produce";
import { mapGetters } from "vuex";

export default {
  name: "produce",
  data() {
    return {
      box: false,
      menuGrantList: [],
      menuTreeObj: [],
      props: {
        label: "title",
        value: "key"
      },
      form: {},
      selectionList: [],
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        height: "auto",
        calcHeight: 30,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        indexLabel: "序号",
        selection: true,
        viewBtn: true,
        column: [
          {
            label: "产品名称",
            prop: "produceName",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入参数",
                trigger: "blur"
              }
            ]
          },
          {
            label: "产品标志",
            prop: "produceSign",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入参数",
                trigger: "blur"
              }
            ]
          },
          {
            label: "产品类别",
            prop: "useType",
            rules: [
              {
                required: true,
                message: "请输入参数",
                trigger: "blur"
              }
            ]
          },
          {
            label: "产品说明",
            prop: "descInfo",
            rules: [
              {
                required: true,
                message: "请输入参数",
                trigger: "blur"
              }
            ]
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.produce_add, false),
        viewBtn: this.vaildData(this.permission.produce_view, false),
        delBtn: this.vaildData(this.permission.produce_delete, false),
        editBtn: this.vaildData(this.permission.produce_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    submit() {
      const menuList = this.$refs.treeMenu.getCheckedKeys().join(",");
      grant(this.ids, menuList).then(() => {
        this.box = false;
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        this.onLoad(this.page);
      });
    },
    rowSave(row, loading, done) {
      add(row).then(
        () => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        () => {
          done();
        }
      );
    },
    rowUpdate(row, index, loading, done) {
      update(row).then(
        () => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        () => {
          done();
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    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();
        });
    },
    handleMenuSetting() {
      if (this.selectionList.length !== 1) {
        this.$message.warning("只能选择一条数据");
        return;
      }
      this.menuTreeObj = [];
      //获取树形菜单
      grantTree().then(res => {
        this.menuGrantList = res.data.data.menu;
        getProduceTree(this.ids).then(res => {
          this.menuTreeObj = res.data.data.menu;
          this.box = true;
        });
      });
    },
    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>
