<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="监管码" prop="supervisionCode">
        <el-input
          v-model="queryParams.supervisionCode"
          placeholder="请输入监管码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="fName">
        <el-input
          v-model="queryParams.fName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任人" prop="fResPer">
        <el-input
          v-model="queryParams.fResPer"
          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="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="监管码" align="center" prop="supervisionCode" />
      <el-table-column label="名称" align="center" prop="fName" />
      <el-table-column label="责任人" align="center" prop="fResPer" />
      <el-table-column label="电话" align="center" prop="fPhone" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="分级情况" align="center" prop="fLevel" :formatter="fLevelFormat" />
      <el-table-column label="所属单位" align="center" prop="beyondEnterpriseId" :formatter="beyondEnterpriseFormat" />
      <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-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleSupervisionRecord(scope.row)"
          >监督检查记录</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-warning-outline"
            @click="handleHazardRectification(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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="fName">
              <el-input v-model="form.fName" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="监管码" prop="supervisionCode">
              <el-input v-model="form.supervisionCode" placeholder="请输入监管码" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="责任人" prop="fResPer">
              <el-input v-model="form.fResPer" placeholder="请输入责任人" />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="电话" prop="fPhone">
              <el-input v-model="form.fPhone" onkeyup="this.value=this.value.replace(/\D/g,'')"  placeholder="请输入电话" />
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="23">
            <el-form-item label="经纬度坐标" prop="longitude">
              <el-col :span="9">
                <el-input v-model="form.longitude" placeholder="请输入经度" />
              </el-col>
              <el-col :span="9" style="margin-left: 10px">
                <el-input v-model="form.latitude" placeholder="请输入纬度" />
              </el-col>
              <el-col :span="3" style="margin-left: 30px">
                <el-button type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
        <el-col :span="11">
          <el-form-item label="分级情况" prop="fLevel">
              <el-select v-model="form.fLevel" placeholder="请选择分级情况" style="width: 100%;">
                <el-option
                  v-for="dict in fLevelOptions"
                  :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="beyondEnterpriseId">
          <el-select v-model="form.beyondEnterpriseId" placeholder="请选择所属单位名称" style="width: 100%" >
            <el-option
              v-for="item in enterprises"
              :key="item.enterpriseId"
              :label="item.enterpriseName"
              :value="item.enterpriseId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        </el-row>

          <el-form-item label="安全评估信息" prop="fEvaInfor">
            <editor v-if="open" v-model="form.fEvaInfor" placeholder="请输入安全评估信息" :min-height="192" />
          </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>

    <GetPos
      :dialogVisible.sync="dialogTableVisibles"
      device=""
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
    />

    <!-- 监督检查记录对话框 -->
    <el-dialog
      :title="'监督检查记录'"
      :visible.sync="supervisionDialogVisible"
      width="70%"
      append-to-body
      @close="closeSupervisionDialog"
    >
      <sup-major-hazard
        ref="supMajorHazardRef"
        :f-hazard-id="currentFHazardId"
      />
    </el-dialog>

    <!-- 隐患及整改对话框 -->
    <el-dialog
      :title="'隐患及整改'"
      :visible.sync="hazardDialogVisible"
      width="70%"
      append-to-body
      @close="closeHazardDialog"
    >
      <dangers-rectifications
        ref="dangersRectificationsRef"
        :f-hazard-id="currentFHazardId"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord, exportRecord } from "@/api/majorpollsourcelevel/record";
import GetPos from '@/components/GetPos';
import Editor from '@/components/Editor';
import { enterpriseLists } from "@/api/regulation/info";
import { getInspectionUsers } from "@/api/system/user";
import SupMajorHazard from '../supMajorHazard/index.vue';
import DangersRectifications from '../dangersRectifications/index.vue';

