<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      :before-open="beforeOpen"
    >
      <template #menu-left> </template>

      <template #menu-right="{ size }">
        <span style="display: inline-flex; margin-right: 12px">
          <avue-select
            v-model="slectForm.wtName"
            placeholder="请选择能力类型"
            type="tree"
            :dic="slectDic"
            :filterable="true"
            :props="{
              label: 'dictValue',
              value: 'dictKey',
            }"
            style="width: 200px; margin-right: 12px"
          ></avue-select>
          <el-button type="primary" @click="batchSet()">批量设置</el-button>
          <el-button type="primary" @click="handleImport">导入</el-button>
        </span>
      </template>

      <template #name="{ row }">
        <i :class="row.source" style="margin-right: 5px" />
        <span>{{ row.name }}</span>
      </template>
      <template #source="{ row }">
        <div style="text-align: center">
          <i :class="row.source" />
        </div>
      </template>
    </avue-crud>

    <!-- 导入 -->
    <basic-import v-if="isShowImport" title="导入" :isShow="isShowImport"
      templateUrl="/blade-desk/QA/CycleTestItem/download-excel-template"
      templateName="试验项目模板.xlsx"
      importUrl="/blade-desk/QA/CycleTestItem/import-excel"
     @closeDialog="closeDialog"></basic-import>
  </basic-container>
</template>
<script>
import { getLazyList, remove, update, add, getMenu } from "@/api/system/menu";
import basicImport from '@/components/basic-import/main.vue'
import { mapGetters } from "vuex";
import {
  getQueryAllCaList,
  getQueryAllCaDeatils,
  getQueryAllCaSave,
  getQueryAllCaDel,
} from "@/api/processManagement/processCapability";
import { getDictionary } from "@/api/system/dict";

