<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单位名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="responsiblePerson">
        <el-input
          v-model="queryParams.responsiblePerson"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存在隐患" prop="risk">
        <el-input
          v-model="queryParams.risk"
          placeholder="请输入存在隐患"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否重大隐患" prop="isLargeRisk" label-width="100px">
        <el-select v-model="queryParams.isLargeRisk" placeholder="请选择是否重大隐患" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="整改措施" prop="rectificationMeasure">
        <el-input
          v-model="queryParams.rectificationMeasure"
          placeholder="请输入整改措施"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="整改责任人" prop="rectificationPeople" label-width="89px">
        <el-input
          v-model="queryParams.rectificationPeople"
          placeholder="请输入整改责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业类别" prop="enterpriseCategory">
        <el-select v-model="queryParams.enterpriseCategory" placeholder="请选择企业类别" clearable>
          <el-option
            v-for="dict in dict.type.enterprise_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否建立双预防" prop="isEstablishPrevention" label-width="110px">
        <el-select v-model="queryParams.isEstablishPrevention" placeholder="请选择是否建立双预防" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="时间" prop="time">
        <el-date-picker clearable
          v-model="queryParams.time"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择时间">
        </el-date-picker>
      </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"
          v-hasPermi="['ehs:ehsLawEnforce:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ehs:ehsLawEnforce:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ehs:ehsLawEnforce:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ehs:ehsLawEnforce:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >批量导入</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ehsLawEnforceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="单位名称" align="center" prop="enterpriseName" />
      <el-table-column label="地址" align="center" prop="location" />
      <el-table-column label="负责人" align="center" prop="responsiblePerson" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="存在隐患" align="center" prop="risk" />
      <el-table-column label="是否重大隐患" align="center" prop="isLargeRisk">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isLargeRisk"/>
        </template>
      </el-table-column>
      <el-table-column label="整改措施" align="center" prop="rectificationMeasure" />
      <el-table-column label="整改责任人" align="center" prop="rectificationPeople" />
      <el-table-column label="企业类别" align="center" prop="enterpriseCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.enterprise_category" :value="scope.row.enterpriseCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="企业人数" align="center" prop="employeeNum" />
      <el-table-column label="是否建立双预防" align="center" prop="isEstablishPrevention">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isEstablishPrevention"/>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="其他" align="center" prop="other" />
      <el-table-column label="备注" align="center" prop="remark" />
      <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)"
            v-hasPermi="['ehs:ehsLawEnforce:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ehs:ehsLawEnforce:remove']"
          >删除</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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单位名称" prop="enterpriseName">
          <el-input v-model="form.enterpriseName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="地址" prop="location">
          <el-input v-model="form.location" placeholder="请输入地址" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="负责人" prop="responsiblePerson">
              <el-input v-model="form.responsiblePerson" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="存在隐患" prop="risk">
          <el-input v-model="form.risk" placeholder="请输入存在隐患" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否重大隐患" prop="isLargeRisk" label-width="100px">
              <el-select v-model="form.isLargeRisk" placeholder="请选择是否重大隐患">
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否建立双预防" prop="isEstablishPrevention" label-width="110px">
              <el-select v-model="form.isEstablishPrevention" placeholder="请选择是否建立双预防">
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="整改措施" prop="rectificationMeasure">
          <el-input v-model="form.rectificationMeasure" placeholder="请输入整改措施" />
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="企业类别" prop="enterpriseCategory">
              <el-select v-model="form.enterpriseCategory" placeholder="请选择企业类别">
                <el-option
                  v-for="dict in dict.type.enterprise_category"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业人数" prop="employeeNum">
              <el-input v-model="form.employeeNum" placeholder="请输入企业人数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="时间" prop="time"  label-width="60px">
              <el-date-picker clearable
                v-model="form.time"
                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="rectificationPeople"  label-width="90px">
              <el-input v-model="form.rectificationPeople" placeholder="请输入整改责任人" />
            </el-form-item>

          </el-col>
        </el-row>
        <el-form-item label="其他" prop="other">
          <el-input v-model="form.other" placeholder="请输入其他" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </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>

    <!-- 导入 -->
    <importUpload
      ref="uploadRef"
      @ok="upLoadOk"
      downUrl="/ehs/ehsLawEnforce/importTemplate"
      temName="行政执法台账导入模板"
    ></importUpload>
  </div>
</template>

<script>
import { listEhsLawEnforce, getEhsLawEnforce, delEhsLawEnforce, addEhsLawEnforce, updateEhsLawEnforce } from "@/api/ehs/ehsLawEnforce";
import importUpload from "@/components/upload/upload";
export default {
  name: "EhsLawEnforce",
  dicts: ['sys_yes_no', 'enterprise_type', 'enterprise_category'],
  components: {
    importUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      lawEnforceClass: 0,
      // 行政执法台账表格数据
      ehsLawEnforceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
        location: null,
        responsiblePerson: null,
        phone: null,
        risk: null,
        isLargeRisk: null,
        rectificationMeasure: null,
        rectificationPeople: null,
        enterpriseCategory: null,
        employeeNum: null,
        isEstablishPrevention: null,
        time: null,
        other: null,
        classFirst: null,
        classFirst:this.$route.query.classFirst,
      },
      // 表单参数
      form: {
        classFirst:this.$route.query.classFirst,
      },
      // 表单校验
      rules: {
        enterpriseName: [
          { required: true, message: "单位名称不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "电话不能为空", trigger: "blur" },
          /*{ pattern:/^((0\d{2,3}-\d{7,8})|(1[345678]\d{9}))$/, message: "联系电话必须是手机或固话(区号-固话)", trigger: "change" }*/
        ],
      }
    };
  },
  created() {
    this.lawEnforceClass = this.$route.query.classFirst;
    this.getList();
  },
  methods: {
    /** 查询行政执法台账列表 */
    getList() {
      this.loading = true;
      listEhsLawEnforce(this.queryParams).then(response => {
        this.ehsLawEnforceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseName: null,
        location: null,
        responsiblePerson: null,
        phone: null,
        risk: null,
        isLargeRisk: null,
        rectificationMeasure: null,
        rectificationPeople: null,
        enterpriseCategory: null,
        employeeNum: null,
        isEstablishPrevention: null,
        time: null,
        other: null,
        classFirst: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        createUserId: null,
        updateUserId: null,
        classFirst:this.$route.query.classFirst,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加行政执法台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEhsLawEnforce(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改行政执法台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEhsLawEnforce(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEhsLawEnforce(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除行政执法台账编号为"' + ids + '"的数据项？').then(function() {
        return delEhsLawEnforce(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehs/ehsLawEnforce/export', {
        ...this.queryParams
      }, `行政执法台账_${new Date().getTime()}.xlsx`)
    },
    /** 批量导入 */
    handleImport() {
      console.log(this.$refs.uploadRef.upload);
      this.$refs.uploadRef.upload.open = true;
      this.$refs.uploadRef.upload.title = "行政执法台账导入";
      this.$refs.uploadRef.upload.params = this.$route.query.classFirst;
      this.$refs.uploadRef.upload.url =
        process.env.VUE_APP_BASE_API + "/ehs/ehsLawEnforce/importData";
    },
    /** 导入成功 */
    upLoadOk() {
      // console.log("导入成功")
      this.getList();
    },
  }
};
</script>
