<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="专管员" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入专管员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          clearable
          placeholder="请选择所属区域"
          @change="handleQuery"
          style="width: 150px"
        >
          <el-option
            v-for="dict in enterprise_type"
            :key="dict.value"
            :lable="dict.value"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属区域" prop="region">
        <el-select
          v-model="queryParams.region"
          clearable
          placeholder="请选择所属区域"
          @select="handleQuery"
          style="width: 150px"
        >
          <el-option
            v-for="dict in sys_region"
            :key="dict.value"
            :lable="dict.value"
            :value="dict.label"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="企业名称" prop="enterprisesName">
        <el-input
          v-model="queryParams.enterprisesName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="环保负责人" prop="environmentalContactName">
        <el-input
          v-model="queryParams.environmentalContactName"
          placeholder="请输入企业环保负责人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="注册号" prop="registrationNumber">
        <el-input
          v-model="queryParams.registrationNumber"
          placeholder="请输入企业注册号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="创建人" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入创建人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @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="Plus"
          @click="handleAdd"
          v-hasPermi="['system:enterprises:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:enterprises:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="enterprisesList">
      <el-table-column
        label="名称"
        align="center"
        prop="enterprisesName"
        show-overflow-tooltip
      >
        <template #default="scope">
          <section class="name">
            <DictTag :options="enterprise_type" :value="scope.row.region" />
            <span>{{ scope.row.enterprisesName }}</span>
          </section>
        </template>
      </el-table-column>
      <el-table-column label="所属区域" align="center" prop="region">
        <template #default="scope">
          <DictTag :options="sys_region" :value="scope.row.region" />
        </template>
      </el-table-column>
      <el-table-column
        label="地址"
        align="center"
        prop="address"
        show-overflow-tooltip
      />
      <el-table-column label="环保负责人" align="center" prop="" />
      <el-table-column
        label="专管员"
        align="center"
        prop="environmentalContactName"
      />
      <el-table-column
        label="注册号"
        align="center"
        prop="registrationNumber"
      />
      <el-table-column
        label="成立时间"
        align="center"
        prop="establishmentDate"
        width="180"
      >
        <template #default="scope">
          <span>
            {{ parseTime(scope.row.establishmentDate, "{y}-{m}-{d}") }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="300px"
      >
        <template #default="scope">
          <el-button link type="primary" icon="Collection"> 详情 </el-button>
          <el-button
            link
            type="primary"
            icon="Document"
            @click="introduce(scope.row)"
            v-hasPermi="['system:enterprises:remove']"
          >
            图文介绍
          </el-button>
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:enterprises:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <EnterprisesFrom ref="enterprisesFromRef" @success="handleQuery" />
    <IntroduceForm ref="introduceFormRef" />
  </div>
</template>

<script setup name="Enterprises">
import EnterprisesFrom from "./component/form.vue";
import IntroduceForm from "./component/introduce.vue";
import {
  listEnterprises,
  delEnterprises,
} from "@/api/system/enterprises/enterprises";
import DictTag from "@/components/DictTag/index.vue";
const { proxy } = getCurrentInstance();
const { enterprise_type, sys_region } = proxy.useDict(
  "enterprise_type",
  "sys_region",
);
const enterprisesList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const enterprisesFromRef = ref();
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    departmentId: null,
    userId: null,
    type: null,
    region: null,
    enterprisesName: null,
    address: null,
    environmentalContactName: null,
    environmentalContactPhone: null,
    registrationNumber: null,
    introduction: null,
    establishmentDate: null,
    creator: null,
    updater: null,
    deleted: null,
  },
});
const { queryParams } = toRefs(data);
const introduceFormRef = ref();
/** 查询企业列表 */
function getList() {
  loading.value = true;
  listEnterprises(queryParams.value).then((response) => {
    enterprisesList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  unref(enterprisesFromRef).open();
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除企业编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delEnterprises(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/enterprises/export",
    {
      ...queryParams.value,
    },
    `enterprises_${new Date().getTime()}.xlsx`,
  );
}


/** 图文介绍按钮操作 */
function  introduce(row){
	unref(introduceFormRef).open(row.id);
}
getList();
</script>

<style lang="scss" scoped>
.name {
  display: flex;
  width: fit-content;
  gap: 10px;
}
</style>
