<template>
  <div>
    <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"
      @row-save="rowSave"
      @row-update="rowUpdate"
    >
      <template #menu-left>
        <el-button type="danger" @click="handleDelete">删除 </el-button>
      </template>
      <template #menu-right>
        <el-button type="primary" @click="handleImport">导入 </el-button>
      </template>
      <template #menu="scope"> </template>
    </avue-crud>
    <!-- 导入 -->
    <basic-import
      v-if="isShowImport"
      title="导入"
      :isShow="isShowImport"
      templateUrl="/blade-desk/dsSpecialPro/import-template"
      templateName="工艺特殊过程项导入模板.xls"
      importUrl="/blade-desk/dsSpecialPro/import-excel-SpecialPro"
      @closeDialog="closeDialog"
    ></basic-import>
  </div>
</template>
<script>
import { getList, remove, add, update } from '@/api/processManagement/specialProcess';
import basicImport from '@/components/basic-import/main.vue';
export default {
  components: {
    basicImport,
  },
  data() {
    return {
      isShowImport: false,
      data: [],
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 32,
        tip: false,
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 12,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: false,
        delBtn: true,
        addBtn: true,
        editBtnText: '修改',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        viewBtnText: '详情',
        labelWidth: 120,
        menuWidth: 180,
        dialogWidth: 1040,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        excelBtn: true,
        columnSort: true,
        index: false,
        showOverflowTooltip: true,
        gridBtn: false,
        searchMenuPosition: 'right',
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'left',
        gridBtn: false,
        searchMenuPosition: 'right',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        align: 'center',
        column: [
          {
            label: '作业中心',
            prop: 'wcId',
            search: true,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'left',
            type: 'select',
            dicUrl: '/blade-desk/bsWorkCenter/getList',
            props: {
              label: 'wcName',
              value: 'id',
            },
            rules: [
              {
                required: true,
                message: '请输入',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '产线',
            prop: 'equipmentName',
            search: true,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'left',
            type: 'select',
            dicData: [
              {
                label: '产线1',
                value: 1,
              },
              {
                label: '产线2',
                value: 2,
              },
            ],
          },
          {
            label: '确认名称',
            prop: 'proItem',
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'left',
            rules: [
              {
                required: true,
                message: '请输入',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '确认编号',
            prop: 'condition',
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'left',
            rules: [
              {
                required: true,
                message: '请输入',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '确认到期日期',
            prop: 'lastDueTime',
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            type: 'date',
            format: 'YYYY-MM-DD',
            valueFormat: 'YYYY-MM-DD',
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '提醒天数',
            prop: 'remDays',
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            rules: [
              {
                required: true,
                message: '请输入',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '工艺员',
            prop: 'proMan',
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            type: 'select',
            dicUrl: '/blade-system/user/page?current=1&&size=99999',
            props: {
              label: 'name',
              value: 'id',
              res: 'data.records',
            },
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '备注',
            prop: 'remarks',
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'left',
          },
        ],
      },
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      query: {},
      loading: false,
    };
  },
  methods: {
    // 点击导入按钮
    handleImport() {
      this.isShowImport = true;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {});
    },
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    // 保存
    rowSave(row, done, loading) {
      add(row).then(
        res => {
          // 获取新增数据的相关字段
          const data = res.data.data;
          row.id = data.id;
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          // 数据回调进行刷新
          done();
        },
        error => {
          window.console.log(error);
          loading();
        }
      );
    },
    // 修改
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          // 数据回调进行刷新
          this.onLoad(this.page);
          done();
        },
        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();
          this.onLoad(this.page);
           this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
    },
    // 重置
    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();
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        this.data = res.data.data.records;
        this.loading = false;
        this.page.total = res.data.data.total;
        // this.selectionClear();
      });
    },
  },
};
</script>
<style lang="scss" scoped></style>
