<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-del="rowDel"
      @row-save="rowSave" 
      @row-update="rowUpdate"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left>
        <el-button type="primary" @click="handleAdd">新增 </el-button>
        <el-button type="danger" icon="el-icon-delete" @click="handleDelete"
          >删除
        </el-button>
      </template>
      <template #menu-right>
        <el-button type="primary" icon="el-icon-upload" @click="handleImport"
          >导入
        </el-button>
      </template>
    </avue-crud>
      <!-- 导入 -->
    <basic-import v-if="isShowImport" title="导入" :isShow="isShowImport"
      templateUrl="/blade-desk/bsEpciuInspectionPoint/downloadExcelTemplate"
      templateName="环保巡检点配置表模板.xlsx"
      importUrl="/blade-desk/bsEpciuInspectionPoint/importExcel"
      @closeDialog="closeImportDialog"></basic-import>
      <!-- 新增弹窗 -->
    <addInspectDailog v-if="showDialog" :showDialog="showDialog" :moldAddMore="moldAddMore" @closeDialog="closeDialog"
      :title="title"></addInspectDailog>
  </basic-container>
</template>

<script>
import basicImport from '@/components/basic-import/main.vue';
import { getList, add, remove, update } from '@/api/safetyManagement/equipmentInspectionPoint';
import addInspectDailog from './addInspectDailog.vue'
export default {
  components: {
    basicImport,
    addInspectDailog
  },
  data() {
    return {
      isShowImport:false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      data: [],
      option: {
        height: "auto",
        calcHeight: 32,
        tip: false,
        size: "medium",
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: false,
        delBtn: false,
        addBtn: false,
        editBtn: true,
        editBtnText: "修改",
        addBtnIcon: " ",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        labelWidth: 120,
        menuWidth: 150,
        dialogWidth: 600,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        excelBtn: true,
        columnSort: true,
        index: false,
        showOverflowTooltip: true,
        searchLabelPosition: "left",
        searchLabelPosition: "left",
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: "left",
        gridBtn: false,
        searchMenuPosition: "right",
        align: "center",
        column: [
          {
            label: "巡检点编号",
            prop: "insNum",
            sortable: true,
            filter: true,
            span: 24,
            search: true,
            searchLabelWidth: 90,
            rules: [
              {
                required: false,
                message: "请输入巡检点编号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "巡检点位置",
            prop: "insSite",
            sortable: true,
            filter: true,
            span: 24,
            search: true,
            searchLabelWidth: 90,
            rules: [
              {
                required: false,
                message: "请输入巡检点位置",
                trigger: "blur",
              },
            ],
          },
          {
            label: "巡检点类型",
            prop: "insType",
            sortable: true,
            filter: true,
            span: 24,
            search: true,
            searchLabelWidth: 90,
            type: 'select',
            dicData: [
              { label: '废水巡检点', value: '1' },
              { label: '废气巡检点', value: '2' },
            ],
            rules: [
              {
                required: false,
                message: "请输入巡检点位置",
                trigger: "blur",
              },
            ],
          },
            {
            label: "巡检周期(h)",
            prop: "insCycle",
            sortable: true,
            filter: true,
            span: 24,
            search: false,
            searchLabelWidth: 90,
            // type: 'number',
            rules: [
              {
                required: false,
                message: "请输入巡检周期(h)",
                trigger: "blur",
              },
            ],
          },
          {
            label: "上次检验日期",
            prop: "lastInsCycle",
            sortable: true,
            filter: true,
            span: 24,
            search: false,
            display: false,
            searchLabelWidth: 100,
          },
          {
            label: "备注",
            prop: "memo",
            sortable: true,
            filter: true,
            span: 24,
            search: false,
            type: "textarea",
            searchLabelWidth: 90,
            rows: 3,
            rules: [
              {
                required: false,
                message: "请输入备注",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      title: '新增',
      showDialog: false,
      moldAddMore: false,
    };
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(',');
    },
  },
  methods: {
    // 点击导入按钮
    handleImport() {
      this.isShowImport = true
    },
    // 关闭导入弹窗
    closeImportDialog() {
      this.isShowImport = false;
      this.onLoad(this.page);
    },
    // 保存
    rowSave(row, done, loading) {
      add(row).then(
        res => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          done();
        },
        error => {
          window.console.log(error);
          loading();
        }
      );
    },
    //删除
    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: '操作成功!',
          });
        });
    },
    //更新
    rowUpdate(row, index, done, loading) {
     
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          done();
        },
        error => {
          window.console.log(error);
          loading();
        }
      );
    },
    //选择行
    selectionChange(list) {
      this.selectionList = list;
    },
    // 搜索重置
    searchReset() {
      this.query = {}
      this.onLoad(this.page)
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.table.clearSelection();
    },
    // 加载页面数据
    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;
        }
      );

    },
    //刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    //搜索
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params)
      done()

    },
    // 关闭弹窗
    closeDialog() {
      this.showDialog = false;
      this.onLoad(this.page);
    },
    // 新增
    handleAdd() {
      this.title = '新增';
      // this.tableData = [];
      this.showDialog = true;
      this.moldAddMore = true;
    },
  },
};
</script>

<style></style>
