<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model:page="page"
      v-model="form"
      ref="crud"
      :before-open="beforeOpen"
      @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"
      @sort-change="sortChange"
      :permission="permissionList"
    >
      <template #menu-left>
        <el-button type="danger" v-if="permission.workTankManagement_del" @click="handleDelete">
          删 除
        </el-button>
      </template>
      <template #menu-right>
        <el-button type="primary" v-if="permission.workTankManagement_import" @click="handleImport">
          导入
        </el-button>
      </template>
      <template #status-desc="{ type }">
        <p v-if="type == 'edit'" style="color: red">注：作业槽停用将停止下发槽液任务</p>
      </template>
      <template #status="{ row }">
        <div style="display: flex; align-items: center; justify-content: center">
          <span
            :style="{
              marginRight: '5px',
              backgroundColor: row.status == 1 ? '#00A854' : '#F04134',
              width: '12px',
              height: '12px',
              borderRadius: '50%',
              display: 'inline-block',
            }"
          ></span
          ><span>{{ row.status == 0 ? '暂停使用' : '使用中' }}</span>
        </div>
      </template>
    </avue-crud>
    <!-- 导入 -->
    <basic-import
      v-if="isShowImport"
      title="导入"
      :isShow="isShowImport"
      templateUrl="/blade-desk/BA/WorkTank/downloadExcelTemplate"
      templateName="作业槽模板.xls"
      importUrl="/blade-desk/BA/WorkTank/importExcel"
      @closeDialog="closeDialog"
    ></basic-import>
  </basic-container>
</template>

