<template>
  <basic-container>
    <SearchHeader
      style="margin-left: -10px"
      :searchForm="searchForm"
      :searchText="searchText"
      @searchHandle="searchHandle"
      @addHandle="addHandle"
    ></SearchHeader>
    <avue-crud
      id="avue-id"
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :permission="permissionList"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      @row-del="rowDel"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
    >
      <template slot-scope="{ row, index }" slot="menu">
        <el-button @click="$refs.crud.rowEdit(row, index)" type="primary">编辑</el-button>
        <el-button @click="$refs.crud.rowDel(row, index)">删除</el-button>
        <el-button style="width: 90px" @click.stop="handleAdd(row, index)"
          >新增子项</el-button
        >
      </template>
      <template slot-scope="{ row }" slot="status">
        <el-tag v-if="row.status === 1" @click="changeStatus(row)" type="danger"
          >禁用</el-tag
        >
        <el-tag v-else @click="changeStatus(row)">启用</el-tag>
      </template>
      <!-- 弹框内按钮 -->
      <template slot-scope="{ type }" slot="menuForm">
        <el-button size="small" @click="$refs.crud.closeDialog()" class="cus-cencel"
          >取消</el-button
        >
        <el-button
          size="small"
          v-if="type == 'add'"
          @click="$refs.crud.rowSave()"
          class="confirm"
          >确定</el-button
        >
        <el-button
          size="small"
          v-if="type == 'edit'"
          @click="$refs.crud.rowUpdate()"
          class="confirm"
          >提交</el-button
        >
      </template>
    </avue-crud>
  </basic-container>
</template>
<script>
import {
  getList,
  getListTree,
  add,
  remove,
  update,
} from "@/api/capital/productclass";
import { mapGetters } from "vuex";
import { tableOption } from "@/const/capital/productclass";
export default {
  data() {
    return {
      search: {},
      form: {},
      selectionList: [],
      searchText: {
        flag: 3,
        addText: "新 增",
      },
      // 查询
      searchForm: {
        name: "",
      },
      loading: true,
      parentId: 0,
      // productCategory:0,
      page: {
        size: 10,
        surrent: 1,
        total: 0,
      },
      option: tableOption,
      data: [],
    };
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
    permissionList() {
      return {
        // addBtn: this.vaildData(this.permission.itemType_add, false),
        // viewBtn: this.vaildData(this.permission.dept_view, false),
        // delBtn: this.vaildData(this.permission.itemType_delete, false),
        // editBtn: this.vaildData(this.permission.itemType_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  mounted() {
    this.onLoad();
  },
  methods: {
    // 搜索
    searchHandle(item,index) {
      // 重置
      if (index === 1) {
        this.searchForm = {name: ''};
      }else {
				this.searchForm = {...item}
			}
      this.onLoad();
    },
    // 新增
    addHandle() {
      this.$refs.crud.rowAdd();
    },
    /**
     * 单独改状态
     */
    changeStatus(row) {
      if (row.status === 1) {
        row.status = 0;
      } else if (row.status === 0) {
        row.status = 1;
      } else {
        row.status = 0;
      }
      console.log("value:", row);
      update(row).then(() => {
        this.$message({ type: "success", message: "修改成功!" });
        this.onLoad();
      });
    },
    // 表单中
    initData() {
      getListTree().then((res) => {
        const column = this.findObject(this.option.column, "parentId");
        column.dicData = res.data.data;
      });
    },
    //新增子项
    handleAdd(row, index) {
      this.parentId = row.id;
      const column = this.findObject(this.option.column, "parentId");
      column.value = row.id;
      column.addDisabled = true;
      this.form.productCategory = row.productCategory;
      this.$refs.crud.rowAdd();
      const columns = this.findObject(this.option.column, "productCategory");
      columns.disabled = true;
    },
    rowSave(row, done, loading) {
      // 处理是否危险品按钮是否选择
      let danger = row.danger === "" ? 0 : row.danger;
      let params = {
        ...row,
        danger,
      };
      add(params).then(
        (res) => {
          // 获取新增数据的相关字段
          const data = res.data.data;
          row.id = data.id;
          this.$message({ type: "success", message: "新增成功!" });
          this.onLoad();
          // 数据回调进行刷新
          done(row);
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.$message({ type: "success", message: "修改成功!" });
          this.onLoad();
          // 数据回调进行刷新
          done(row);
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row, index, done) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 数据回调进行刷新
          done(row);
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          // 刷新表格数据并重载
          this.data = [];
          this.parentId = 0;
          this.$refs.crud.refreshTable();
          this.$refs.crud.toggleSelection();
          // 表格数据重载
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    beforeOpen(done, type) {
      if (["add", "edit"].includes(type)) {
        this.initData();
      }

      done();
    },
    beforeClose(done) {
      this.parentId = "";
      const column = this.findObject(this.option.column, "parentId");
      column.value = "";
      column.addDisabled = false;
      const columns = this.findObject(this.option.column, "productCategory");
      columns.disabled = false;
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    /**
     * 列表数据加载
     */
    onLoad() {
      this.loading = true;
      const { current, size } = this.page;
      getList(
        Object.assign({ current, size }, this.searchForm, this.parentId)
      ).then((res) => {
        this.data = res.data.data;
        this.loading = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
/deep/ .avue-crud__menu {
  display: none;
}
</style>
