<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <!-- <el-col :span="4" :xs="24">
        <el-scrollbar>
          <div class="tree-overflow">
            <div class="head-container">
              <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search"
                style="margin-bottom: 20px;width: 160px;" />
            </div>
            <div class="head-container">
              <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
                :filter-node-method="filterNode" ref="tree" highlight-current @node-click="handleNodeClick" />
            </div>
          </div>
        </el-scrollbar>
      </el-col> -->
      <el-col :span="24" :xs="24">
        <!-- 搜索工作栏 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="140px">
          <el-form-item label="检查项名称" prop="inspName">
            <el-input v-model="queryParams.inspName" placeholder="请输入检查项名称" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <!-- <el-form-item label="检查项代码" prop="inspCode">
            <el-input v-model="queryParams.inspCode" placeholder="请输入检查项代码" clearable @keyup.enter.native="handleQuery" />
          </el-form-item> -->
          <el-form-item label="检查项类型" prop="inspType">
            <el-select v-model="queryParams.inspType" placeholder="请选择检查项类型" clearable size="small">
              <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DICT_INSPECT_TYPE)" :key="dict.value"
                :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
              <el-option v-for="dict in statusDictDatas" :key="parseInt(dict.value)" :label="dict.label"
                :value="parseInt(dict.value)" />
            </el-select>
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 操作工具栏 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['backend:inspect-info:create']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
              :loading="exportLoading" v-hasPermi="['backend:inspect-info:export']">导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <!-- 列表 -->
        <el-table v-loading="loading" :data="list">
          <el-table-column label="ID" align="center" prop="id" />
          <el-table-column label="检查项名称" align="center" prop="inspName" />
          <el-table-column label="检查项代码" align="center" :show-overflow-tooltip="true">
            <template v-slot="scope">
              <router-link :to="'/inspectInfo/list/data/' + scope.row.id" class="link-type">
                <span>{{ scope.row.inspCode }}</span>
              </router-link>
            </template>
          </el-table-column>
          <el-table-column label="检查项类型" align="center" prop="inspType">
            <template v-slot="scope">
              <dict-tag :type="DICT_TYPE.DICT_INSPECT_TYPE" :value="scope.row.inspType" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="状态" align="center" prop="status">
            <template v-slot="scope">
              <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
            </template>
          </el-table-column> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['backend:inspect-info:update']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['backend:inspect-info:delete']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="上级检查项" prop="pid">
          <treeselect v-model="form.pid" :options="deptOptions" :show-count="true" :clearable="false" placeholder="上级检查项"
            :normalizer="normalizer" />
        </el-form-item> -->
        <el-form-item label="检查项名称" prop="inspName">
          <el-input v-model="form.inspName" placeholder="请输入检查项名称" />
        </el-form-item>
        <el-form-item label="检查项代码" prop="inspCode">
          <el-input v-model="form.inspCode" placeholder="请输入检查项代码" />
        </el-form-item>
        <el-form-item label="检查项类型" prop="inspType">
          <el-select v-model="form.inspType" placeholder="请选择检查项类型" @change="inspTypeChange" >
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DICT_INSPECT_TYPE)" :key="dict.value"
              :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="fpdType" v-if="form.inspType == 'fpd'">
          <el-select v-model="form.fpdType" placeholder="请选择检查项类型">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DICT_FIRE_DECIVE_TYPE)" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)" :key="dict.value"
              :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createInspectInfo, updateInspectInfo, deleteInspectInfo, getInspectInfo, getInspectInfoPage, exportInspectInfoExcel } from "@/api/backend/inspectInfo"
import { getInspectInfoTree, getInspectInfoNavTree } from "@/api/backend/inspectInfo";
import { DICT_TYPE, getDictDatas } from "@/utils/dict";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "InspectInfo",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检查项基础信息主列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        inspName: null,
        inspCode: null,
        inspType: null,
        status: null,
      },
      // 表单参数
      form: {
        status: 0
      },
      // 表单校验
      rules: {
        inspName: [{ required: true, message: "检查项名称不能为空", trigger: "blur" }],
        // inspCode: [{ required: true, message: "检查项代码不能为空", trigger: "blur" }],
        inspCode:[{ required: true, message: "检查项代码不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        inspType:[{ required: true, message: "请选择检查项类型", trigger: "blur" }],
      },
      // ========树相关===================
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: "inspectInfoDOList",
        label: "label"
      },
      // ============================================
      // 数据字典
      statusDictDatas: getDictDatas(DICT_TYPE.COMMON_STATUS), //状态
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      this.loading = false
      getInspectInfoPage(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
      })
      this.getTreeselect();
    },

    // ========树相关===================
    /** 查询部门下拉树结构 + 岗位下拉 */
    getTreeselect() {
      getInspectInfoNavTree({ inspType: 'dict_inspect_type' }).then(response => {
        // 处理 deptOptions 参数
        // this.deptOptions = [];
        // this.deptOptions.push(...this.handleTree(response.data, "id"));

        this.deptOptions = response.data
      });
    },
    // 格式化部门的下拉框
    normalizer(node) {
      return {
        id: node.id,
        label: node.inspName,
        children: node.childrenList
      }
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.pid = data.id;
      this.getList();
    },
    // ============================================
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        pid: undefined,
        inspName: undefined,
        inspCode: undefined,
        inspType: undefined,
        remark: undefined,
        status: 0,
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加检查项基础信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getInspectInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改检查项基础信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return
        }
        // 修改的提交
        if (this.form.id != null) {
          console.log('this.form', this.form)
          updateInspectInfo(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功")
            this.open = false
            this.getList()
          })
          return
        }
        // 添加的提交
        createInspectInfo(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功")
          this.open = false
          this.getList()
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal
      .confirm('是否确认删除名称为"' + row.inspName + '"的数据项?')
        .then(function () {
          return deleteInspectInfo(id)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        })
        .catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      // 执行导出
      this.$modal
      .confirm("是否确认导出?")
        .then(() => {
          this.exportLoading = true
          return exportInspectInfoExcel(params)
        })
        .then((response) => {
          this.$download.excel(response, "检查列表.xls")
          this.exportLoading = false
        })
        .catch(() => { })
    },
    inspTypeChange(e) {
      if(e!='fpd'){
        this.form.fpdType=null
      }
      console.log('inspTypeChange', e);
    }
  },
}
</script>

<style scoped>
::v-deep .el-scrollbar__view {
  overflow-x: auto;
}

.tree-overflow {
  height: 81vh;
  width: 30vw;
}
</style>