<script>
import {
  getList,
  addWorkTank,
  updateWorkTank,
  deleteWorkTank,
} from '@/api/basicData/workTankManagement';
import basicImport from '@/components/basic-import/main.vue';
import { mapGetters } from 'vuex';
export default {
  components: {
    basicImport,
  },
  data() {
    return {
      isShowImport: false,
      data: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      form: {},
      query: {},
      loading: true,
      excelBox: false,
      option: {
        tip: false,
        height: 'auto',
        calcHeight: 32,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        columnSort: true,
        tree: false,
        border: true,
        index: false,
        selection: true,
        viewBtn: false,
        delBtn: true,
        editBtnText: '修改',
        viewBtnText: '详情',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        labelWidth: 120,
        dialogWidth: 600,
        dialogClickModal: false,
        searchEnter: true,
        filterBtn: true,
        searchShowBtn: false,
        excelBtn: true,
        showOverflowTooltip: true,
        align: 'center',
        searchLabelPosition: 'left',
        searchLabelWidth: 'auto',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'center',
        menuWidth: 120,
        gridBtn: false,
        searchMenuPosition: 'right',
        column: [
          {
            label: '作业中心',
            prop: 'workstationId',
            type: 'select',
            sortable: 'custom',
            filter: true,
            span: 24,
            search: true,
            filterable: true,
            rules: [
              {
                required: true,
                message: '请输入作业中心',
                trigger: 'blur',
              },
            ],
            dicUrl: '/api/blade-desk/BA/WorkCenter/listForSelect',
            props: {
              label: 'wcName',
              value: 'id',
            },
          },
          {
            label: '槽号',
            prop: 'workTankCode',
            search: true,
            sortable: 'custom',
            filter: true,
            searchOrder: 1,
            // addDisplay: false,
            // editDisplay: false,
            // viewDisplay: false,
            span: 24,
            rules: [
              {
                required: true,
                message: '请输入槽号',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '作业槽描述',
            prop: 'name',
            search: false,
            sortable: 'custom',
            filter: true,
            span: 24,
            rules: [
              {
                required: true,
                message: '请输入作业槽描述',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '状态',
            prop: 'status',
            type: 'switch',
            search: true,
            sortable: 'custom',
            filter: true,
            span: 24,
            value: 1,
            dicData: [
              { label: '暂停使用', value: 0 },
              { label: '使用', value: 1 },
            ],
            width: 110,
          },
          {
            label: '操作人',
            prop: 'opUserRealName',
            search: false,
            sortable: 'custom',
            filter: true,
            editDisplay: false,
            addDisplay: false,
            span: 24,
            rules: [
              {
                required: true,
                message: '请输入作业槽描述',
                trigger: 'blur',
              },
            ],
            width: 100,
          },
          {
            label: '更新时间',
            prop: 'updateTime',
            search: false,
            sortable: 'custom',
            editDisplay: false,
            filter: true,
            addDisplay: false,
            span: 24,
            rules: [
              {
                required: true,
                message: '请输入作业槽描述',
                trigger: 'blur',
              },
            ],
            width: 180,
          },
        ],
      },
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: '模板上传',
            prop: 'excelFile',
            type: 'upload',
            drag: true,
            loadText: '模板上传中，请稍等',
            span: 24,
            propsHttp: {
              res: 'data',
            },
            tip: '请上传 .xls,.xlsx 标准格式文件',
            action: '/blade-system/user/import-user',
          },
          {
            label: '模板下载',
            prop: 'excelTemplate',
            formslot: true,
            span: 24,
          },
        ],
      },
      selectionList: [],
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.validData(this.permission.workTankManagement_add, false),
        editBtn: this.validData(this.permission.workTankManagement_edit, false),
        delBtn: this.validData(this.permission.workTankManagement_del, false),
      };
    },
  },
  methods: {
    beforeOpen(done, type) {
      if (type === 'edit') {
        // 将 workstationId 转换为字符串以匹配下拉框的 value 类型
        if (this.form.workstationId) {
          this.form.workstationId = this.form.workstationId + '';
        }
      }
      done();
    },
    // 点击导入按钮
    handleImport() {
      this.isShowImport = true;
    },
    closeDialog(val) {
      this.isShowImport = false;
      if (val) {
        this.onLoad();
      }
    },
    handleDelete() {
      if (this.selectionList.length == 0) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteWorkTank({
          ids: this.selectionList.map(item => item.id).join(','),
        }).then(res => {
          if (res.data.code == 200) {
            this.$message.success('删除成功');
            this.onLoad();
          }
        });
      });
    },
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    rowUpdate(row, index, done, loading) {
      // 将 workstationId 转回数字类型
      // if(row.workstationId) {
      //   row.workstationId = parseInt(row.workstationId)
      // }
      updateWorkTank(row)
        .then(res => {
          if (res.data.code == 200) {
            this.$message.success('修改成功');
            this.onLoad();
            done();
          }
        })
        .catch(err => {
          loading();
        });
    },
    rowSave(row, done, loading) {
      row.status = row.status == '' ? 0 : row.status;
      // 将 workstationId 转为数字类型
      // if(row.workstationId) {
      //   row.workstationId = parseInt(row.workstationId)
      // }
      addWorkTank(row)
        .then(res => {
          if (res.data.code == 200) {
            this.$message.success('新增成功');
            this.onLoad();
            done();
          }
        })
        .catch(err => {
          loading(false);
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    rowDel(row, index, done) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteWorkTank({
          ids: row.id,
        }).then(res => {
          if (res.data.code == 200) {
            this.$message.success('删除成功');
            this.onLoad();
            done();
          }
        });
      });
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
        // 排序
    sortChange({ prop, order }) {
      this.query.descs = undefined;
      this.query.ascs = undefined;
      let orderByFieldKey = order === 'descending' ? 'descs' : 'ascs';
      this.query[orderByFieldKey] = !prop
        ? undefined
        : prop.replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase();
      // // 重新加载数据
      this.onLoad(this.page, this.query);
    },
    onLoad() {
      this.loading = true;
      getList({
        current: this.page.currentPage,
        size: this.page.pageSize,
        ...this.query,
      }).then(res => {
        this.data = res.data.data.records;
        this.page.total = res.data.data.total;
        this.loading = false;
      });
    },
  },
};
</script>

<style></style>
