<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="检查记录编码" prop="fCheckCode">
        <el-input
          v-model="queryParams.fCheckCode"
          placeholder="请输入检查记录编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查对象分类" prop="fObjectType">
        <el-select v-model="queryParams.fObjectType" placeholder="请选择检查对象分类" clearable size="small">
          <el-option
            v-for="dict in fObjectTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检查人员姓名" prop="fCheckManName">
        <el-input
          v-model="queryParams.fCheckManName"
          placeholder="请输入检查人员姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="fCheckManPhone">
        <el-input
          v-model="queryParams.fCheckManPhone"
          placeholder="请输入检查人员联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="上报状态" prop="govUploadState">
        <el-select v-model="queryParams.govUploadState" placeholder="请选择上报状态">
          <el-option label="未上报" value="0"/>
          <el-option label="已上报" value="1"/>
        </el-select>
      </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="fCheckCode" width="150"/>
      <el-table-column label="任务编码" align="center" prop="fCheckTaskCode" :show-overflow-tooltip="true"/>
      <el-table-column label="任务名称" align="center" prop="taskName" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span
            @click="taskDetail(scope.row.fCheckTaskCode)"
            v-if="scope.row.taskName"
            style="color: blue; cursor: pointer;"
          >
            {{ scope.row.taskName }}
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="县级行政区" align="center" prop="countyName" width="100"/>
      <el-table-column label="检查对象分类" align="center" prop="fObjectType" width="100"/>
      <el-table-column label="对象编码" align="center" prop="fObjectCode" :show-overflow-tooltip="true"/>
      <el-table-column label="检查人员姓名" align="center" prop="fCheckManName" width="120"/>
      <el-table-column label="检查人员联系电话" align="center" prop="fCheckManPhone" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.fCheckManPhone">{{ scope.row.fCheckManPhone }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="检查时间" align="center" prop="fCheckTime" width="100"/>
      <el-table-column label="隐患数量" align="center" prop="fHazardsNum">
        <template slot-scope="scope">
          <span v-if="scope.row.fHazardsNum">{{ scope.row.fHazardsNum }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
     <!-- <el-table-column label="是否需执法" align="center" prop="fDoscNeedExecute" width="90">
        <template slot-scope="scope">
          <span v-if="scope.row.fDoscNeedExecute == '1'">是</span>
          <span v-else-if="scope.row.fDoscNeedExecute == '0'">否</span>
          <span v-else>-</span>
        </template>
      </el-table-column>-->
      <el-table-column label="上报状态" align="center" prop="govUploadState">
        <template slot-scope="scope">
          <span v-if="scope.row.govUploadState == '1'">已上报</span>
          <span v-else-if="scope.row.govUploadState == '0'">未上报</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="上报时间" align="center" prop="govUploadTime" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.govUploadTime">{{ parseTime(scope.row.govUploadTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <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>
          <el-button
            size="mini"
            type="text"
            @click="handleReport(scope.row)"
          >上报记录</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleReportHidden(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="1000px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <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%"
                @change="taskChange"
              >
                <el-option
                  v-for="task in taskData"
                  :key="task.fInsTaskInforId"
                  :label="task.fName + ' + ' +task.fUniqueCode"
                  :value="task.fUniqueCode"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="任务名称">
              <el-input disabled v-model="taskName" placeholder="任务名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="县级行政区" prop="fRegionCode">
              <el-select
                v-model="form.fRegionCode"
                placeholder="请选择县级行政区"
                style="width: 100%"
              >
                <el-option
                  v-for="county in countyInfo"
                  :key="county.fId"
                  :label="county.fName.trim()"
                  :value="county.fCountyCode"
                />
              </el-select>
            </el-form-item>
          </el-col>

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

          <el-col :span="12">
            <el-form-item label="对象编码" prop="fObjectCode">
              <el-input disabled v-model="form.fObjectCode" placeholder="请输入对象编码" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检查人员" prop="fCheckUserId">
              <el-select
                v-model="form.fCheckUserId"
                placeholder="请选择检查人员"
                style="width: 100%"
                @change="inspectorChange"
              >
                <el-option
                  v-for="inspect in inspector"
                  :key="inspect.userId"
                  :label="inspect.nickName"
                  :value="''+inspect.userId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item
              :rules="{pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,message: '手机号格式有误！',trigger: 'blur'}"
              label="检查人员联系电话"
              prop="fCheckManPhone"
            >
              <el-input v-model="form.fCheckManPhone" placeholder="请输入检查人员联系电话"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检查时间" prop="fCheckTime">
              <el-date-picker
                style="width: 100%"
                v-model="form.fCheckTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择检查时间">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="隐患数量" prop="fHazardsNum">
              <el-input disabled v-model="form.fHazardsNum" type="number" placeholder="请输入隐患数量" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否需执法" prop="fDoscNeedExecute">
              <el-select
                v-model="form.fDoscNeedExecute"
                placeholder="请选择是否需执法"
                style="width: 100%"
              >
                <el-option label="否" value="0"/>
                <el-option label="是" value="1"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="检查单照片">
              <imageUpload v-model="form.fAnnex"/>
            </el-form-item>
          </el-col>  
        </el-row>

        <!-- 隐患管理 -->
        <Rectification :fCheckCode="form.fCheckCode" v-if="open"/>

      </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"/>

    <!-- 任务详情 -->
    <TaskInfo ref="task"/>
  </div>
</template>

<script>
import { listInfor, getInfor, delInfor, addInfor, updateInfor, exportInfor, reportRecInfo } from "@/api/supervision/record";
import ImageUpload from '@/components/ImageUpload';
import { taskList } from "@/api/supervision/task";
import DetailInfo from "./components/DetailInfo";
import { getDefaultCountyList } from "@/api/area/county";
import { getInspectionUsers } from "@/api/system/user";
import TaskInfo from "./components/TaskInfo";
import Rectification from "./components/Rectification";
import { reportHazRefInfoByCheckCode } from "@/api/supervision/rectification";
export default {
  name: "Infor",
  components: {
    ImageUpload,
    DetailInfo,
    TaskInfo,
    Rectification
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检查记录表格数据
      inforList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 检查对象分类
      fObjectTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fCheckCode: null,
        fObjectType: null,
        fCheckManName: null,
        fCheckManPhone: null,
        govUploadState: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
       /* fCheckCode: [
          { required: true, message: "检查记录编码", trigger: "blur" }
        ],*/
        fCheckTaskCode: [
          { required: true, message: "关联的检查任务编码不能为空", trigger: "blur" }
        ],
        fRegionCode: [
          { required: true, message: "县级行政区ID不能为空", trigger: "blur" }
        ],
        fObjectType: [
          { required: true, message: "检查对象分类", trigger: "change" }
        ],
        fObjectCode: [
          { required: true, message: "对象编码不能为空", trigger: "blur" }
        ],
        fCheckUserId: [
          { required: true, message: "请选择检查人员", trigger: "change" }
        ],
        fCheckTime: [
          { required: true, message: "检查时间不能为空", trigger: "blur" }
        ],
      },
      taskData: [],
      fCheckCodes: [],
      taskName: "",
      countyInfo: [],
      inspector: []
    };
  },
  created() {
    this.getList();
    this.getDicts("t_type_code").then(response => {
      this.fObjectTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询检查记录列表 */
    getList() {
      this.loading = true;
      listInfor(this.queryParams).then(response => {
        this.inforList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 检查对象分类
    fObjectTypeFormat(row, column) {
      return this.selectDictLabel(this.fObjectTypeOptions, row.fObjectType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fInsRecInforId: null,
        fCheckCode: null,
        fCheckTaskCode: null,
        fRegionCode: null,
        fObjectType: null,
        fObjectCode: null,
        fCheckUserId: null,
        fCheckManName: null,
        fCheckManPhone: null,
        fCheckTime: null,
        fHazardsNum: null,
        fDoscNeedExecute: null,
        fAnnex: null,
        fLastUpdateTime: null,
        fRemark: null
      };
      this.resetForm("form");
      this.taskName = "";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fInsRecInforId);
      this.fCheckCodes = selection.map(item => item.fCheckCode);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTaskList();
      this.getCountyInfo();
      this.getInspector();
      this.open = true;
      this.title = "添加检查记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fInsRecInforId = row.fInsRecInforId || this.ids
      getInfor(fInsRecInforId).then(response => {
        this.form = response.data;
        this.getTaskList();
        this.getCountyInfo();
        this.getInspector();
        this.open = true;
        this.title = "修改检查记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fInsRecInforId != 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 fInsRecInforIds = row.fInsRecInforId || this.ids;
      const fCheckCodes = row.fCheckCode || this.fCheckCodes;
      this.$confirm('是否确认删除检查记录编码为"' + fCheckCodes + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfor(fInsRecInforIds);
        }).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;
        }
      })
    },
    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.fInsRecInforId);
    },
    //上报
    handleReport(row) {
      this.$confirm('是否确认上报检查记录编码为"' + row.fCheckCode + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return reportRecInfo({fInsRecInforId : row.fInsRecInforId});
      }).then(() => {
        this.getList();
        this.msgSuccess("上报成功");
      }).catch(() => {});
    },
    //上报隐患
    handleReportHidden(row){
      this.$confirm('是否确认上报检查记录编码为"' + row.fCheckCode + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return reportHazRefInfoByCheckCode({checkCode : row.fCheckCode});
      }).then(() => {
        this.getList();
        this.msgSuccess("上报隐患成功");
      }).catch(() => {});
    },
    taskChange(val){
      const selectTask = this.taskData.find(item => item.fUniqueCode == val);
      this.taskName = selectTask.fName;
    },
    //获取县级
    getCountyInfo(){
      getDefaultCountyList().then(res =>{
        if(res.code == 200 && res.data){
          this.countyInfo =  res.data;
        }
      })
    },
    objectTypeChange(val){
      this.form.fObjectCode = this.fObjectTypeOptions.find(item => item.dictLabel == val).dictValue;
    },
    //获取检查人信息
    getInspector(){
      getInspectionUsers(-2,null).then(res =>{
        if(res.code == 200 && res.data){
          this.inspector = res.data;
        }
      })
    },
    inspectorChange(val){
      this.form.fCheckManName = this.inspector.find(item => item.userId == val).nickName;
    },
    taskDetail(fCheckTaskCode){
      this.$refs.task.getTaskInfo(fCheckTaskCode);
    }
  }
};
</script>