export default {
  name: "Record",
  components: {
    GetPos,
    Editor,
    SupMajorHazard,
    DangersRectifications
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,

      enterprises: [],
      // 总条数
      total: 0,
      // 重大危险源监管表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分级情况字典
      fLevelOptions: [],
      // 企业单位情况字典
      beyondEnterpriseOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fName: null,
        fResPer: null,
        supervisionCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
          fName: [
            { required: true, message: "请输入名称", trigger: "blur" },
          ],
          fResPer: [
            { required: true, message: "请输入责任人", trigger: "blur" },
          ],
      },

      /**--------------地图使用数据---------------*/
      dialogTableVisibles: false,
      devicePos: [],
      // 监督检查记录对话框
      supervisionDialogVisible: false,
      // 隐患及整改对话框
      hazardDialogVisible: false,
      // 当前选中的 fHazardId
      currentFHazardId: null,
    };
  },
  created() {
    this.getList();
    this.getEnterpriseLists();
    this.getDicts("major_poll_source_level").then(response => {
      this.fLevelOptions = response.data;
    });
  },
  methods: {
    // 分级情况字典翻译
    // fLevelFormat(row, column) {
    //   return this.selectDictLabel(this.fLevelOptions, row.fLevel);
    // },
    fLevelFormat(row){
      let obj=this.fLevelOptions.find(item=>{
        return item.dictValue==row.fLevel;
      })

      if(obj!=undefined || obj!=null){
        return obj.dictLabel;
      }
    },
    beyondEnterpriseFormat(row){
      let info = this.enterprises.find(item => item.enterpriseId == row.beyondEnterpriseId);
      return info?info.enterpriseName:"-";
    },
    /** 查询重大危险源监管列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fHazardId: null,
        fName: null,
        fResPer: null,
        fPhone: null,
        fEvaInfor: null,
        longitude: null,
        latitude: null,
        fLevel: null,
        fCreateTime: null,
        fUpdateTime: null,
        fIsDel: null,
        fRemarks: null,
        beyondEnterpriseId: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.fHazardId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加重大危险源监管";
      this.getEnterpriseLists();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fHazardId = row.fHazardId || this.ids
      getRecord(fHazardId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改重大危险源监管";
      });
      this.getEnterpriseLists();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fHazardId != null) {
            updateRecord(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fHazardIds = row.fHazardId || this.ids;
      this.$confirm('是否确认删除重大危险源监管编号为"' + fHazardIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRecord(fHazardIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有重大危险源监管数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportRecord(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    /**
     * 经纬度坐标方法
     */
    MapdialogFun() {
      this.dialogTableVisibles = true;
    },
    /**
     * 地图关闭方法
     */
    dialogcancelFun() {
      this.dialogTableVisibles = false;
    },
    /**
     * 经纬度 选择
     * @param res
     */
    getPath(res){
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    },
      // getInspectionUserList(enterpriseId){
      //   getInspectionUsers(enterpriseId,"se").then(response =>{
      //     this.inspectors = response.data;
      // })
      // },
      //
      // selectworkAssignEnterprose(enterpriseId){
      //   this.getInspectionUserList(enterpriseId);
      // },
      //所属单位
      getEnterpriseLists(){
        const param = {};
        // this.judgeOperateType(param);
        enterpriseLists(param).then(response => {
          if (response.rows.length>1){
            this.enterprises = response.rows;
          }else {
            this.form.beyondEnterpriseId=response.rows[0].enterpriseId;
            this.enterprises = response.rows;
          }

      });
      },
      /** 打开监督检查记录 */
      handleSupervisionRecord(row) {
        this.currentFHazardId = row.fHazardId;
        this.supervisionDialogVisible = true;
        this.$nextTick(() => {
          if (this.$refs.supMajorHazardRef) {
            this.$refs.supMajorHazardRef.initQuery(this.currentFHazardId);
          }
        });
      },
      /** 打开隐患及整改 */
      handleHazardRectification(row) {
        this.currentFHazardId = row.fHazardId;
        this.hazardDialogVisible = true;
        this.$nextTick(() => {
          if (this.$refs.dangersRectificationsRef) {
            this.$refs.dangersRectificationsRef.initQuery(this.currentFHazardId);
          }
        });
      },
      /** 关闭监督检查记录对话框 */
      closeSupervisionDialog() {
        this.currentFHazardId = null;
      },
      /** 关闭隐患及整改对话框 */
      closeHazardDialog() {
        this.currentFHazardId = null;
      }
  }
};
</script>
