<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="deptId" v-show="deptShow">
        <el-select v-model="queryParams.deptId" filterable placeholder="请选择部门" clearable>
          <el-option
            v-for="dict in allDeptList"
            :key="dict.deptId"
            :label="dict.deptName"
            :value="dict.deptId"
          />
        </el-select>
      </el-form-item>

      <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="enterpriseType">
        <el-select v-model="queryParams.enterpriseType" placeholder="请选择行业" clearable>
          <el-option
            v-for="dict in dict.type.enterprise_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="企业现状" prop="enterpriseStatus">
        <el-select v-model="queryParams.enterpriseStatus" placeholder="请选择行业" clearable>
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="社会信用代码" prop="enterpriseCode" label-width="98px">
        <el-input
          v-model="queryParams.enterpriseCode"
          placeholder="请输入社会信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="生产经营地址" prop="enterpriseAddr">
        <el-input
          v-model="queryParams.enterpriseAddr"
          placeholder="请输入生产经营地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业负责人" prop="enterpriseLeader">
        <el-input
          v-model="queryParams.enterpriseLeader"
          placeholder="请输入企业负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业负责人电话" prop="leaderTel">
        <el-input
          v-model="queryParams.leaderTel"
          placeholder="请输入企业负责人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册资本" prop="registeredCapital">
        <el-input
          v-model="queryParams.registeredCapital"
          placeholder="请输入注册资本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证照上核定的业务范围" prop="businessScope">
        <el-input
          v-model="queryParams.businessScope"
          placeholder="请输入证照上核定的业务范围"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际从事的业务范围" prop="businessProjects">
        <el-input
          v-model="queryParams.businessProjects"
          placeholder="请输入实际从事的业务范围"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分管安全副职姓名" prop="safetyManagerName">
        <el-input
          v-model="queryParams.safetyManagerName"
          placeholder="请输入分管安全副职姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分管安全副职电话" prop="safetyManagerTel">
        <el-input
          v-model="queryParams.safetyManagerTel"
          placeholder="请输入分管安全副职电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者部门" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入创建者部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者id" prop="createUserId">
        <el-input
          v-model="queryParams.createUserId"
          placeholder="请输入创建者id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改着id" prop="updateUserId">
        <el-input
          v-model="queryParams.updateUserId"
          placeholder="请输入修改着id"
          clearable
          @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"
          v-hasPermi="['ehsEnterprise:ehsEnterprise: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="['ehsEnterprise:ehsEnterprise: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="['ehsEnterprise:ehsEnterprise: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="['ehsEnterprise:ehsEnterprise:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['ehsEnterprise:ehsEnterprise:import']"
        >导入</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ehsEnterpriseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
     <!-- <el-table-column label="" align="center" prop="enterpriseId" />-->
      <el-table-column label="登记部门" align="center" prop="deptName"  v-if="this.$store.state.user.parentId==0"/>
      <el-table-column label="社会信用代码" align="center" prop="enterpriseCode" />
      <el-table-column label="名称" align="center" prop="enterpriseName" />
      <el-table-column label="行业" align="center" prop="enterpriseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.enterprise_type" :value="scope.row.enterpriseType"/>
        </template>
      </el-table-column>
      <el-table-column label="企业现状" align="center" prop="enterpriseStatus">
        <template slot-scope="scope">
          <span> {{ (scope.row.enterpriseStatus !=null && scope.row.enterpriseStatus != "") ? statusOptions[scope.row.enterpriseStatus].label:""}}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产经营地址" align="center" prop="enterpriseAddr" />
      <el-table-column label="企业负责人" align="center" prop="enterpriseLeader" />
      <el-table-column label="企业负责人电话" align="center" prop="leaderTel" />
      <el-table-column label="注册资本" align="center" prop="registeredCapital" />
      <el-table-column label="证照上核定的业务范围" align="center" prop="businessScope" />
      <el-table-column label="实际从事的业务范围" align="center" prop="businessProjects" />
      <el-table-column label="分管安全副职姓名" align="center" prop="safetyManagerName" />
      <el-table-column label="分管安全副职电话" align="center" prop="safetyManagerTel" />
      <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="['ehsEnterprise:ehsEnterprise:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ehsEnterprise:ehsEnterprise: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="form.enterpriseName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="社会信用代码" prop="enterpriseCode">
              <el-input v-model="form.enterpriseCode" placeholder="请输入社会信用代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业地址" prop="areaId">
              <el-select v-model="form.areaId" placeholder="请选择企业地址">
                <el-option
                  v-for="dict in dict.type.area_id"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
          <el-form-item label="行业" prop="enterpriseType">
            <el-select v-model="form.enterpriseType" placeholder="请选择行业">
              <el-option
                v-for="dict in dict.type.enterprise_type"
                :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="enterpriseStatus">
              <el-select v-model="form.enterpriseStatus" placeholder="请选择企业现状" clearable>
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经济类型" prop="economicType">
              <el-select v-model="form.economicType" placeholder="请选择经济类型">
                <el-option
                  v-for="dict in dict.type.economic_type"
                  :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="enterpriseScale">
              <el-select v-model="form.enterpriseScale" placeholder="请选择企业规模" clearable>
                <el-option
                  v-for="dict in dict.type.enterprise_scale"
                  :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="enterpriseAddr">
          <el-input v-model="form.enterpriseAddr" placeholder="请输入生产经营地址" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业负责人" prop="enterpriseLeader">
              <el-input v-model="form.enterpriseLeader" placeholder="请输入企业负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业负责人电话" prop="leaderTel">
              <el-input v-model="form.leaderTel" placeholder="请输入企业负责人电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="注册资本" prop="registeredCapital">
          <el-input v-model="form.registeredCapital" placeholder="请输入注册资本" />
        </el-form-item>
        <el-form-item label="证照上核定的业务范围" prop="businessScope">
          <el-input v-model="form.businessScope" placeholder="请输入证照上核定的业务范围" />
        </el-form-item>
        <el-form-item label="实际从事的业务范围" prop="businessProjects">
          <el-input v-model="form.businessProjects" placeholder="请输入实际从事的业务范围" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="分管安全副职姓名" prop="safetyManagerName">
              <el-input v-model="form.safetyManagerName" placeholder="请输入分管安全副职姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分管安全副职电话" prop="safetyManagerTel">
              <el-input v-model="form.safetyManagerTel" placeholder="请输入分管安全副职电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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="/ehsEnterprise/ehsEnterprise/importTemplate"
      temName="企业信息导入模板"
    ></importUpload>
  </div>
</template>

<script>
import { listEhsEnterprise, getEhsEnterprise, delEhsEnterprise, addEhsEnterprise, updateEhsEnterprise } from "@/api/ehs/ehsEnterprise";
import {listAllDept } from "@/api/system/dept";
import importUpload from "@/components/upload/upload";
export default {
  name: "EhsEnterprise",
  dicts: ['enterprise_type','enterprise_scale','economic_type','area_id'],
  components: {
    importUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业基本信息表格数据
      ehsEnterpriseList: [],
      //是否显示部门列表查询条件
      deptShow: false,
      //所有部门列表字典
      allDeptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseCode: null,
        enterpriseName: null,
        enterpriseType: null,
        enterpriseAddr: null,
        enterpriseLeader: null,
        leaderTel: null,
        registeredCapital: null,
        businessScope: null,
        businessProjects: null,
        enterpriseStatus: null,
        safetyManagerName: null,
        safetyManagerTel: null,
        deptId: null,
        createUserId: null,
        updateUserId: null
      },
      statusOptions: [{
        value: '0',
        label: '停业',
      }, {
        value: '1',
        label: '正常经营',
      }],
      editShow:true,//删除编辑按钮是否显示
      curDeptId:-1,//当前部门
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        enterpriseName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        enterpriseType: [
          { required: true, message: "行业不能为空", trigger: "change" }
        ],
        enterpriseStatus: [
          { required: true, message: "企业现状不能为空", trigger: "change" }
        ],
        areaId: [
          { required: true, message: "不能为空", trigger: "change" }
        ],
        economicType: [
          { required: true, message: "不能为空", trigger: "change" }
        ],
        enterpriseScale: [
          { required: true, message: "不能为空", trigger: "change" }
        ],
        leaderTel: [
          { required: true, message: "电话不能为空", trigger: "blur" },
          /*{ pattern:/^((0\d{2,3}-\d{7,8})|(1[345678]\d{9}))$/, message: "联系电话必须是手机或固话(区号-固话)", trigger: "change" }*/
        ],
        safetyManagerTel: [
          { required: true, message: "电话不能为空", trigger: "blur" },
          /*{ pattern:/^((0\d{2,3}-\d{7,8})|(1[345678]\d{9}))$/, message: "联系电话必须是手机或固话(区号-固话)", trigger: "change" }*/
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getListAllDept();
    this.deptShow = this.$store.state.user.parentId==0 ?true :false;
    this.curDeptId = this.$store.state.user.deptId;
  },
  methods: {
    /** 查询企业基本信息列表 */
    getList() {
      this.loading = true;
      listEhsEnterprise(this.queryParams).then(response => {
        this.ehsEnterpriseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 单位信息列表 */
    getListAllDept() {
      console.log(this.$store.state.user.deptId);
      this.loading = true;
      listAllDept().then(response => {
        this.allDeptList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        enterpriseId: null,
        enterpriseCode: null,
        enterpriseName: null,
        enterpriseType: null,
        enterpriseAddr: null,
        enterpriseLeader: null,
        leaderTel: null,
        registeredCapital: null,
        businessScope: null,
        businessProjects: null,
        enterpriseStatus: null,
        safetyManagerName: null,
        safetyManagerTel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        createUserId: null,
        updateUserId: null,
        areaId:null,
        economicType:null,
        enterpriseScale: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.enterpriseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const enterpriseId = row.enterpriseId || this.ids
      getEhsEnterprise(enterpriseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.enterpriseId != null) {
            updateEhsEnterprise(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEhsEnterprise(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const enterpriseIds = row.enterpriseId || this.ids;
      this.$modal.confirm('是否确认删除企业基本信息编号为"' + enterpriseIds + '"的数据项？').then(function() {
        return delEhsEnterprise(enterpriseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehsEnterprise/ehsEnterprise/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.url =
        process.env.VUE_APP_BASE_API + "/ehsEnterprise/ehsEnterprise/importData";
    },
    /** 导入成功 */
    upLoadOk() {
      // console.log("导入成功")
      this.getList();
    },
  }
};
</script>