export default {
  components: {
    basicImport
  },
  data() {
    return {
      isShowImport:false,
      slectForm: {
        wtName: "",
      },
      slectDic: [],
      form: {},
      query: {},
      loading: true,
      selectionList: [],
      parentId: "",
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      formOption: {
        menuSpan: 12,
        enter: true,
        menuBtn: false,
        column: [
          {
            label: "",
            prop: "wtName",
            type: "select",
            filterable: true,
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dicUrl: "/blade-system/dict/dictionary?code=ProcessCapabilityType",
          },
        ],
      },
      option: {
        columnSort: true,
        tip: false,
        height: "auto",
        calcHeight: 32,
        simplePage: false,
        searchShow: true,
        searchMenuSpan: 6,
        dialogWidth: "60%",
        border: true,
        selection: true,
        viewBtn: true,
        addBtnIcon: " ",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        menuWidth: 150,
        dialogClickModal: false,
        excelBtn: true,
        viewBtn: false,
        editBtnText: "编辑",
        refreshBtn: false,
        searchShowBtn: false,
        gridBtn: false,
        searchIndex: 3,
        searchIcon: true,
        searchLabelPosition: "left",
        searchLabelPosition: "left",
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: "left",
        gridBtn: false,
        searchMenuPosition: "right",
        align: "center",

        column: [
          {
            label: "编码",
            prop: "caCode",
            search: true,
            sortable: true,
            rules: [
              {
                required: true,
                message: "请输入编码",
                trigger: "blur",
              },
            ],
          },
          {
            label: "名称",
            prop: "caName",
            sortable: true,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "能力类型",
            prop: "wtName",
            bind: "bsWorkType.wtName",
            sortable: true,
            search: true,
            type: "select",
            filterable: true,
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dicUrl: "/blade-system/dict/dictionary?code=ProcessCapabilityType",
            rules: [
              {
                required: false,
                message: "请选择能力类型",
                trigger: "click",
              },
            ],
          },
          {
            label: "修改时间",
            prop: "updateTime",
            sortable: true,
            search: true,
            display: false,
            type: "date",
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
    permissionList() {
      return {
        addBtn: this.validData(this.permission.menu_add, false),
        viewBtn: this.validData(this.permission.menu_view, false),
        delBtn: this.validData(this.permission.menu_delete, false),
        editBtn: this.validData(this.permission.menu_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  methods: {
    // 点击导入按钮
    handleImport() {
      this.isShowImport = true
    },
    getDictionary() {
      getDictionary({ code: "ProcessCapabilityType" }).then((res) => {
        this.slectDic = res.data.data;
      });
    },
    // 批量设置
    batchSet() {
      if (this.selectionList.length === 0) {
        return this.$message.warning("请先选择数据再进行操作！");
      }
      if (this.slectForm.wtName == "") {
        return this.$message.warning("请先选择能力类型！");
      }
      if (this.slectForm.wtName) {
        this.$confirm("请确认此操作！", "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$message.success("操作成功");
            // this.selectionArr.forEach((item) => {
            //     this.list.push(item.caId);
            // });
            // this.$ajax
            //     .post('bsCraftAbility/batchWtSet', {
            //         list: this.list,
            //         wtId: this.formData.wtId
            //     })
            //     .then((res) => {
            //         if (res.code === 0) {
            //             this.$message.success(
            //                 this.$t('global.successfulOperation')
            //             );
            //             this.$refs.myTable.load();
            //         }
            //     });
          })
          .catch(() => {
            this.$message.info(this.$t("global.canceled"));
          });
      }
    },
    initData() {
      // getMenuTree().then(res => {
      //     const column = this.findObject(this.option.column, 'parentId');
      //     column.dicData = res.data.data;
      // });
    },
    handleAdd(row) {
      this.parentId = row.id;
      const column = this.findObject(this.option.column, "parentId");
      column.value = row.id;
      column.addDisabled = true;
      this.$refs.crud.rowAdd();
    },
    // 保存
    rowSave(row, done, loading) {
      // add(row).then(
      //     res => {
      //         // 获取新增数据的相关字段
      //         const data = res.data.data;
      //         row.id = data.id;
      //         this.$message({
      //             type: 'success',
      //             message: '操作成功!',
      //         });
      //         // 数据回调进行刷新
      //         done(row);
      //     },
      //     error => {
      //         window.console.log(error);
      //         loading();
      //     }
      // );
    },
    // 修改
    rowUpdate(row, index, done, loading) {
      // update(row).then(
      //     () => {
      //         this.$message({
      //             type: 'success',
      //             message: '操作成功!',
      //         });
      //         // 数据回调进行刷新
      //         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);
        });
    },
    // 重置
    searchReset() {
      this.query = {};
      this.parentId = 0;
      this.onLoad(this.page);
    },
    // 搜索
    searchChange(params, done) {
      this.query = params;
      this.parentId = "";
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    // 选中表格数据
    selectionChange(list) {
      this.selectionList = list;
      console.log(67676, this.selectionList);
    },
    //
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    // 打开弹框 前操作  打开表单前会执行beforeOpen方法
    beforeOpen(done, type) {
      console.log(999, this.$refs.crud.data);
      // if (['add', 'edit'].includes(type)) {
      //     this.initData();
      // }
      // if (['edit', 'view'].includes(type)) {
      //     getMenu(this.form.id).then(res => {
      //         this.form = Object.assign(res.data.data, {
      //             hasChildren: this.form.hasChildren,
      //         });
      //         if (this.form.parentId === '0') {
      //             this.form.parentId = '';
      //         }
      //     });
      // }
      done();
    },
    // 关闭 弹框  关闭表单前会执行beforeClose方法，
    beforeClose(done) {
      this.parentId = "";
      const column = this.findObject(this.option.column, "parentId");
      column.value = "";
      column.addDisabled = false;
      done();
    },
    // 切换 页码
    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;

      // getLazyList(this.parentId, Object.assign(params, this.query)).then(res => {
      // this.data = res.data.data;
      this.data = [
        {
          bsWorkType: {
            deleted: false,
            keyValue: 1,
            wtCode: "code",
            wtId: 1,
            wtName: "name",
          },
          caCode: "E9881",
          caId: 239,
          caName: "热处理清洗",
          keyValue: 239,
          updateTime: "2023-03-10 15:36:22",
          id: "1",
        },
        {
          bsWorkType: null,
          caCode: "E9880",
          caId: 377,
          caName: "烧结后检验",
          keyValue: 377,
          updateTime: "2024-01-26 11:11:02",
          id: "2",
        },
        {
          bsWorkType: null,
          caCode: "E9726",
          caId: 238,
          caName: "喷特氟龙",
          keyValue: 238,
          updateTime: "2023-03-10 15:36:22",
          id: "3",
        },
        {
          bsWorkType: null,
          caCode: "E9725",
          caId: 243,
          caName: "耐原子氧涂层",
          keyValue: 243,
          updateTime: "2023-03-10 15:38:53",
          id: "4",
        },
        {
          bsWorkType: null,
          caCode: "E9724",
          caId: 242,
          caName: "喷码",
          keyValue: 242,
          updateTime: "2023-03-10 15:38:53",
          id: "5",
        },
        {
          bsWorkType: null,
          caCode: "E9723",
          caId: 241,
          caName: "CC化学转化膜层",
          keyValue: 241,
          updateTime: "2023-03-10 15:38:53",
          id: "6",
        },
        {
          bsWorkType: null,
          caCode: "E9722",
          caId: 240,
          caName: "钛合金阳极化",
          keyValue: 240,
          updateTime: "2023-03-10 15:38:53",
          id: "7",
        },
        {
          bsWorkType: null,
          caCode: "E9721",
          caId: 247,
          caName: "铬酸阳极化",
          keyValue: 247,
          updateTime: "2023-03-10 15:42:37",
          id: "8",
        },
      ];
      this.loading = false;
      this.page.total = this.data.length;
      this.selectionClear();
      // });
    },
  },

  mounted() {
    this.getDictionary();
  },
};
</script>
