<template>
  <div>
    <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="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/bsTower/downloadExcelTemplate"
      templateName="报警点配置模板.xlsx"
      importUrl="/blade-desk/bsTower/importExcel"
      @closeDialog="closeImportDialog"
    ></basic-import>
  </div>
</template>
<script>
import basicImport from '@/components/basic-import/main.vue';
import { getList, add, remove, update } from '@/api/safetyManagement/acidMistToweralarmPoint.js'

export default {
  components: {
    basicImport,
  },
  data() {
    return {
      isShowImport: false,
      loading: false,
      selectionList: [],
      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,
        editBtnText: "修改",
        addBtnIcon: " ",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        viewBtnText: "详情",
        labelWidth: 120,
        menuWidth: 120,
        dialogWidth: 1040,
        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: "btCode",
            sortable: true,
            filter: true,
            span: 12,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入编码",
                trigger: "blur",
              },
            ],
          },
          {
            label: "名称",
            prop: "btName",
            search: true,
            sortable: true,
            filter: true,
            span: 12,
            rules: [
              {
                required: true,
                message: "请输入名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "药品名称",
            prop: "drugName",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            rules: [
              {
                required: true,
                message: "请输入药品名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "报警阙值",
            prop: "threshold",
            search: true,
            sortable: true,
            filter: true,
            type: "number", 
            span: 12,
            rules: [
              {
                required: true,
                message: "请输入报警阙值",
                trigger: "blur",
              },
            ],
          },
          {
            label: "PH报警点位",
            prop: "phPoint",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            rules: [
              {
                required: true,
                message: "请输入PH报警点位",
                trigger: "blur",
              },
            ],
          },
          {
            label: "风机运行点位",
            prop: "fanRunPoint",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            rules: [
              {
                required: true,
                message: "请输入风机运行点位",
                trigger: "blur",
              },
            ],
          },
          {
            label: "风机报警点位",
            prop: "fanPoint",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
          },
          {
            label: "循环泵报警点位",
            prop: "pumpPoint",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
          },
        ],
      },
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
    };
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(',');
    },
  },
  // 关闭导入弹窗
  closeImportDialog() {
    this.isShowImport = false;
    this.onLoad(this.page);
  },
  methods: {
    // 点击导入按钮
    handleImport() {
      this.isShowImport = true
    },
    //删除
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          this.$refs.crud.toggleSelection();
        });
    },
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    //新增
    rowSave(row, done, loading) {
      add(row).then(res => {
        this.onLoad(this.page)
        this.$message({
          type: 'success',
          messge:'操作成功!'
        })
        done();
      },
        error => {
        window.console.log(error)
        loading()
      }
      )  
    },
    //删除
    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();
        }
      );
    },
    // 重置
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.table.clearSelection();
    },
    // 搜索
    searchChange(params, done) {
      this.query = params;
      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 => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
  },
};
</script>
<style lang="scss" scoped></style>
