<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="检查项名称" prop="fCheckTimeName">
        <el-input
          v-model="queryParams.fCheckTimeName"
          placeholder="请输入检查项名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @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"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inforList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="检查项任务编码" align="center" prop="fCheckTaskCode" />
      <el-table-column label="检查项唯一编码" align="center" prop="fUniqueCode" />
      <el-table-column label="检查项名称" align="center" prop="fCheckTimeName" />
      <el-table-column label="检查对象分类" align="center" prop="fCheckClassification" :formatter="fTypeCodeFormat" :show-overflow-tooltip="true" />
      <el-table-column label="检查方法" align="center" prop="fCheckMethod" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.fCheckMethod">{{ scope.row.fCheckMethod }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="检查内容" align="center" prop="fCheckCount" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.fCheckCount">{{ scope.row.fCheckCount }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患级别" align="center" prop="fHazardLevel">
        <template slot-scope="scope">
          <span v-if="scope.row.fHazardLevel">{{ scope.row.fHazardLevel }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="启用状态" align="center" prop="fEnableState" :formatter="fEnableStateFormat" />
      <el-table-column label="检查依据" align="center" prop="fCheckBasis" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.fCheckBasis">{{ scope.row.fCheckBasis }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患分类分级标准" align="center" prop="hazardName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检查项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row class="el-row-table">
          <el-col :span="12">
            <el-form-item label="检查项任务编码" prop="fCheckTaskCode">
              <el-select
                v-model="form.fCheckTaskCode"
                placeholder="请选择任务编码"
                style="width: 100%"
              >
                <el-option
                  v-for="task in taskData"
                  :key="task.fInsTaskInforId"
                  :label="task.fUniqueCode"
                  :value="task.fUniqueCode"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检查项唯一编码" prop="fUniqueCode">
              <el-input v-model="form.fUniqueCode" placeholder="请输入检查项唯一编码" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检查项名称" prop="fCheckTimeName">
              <el-input v-model="form.fCheckTimeName" placeholder="请输入检查项名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检查对象分类" prop="fCheckClassification">
              <el-select
                v-model="form.fCheckClassification"
                placeholder="请选择检查对象分类"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in fTypeCodeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检查方法" prop="fCheckMethod">
              <el-input v-model="form.fCheckMethod" placeholder="请输入检查方法" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检查内容" prop="fCheckCount">
              <el-input v-model="form.fCheckCount" placeholder="请输入检查内容" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="隐患级别" prop="fHazardLevel">
              <el-input v-model="form.fHazardLevel" placeholder="请输入隐患级别" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="启用状态" prop="fEnableState">
              <el-select
                v-model="form.fEnableState"
                placeholder="请选择启用状态"
                style="width:100%;"
              >
                <el-option
                  v-for="dict in fEnableStateOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检查依据" prop="fCheckBasis">
              <el-input v-model="form.fCheckBasis" placeholder="请输入检查依据" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="隐患分类分级标准" prop="fHazardTypeLevelId">
              <el-select
                v-model="form.fHazardTypeLevelId"
                placeholder="请选择隐患分类分级标准"
                style="width: 100%"
              >
                <el-option
                  v-for="hidden in hiddenData"
                  :key="hidden.fInsStaInforId"
                  :label="hidden.fHazardName"
                  :value="hidden.fInsStaInforId"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="删除标记" prop="fDeleteFlag">
              <el-select
                v-model="form.fDeleteFlag"
                placeholder="请选择删除标记"
                style="width:100%;"
              >
                <el-option
                  v-for="dict in fDeleteFlagOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </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>

    <!-- 详情 -->
    <DetailInfo ref="detail"/>
  </div>
</template>

<script>
import { listInfor, getInfor, delInfor, addInfor, updateInfor, exportInfor } from "@/api/supervision/inspect";
import { taskList } from "@/api/supervision/task";
import { hiddenList } from "@/api/supervision/hideType";
import DetailInfo from "./components/DetailInfo";
export default {
  name: "Infor",
  components: {
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      fCheckTimeNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检查项表格数据
      inforList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 启用状态,0-启用,1-禁用字典
      fEnableStateOptions: [],
      // 删除标记,0-可用,1-已删除字典
      fDeleteFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fCheckTimeName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fCheckTaskCode: [
          { required: true, message: "请选择检查项任务编码", trigger: "change" }
        ],
        fUniqueCode: [
          { required: true, message: "检查项唯一编码不能为空", trigger: "blur" }
        ],
        fCheckTimeName: [
          { required: true, message: "检查项名称不能为空", trigger: "blur" }
        ],
        fHazardTypeLevelId: [
          { required: true, message: "请选择隐患分类分级标准", trigger: "change" }
        ],
      },
      taskData: [],
      hiddenData: [],
      // 检查对象分类
      fTypeCodeOptions: [],
    };
  },
  created() {
    this.getList();
    this.getDicts("t_enable_state").then(response => {
      this.fEnableStateOptions = response.data;
    });
    this.getDicts("t_delete_flag").then(response => {
      this.fDeleteFlagOptions = response.data;
    });
    this.getDicts("t_type_code").then(response => {
        this.fTypeCodeOptions = response.data;
    });
  },
  methods: {
    /** 查询检查项列表 */
    getList() {
      this.loading = true;
      listInfor(this.queryParams).then(response => {
        this.inforList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 启用状态,0-启用,1-禁用字典翻译
    fEnableStateFormat(row, column) {
      return this.selectDictLabel(this.fEnableStateOptions, row.fEnableState);
    },
    // 删除标记,0-可用,1-已删除字典翻译
    fDeleteFlagFormat(row, column) {
      return this.selectDictLabel(this.fDeleteFlagOptions, row.fDeleteFlag);
    },
    // 检查对象分类
    fTypeCodeFormat(row, column) {
      const type = this.selectDictLabel(this.fTypeCodeOptions, row.fCheckClassification);
      return type ? type : "-";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fInsInsListInforId: null,
        fCheckTaskCode: null,
        fUniqueCode: null,
        fCheckTimeName: null,
        fCheckClassification: null,
        fCheckMethod: null,
        fCheckCount: null,
        fHazardLevel: null,
        fEnableState: null,
        fCheckBasis: null,
        fHazardTypeLevelId: null,
        fCreateBy: null,
        fCreateTime: null,
        fUpdateBy: null,
        fUpdateTime: null,
        fDeleteFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fInsInsListInforId);
      this.fCheckTimeNames = selection.map(item => item.fCheckTimeName);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTaskList();
      this.getHiddenList();
      this.open = true;
      this.title = "添加检查项";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fInsInsListInforId = row.fInsInsListInforId || this.ids
      getInfor(fInsInsListInforId).then(response => {
        this.form = response.data;
        this.getTaskList();
        this.getHiddenList();
        this.open = true;
        this.title = "修改检查项";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fInsInsListInforId != null) {
            updateInfor(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfor(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fInsInsListInforIds = row.fInsInsListInforId || this.ids;
      const fCheckTimeNames = row.fCheckTimeName || this.fCheckTimeNames;
      this.$confirm('是否确认删除检查项名称为"' + fCheckTimeNames + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfor(fInsInsListInforIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有检查项数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInfor(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    //任务列表
    getTaskList(){
      taskList().then(res =>{
        if(res.code == 200 && res.data){
          this.taskData = res.data;
        }
      })
    },
    //所有隐患
    getHiddenList(){
      hiddenList().then(res =>{
        if(res.code == 200 && res.data){
          this.hiddenData = res.data;
        }
      })
    },
    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.fInsInsListInforId);
    },
  }
};
</script>