<template>
  <basic-container>
    <!-- 累计检查项配置 -->
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      @row-update="rowUpdate"
      @row-del="rowDel"
    >
      <template #menu-left>
        <el-button type="primary" @click="handleAdd">新增</el-button>
        <!-- <el-button type="danger" @click="handleDelete">删除</el-button> -->
        <el-button type="primary" @click="handleMaintain">批量重置</el-button>
      </template>
    </avue-crud>

    <!--批量维护 -->
    <maintain-dialog
      v-if="showMaintain"
      :show-maintain="showMaintain"
      :maintain-data="maintainData"
      @closeDialog="closeDialog"
    ></maintain-dialog>
    <!-- 新增 -->
    <addAccumulateClass
      v-if="showDialog"
      :showDialog="showDialog"
      @closeDialog="closeDialog"
      :moldAddMore="moldAddMore"
      :maintain-data="maintainData"
      :title="title"
    ></addAccumulateClass>
  </basic-container>
</template>

<script>
import addAccumulateClass from './components/addAccumulateClassDialog.vue';
import maintainDialog from './components/maintainDialog.vue';
import { getList, add, remove, update } from '@/api/processManagement/planClass.js';

export default {
  components: {
    maintainDialog,
    addAccumulateClass,
  },
  data() {
    return {
      title: '新增',
      moldAddMore: false,
      loading: false,
      data: [],
      selectionList: [],
      form: {},
      isQualified: null,
      showMaintain: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },

      showDialog: false,
      tableData: [],
      option: {
        height: 'auto',
        calcHeight: 32,
        rowKey: 'id',
        tip: false,
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 12,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: false,
        selection: true,
        viewBtn: false,
        delBtn: true,
        addBtn: false,
        editBtn: true,
        editBtnText: '修改',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        viewBtnText: '详情',
        labelWidth: 120,
        searchLabelWidth: 120,
        menuWidth: 110,
        dialogWidth: 1200,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        excelBtn: true,
        columnSort: true,
        showOverflowTooltip: true,
        menuAlign: 'left',
        gridBtn: false,
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        searchMenuPosition: 'right',
        align: 'center',
        column: [
          {
            label: '作业中心',
            prop: 'wcId',
            type: 'select',
            filterable: true,
            clearable: true,
            search: true,
            sortable: true,
            overHidden: true,
            headerAlign: 'center',
            align: 'left',
            searchLabelWidth: 80,
            width: 120,
            rules: [
              {
                required: true,
                message: '请输入作业中心',
                trigger: 'blur',
              },
            ],
            dicUrl: '/blade-desk/bsWorkCenter/getList',
            props: {
              label: 'wcName',
              value: 'id',
            },
          },
          {
            label: '作业槽',
            prop: 'batchNo',
            search: false,
            sortable: true,
            overHidden: true,
            headerAlign: 'center',
            align: 'center',
            width: 120,
            dicUrl: `/blade-desk/BA/WorkTank/listForSelect?workstationId={{wcId}}`,
            props: {
              label: 'name',
              value: 'id',
            },
            rules: [
              {
                required: true,
                message: '请输入作业槽',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '设备名称',
            prop: 'deviceName',
            search: false,
            sortable: true,
            overHidden: true,
            headerAlign: 'center',
            align: 'left',
            width: 120,
          },
          {
            label: '规则类型',
            prop: 'ruleType',
            type: 'select',
            filterable: true,
            clearable: true,
            search: true,
            sortable: true,
            overHidden: true,
            headerAlign: 'center',
            align: 'left',
            searchLabelWidth: 80,
            width: 120,
            rules: [
              {
                required: true,
                message: '请输入规则类型',
                trigger: 'blur',
              },
            ],
            dicData: [
              {
                label: '电量差类',
                value: 1,
              },
              {
                label: '累计电量类',
                value: 2,
              },
              {
                label: '累计生产日期',
                value: 3,
              },
              {
                label: '主盐添加量类',
                value: 4,
              },
              {
                label: '累计生产面积',
                value: 5,
              },
              {
                label: '累计主盐添加量',
                value: 6,
              },
            ],
          },
          {
            label: '维护内容',
            prop: 'preserveContent',
            search: false,
            sortable: true,
            overHidden: true,
            headerAlign: 'center',
            align: 'left',
            width: 120,
            rules: [
              {
                required: true,
                message: '请输入维护内容',
                trigger: 'blur',
              },
            ],
          },
          //
          {
            label: '状态',
            prop: 'accumulatedStatus',
            search: false,
            sortable: true,
            overHidden: true,
            headerAlign: 'center',
            align: 'left',
            width: 120,
            type: 'select',
            dicData: [
              {
                label: '进行中',
                value: 1,
              },
              {
                label: '已完成',
                value: 2,
              },
            ],
          },
          {
            label: '添加量',
            prop: 'addQuantity',
            search: false,
            sortable: true,
            overHidden: true,
            headerAlign: 'center',
            align: 'center',
            width: 120,
            rules: [
              {
                required: true,
                message: '请输入添加量',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '添加量倍数',
            prop: 'addQuantityMultiple',
            search: false,
            sortable: true,
            overHidden: true,
            headerAlign: 'center',
            align: 'center',
            width: 120,
            rules: [
              {
                required: true,
                message: '请输入添加量倍数',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '累计触发值',
            prop: 'cumulativeTriggerValue',
            search: false,
            sortable: true,
            overHidden: true,
            headerAlign: 'center',
            align: 'center',
            width: 120,
            rules: [
              {
                required: true,
                message: '请输入累计触发值',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '当前值',
            prop: 'currentValue',
            search: false,
            sortable: true,
            overHidden: true,
            headerAlign: 'center',
            align: 'center',
            width: 140,
            rules: [
              {
                required: true,
                message: '请输入当前值(累计)',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '累计初始值',
            prop: 'cumulativeInitialValue',
            search: false,
            sortable: true,
            overHidden: true,
            headerAlign: 'center',
            align: 'center',
            width: 120,
            rules: [
              {
                required: true,
                message: '请输入累计初始值',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '有效期',
            prop: 'periodOfValidity',
            search: false,
            sortable: true,
            overHidden: true,
            headerAlign: 'center',
            align: 'left',
            width: 120,
            type: 'select',
            dicData: [
              {
                label: '长期',
                value: 1,
              },
              {
                label: '一次性',
                value: 2,
              },
            ],
          },
          
        ],
      },
    };
  },
  mounted() {},
  methods: {
    // 关闭弹窗
    closeDialog() {
      this.showMaintain = false;
      this.showDialog = false;
      this.onLoad(this.page);
    },
    selectionChange(val) {
      this.selectionList = val;
    },
    // 新增
    handleAdd() {
      this.tableData = [];
      this.showDialog = true;
      this.moldAddMore = true;
    },
    // 批量维护
    handleMaintain() {
      if (this.selectionList.length == 0) {
        this.$message.error('请先选择数据');
        return;
      }
      this.maintainData = this.selectionList;
      this.title = ' 批量维护';
      // this.showMaintain = true;
      this.showDialog = true;
      this.moldAddMore = false;
    },
    handleDelete() {
      if (this.selectionList.length == 0) {
        this.$message.error('请先选择数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {});
    },

    handleSet() {
      if (this.isQualified == null) {
        this.$message.error('请先选择检查结果');
        return;
      }
      this.maintainData.map(item => {
        item.isQualified = this.isQualified;
      });
    },
    maintainValue() {
      this.showMaintain = false;
    },
    insertEvent() {
      const record = { bsWorkCenter: { wcId: '' }, _select: false };
      this.tableData.push(record);
    },
    selectChange(list, row) {
      row._select = !row._select;
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          done();
        },
        error => {
          window.console.log(error);
          loading();
        }
      );
    },
    handleSave() {
      this.showDialog = false;
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      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;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, { bbpType: 2 })
      ).then(res => {
        this.data = res.data.data.records;
        this.loading = false;
        this.page.total = res.data.data.total;
      });
    },
  },
};
</script>

<style></style>
