<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="隐患名称" prop="fHazardName">
        <el-input
          v-model="queryParams.fHazardName"
          placeholder="请输入隐患名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="隐患编码" prop="fHazardCode">
        <el-input
          v-model="queryParams.fHazardCode"
          placeholder="请输入隐患编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="隐患类别" prop="fHazardCategory">
        <el-select v-model="queryParams.fHazardCategory" placeholder="请选择隐患类别" clearable size="small">
          <el-option
            v-for="dict in fHazardCategoryOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
     <!-- <el-form-item label="隐患等级" prop="fHazardLevel">
        <el-input
          v-model="queryParams.fHazardLevel"
          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>
      <el-col :span="1.5">
        <el-button
          plain
          size="mini"
          type="info"
          @click="getGovernmentHiddenInfo"
        >从省平台获取数据</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="fHazardName" :show-overflow-tooltip="true"/>
      <el-table-column label="隐患编码" align="center" prop="fHazardCode" :show-overflow-tooltip="true"/>
      <el-table-column label="隐患描述" align="center" prop="fHazardDescribe" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.fHazardDescribe">{{ scope.row.fHazardDescribe }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="对象分类" align="center" prop="fObjectType">
        <template slot-scope="scope">
          <span v-if="scope.row.fObjectType">{{ scope.row.fObjectType }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患类别" align="center" prop="fHazardCategory" :formatter="fHazardCategoryFormat"/>
      <el-table-column label="隐患环节/部位" align="center" prop="fHazardLink">
        <template slot-scope="scope">
          <span v-if="scope.row.fHazardLink">{{ scope.row.fHazardLink }}</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="fHazardDefinitionSourceCode">
        <template slot-scope="scope">
          <span v-if="scope.row.fHazardDefinitionSourceCode">{{ scope.row.fHazardDefinitionSourceCode }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <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="120px">
        <el-row class="el-row-table">
          <el-col :span="12">
            <el-form-item label="隐患名称" prop="fHazardName">
              <el-input v-model="form.fHazardName" placeholder="请输入隐患名称" />
            </el-form-item>
          </el-col>

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

          <el-col :span="24">
            <el-form-item label="隐患描述" prop="fHazardDescribe">
              <el-input type="textarea" v-model="form.fHazardDescribe" placeholder="请输入隐患描述" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="对象分类" prop="fObjectType">
              <el-input v-model="form.fObjectType" placeholder="请输入对象分类" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="隐患类别" prop="fHazardCategory">
              <el-select
                v-model="form.fHazardCategory"
                placeholder="请选择隐患类别"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in fHazardCategoryOptions"
                  :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="fHazardLink">
              <el-input v-model="form.fHazardLink" 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="fHazardDefinitionSourceCode">
              <el-input v-model="form.fHazardDefinitionSourceCode" placeholder="请输入隐患定义来源" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="整改责任归属" prop="fOwnerResponse">
              <el-input v-model="form.fOwnerResponse" placeholder="请输入整改责任归属" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="主管部门关联" prop="fAdministrativeSysOrganizationId">
              <el-input v-model="form.fAdministrativeSysOrganizationId" placeholder="请输入主管部门关联" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="主管部门名称" prop="fAdministrativeDepartmentName">
              <el-input v-model="form.fAdministrativeDepartmentName" placeholder="请输入主管部门名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否强制执法" prop="fIsEnforcement">
              <el-select
                v-model="form.fIsEnforcement"
                placeholder="请选择是否强制执法"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in fIsEnforcementOptions"
                  :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="执法部门id" prop="fEnforcementSysOrganizationId">
              <el-input v-model="form.fEnforcementSysOrganizationId" placeholder="请输入执法部门id" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="执法部门名称" prop="fEnforcementDepartmentName">
              <el-input v-model="form.fEnforcementDepartmentName" placeholder="请输入执法部门名称冗余" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="整改监督部门" prop="fSupervisionSysOrganizationId">
              <el-input v-model="form.fSupervisionSysOrganizationId" placeholder="请输入整改监督部门" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="整改部门名称" prop="fSupervisionDepartmentName">
              <el-input v-model="form.fSupervisionDepartmentName" placeholder="请输入整改部门名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="整改要求" prop="fRectRequire">
              <el-input v-model="form.fRectRequire" placeholder="请输入整改要求" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="整改期限" prop="fRectDeadline">
              <el-input v-model="form.fRectDeadline" placeholder="请输入整改期限" />
            </el-form-item>
          </el-col>

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

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

          <el-col :span="12">
            <el-form-item label="检查部门" prop="fCheckDept">
              <el-input v-model="form.fCheckDept" placeholder="请输入检查部门" />
            </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="fEnforcementAccording">
              <el-input v-model="form.fEnforcementAccording" placeholder="请输入执法参考依据" />
            </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/hideType";
import DetailInfo from "./components/DetailInfo";
import { getStandard } from "@/api/govermentdata/GovermentData";
import moment from "moment";
export default {
  name: "Infor",
  components: {
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      fHazardNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 隐患分类分级标准表格数据
      inforList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 隐患类别
      fHazardCategoryOptions: [],
      // 是否强制执法，Y-是，N-否字典
      fIsEnforcementOptions: [],
      // 删除标记0-可用，1-已删除字典
      fDeleteFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fHazardName: null,
        fHazardCode: null,
        fHazardCategory: null,
        fHazardLevel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fHazardName: [
          { required: true, message: "隐患名称不能为空", trigger: "blur" }
        ],
        fHazardCode: [
          { required: true, message: "隐患编码不能为空", trigger: "blur" }
        ],
        fHazardCategory: [
          { required: true, message: "请选择隐患类别", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_hazard_category").then(response => {
      this.fHazardCategoryOptions = response.data;
    });
    this.getDicts("t_is_enforcement").then(response => {
      this.fIsEnforcementOptions = response.data;
    });
    this.getDicts("t_delete_flag").then(response => {
      this.fDeleteFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询隐患分类分级标准列表 */
    getList() {
      this.loading = true;
      listInfor(this.queryParams).then(response => {
        this.inforList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 隐患类别
    fHazardCategoryFormat(row, column) {
      return this.selectDictLabel(this.fHazardCategoryOptions, row.fHazardCategory);
    },
    // 是否强制执法，Y-是，N-否字典翻译
    fIsEnforcementFormat(row, column) {
      return this.selectDictLabel(this.fIsEnforcementOptions, row.fIsEnforcement);
    },
    // 删除标记0-可用，1-已删除字典翻译
    fDeleteFlagFormat(row, column) {
      return this.selectDictLabel(this.fDeleteFlagOptions, row.fDeleteFlag);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fInsStaInforId: null,
        fHazardName: null,
        fHazardCode: null,
        fHazardDescribe: null,
        fObjectType: null,
        fHazardCategory: null,
        fHazardLink: null,
        fHazardLevel: null,
        fHazardDefinitionSourceCode: null,
        fOwnerResponse: null,
        fAdministrativeSysOrganizationId: null,
        fAdministrativeDepartmentName: null,
        fIsEnforcement: null,
        fEnforcementSysOrganizationId: null,
        fEnforcementDepartmentName: null,
        fSupervisionSysOrganizationId: null,
        fSupervisionDepartmentName: null,
        fRectRequire: null,
        fRectDeadline: null,
        fHazardCheckBasis: null,
        fCheckItemId: null,
        fCheckDept: null,
        fCheckMethod: null,
        fEnforcementAccording: 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.fInsStaInforId);
      this.fHazardNames = selection.map(item => item.fHazardName);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加隐患分类分级标准";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fInsStaInforId = row.fInsStaInforId || this.ids
      getInfor(fInsStaInforId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改隐患分类分级标准";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fInsStaInforId != 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 fInsStaInforIds = row.fInsStaInforId || this.ids;
      const fHazardNames = row.fHazardName || this.fHazardNames;
      this.$confirm('是否确认删除隐患分类分级标准名称为"' + fHazardNames + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfor(fInsStaInforIds);
        }).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(() => {});
    },
    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.fInsStaInforId);
    },
    //获取数据
    getGovernmentHiddenInfo(){
      this.$confirm('是否确认从省平台获取数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return getStandard({updateTime:moment().format('YYYY-MM-DD HH:mm:ss'),pageIndex: 1,pageSize: 1000});
      }).then(() => {
        this.getList();
        this.msgSuccess("从省平台获取数据成功");
      }).catch(() => {});
    }
  }
};
</script>
