<template>
  <ContentWrap>
    <section class="flex flex-col gap-20px">
      <!-- 搜索工作栏 -->
      <el-form
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="0"
        size="large"
        class="formClass"
      >
        <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="region">
          <el-select v-model="queryParams.region" placeholder="请选择所属区域" clearable>
            <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.ENTERPRISES_AREA)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="type">
          <el-select
            v-model="queryParams.type"
            placeholder="请选择行业类型"
            clearable
            multiple
            collapse-tags
            collapse-tags-tooltip
          >
            <el-option
              v-for="dict in typeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </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="establishmentDate">
          <el-date-picker
            v-model="queryParams.establishmentDate"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            format="YYYY年M月D日"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          />
        </el-form-item>

        <el-form-item class="btnClass">
          <el-button @click="handleQuery" type="primary" plain>
            <Icon icon="ep:search" class="mr-5px" /> 查询
          </el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            v-hasPermi="['system:enterprise:create']"
            @click="push({ path: 'update' })"
          >
            <Icon
              icon="ep:circle-plus"
              class="mr-5px"
              @click="
                push({
                  path: 'update'
                })
              "
            />
            新增
          </el-button>
          <el-button
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['system:enterprise:export']"
          >
            <Icon icon="ep:download" class="mr-5px" /> 导出
          </el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="企业名称" fixed="left" prop="enterprisesName" />
        <el-table-column label="所属区域" width="200">
          <template #default="scope">
            {{ getDictLabel(DICT_TYPE.ENTERPRISES_AREA, scope.row.region) }}
          </template>
        </el-table-column>
        <el-table-column label="行业类别" width="300">
          <template #default="scope">
            {{ getDictLabel(DICT_TYPE.ENTERPRISES_TYPE, scope.row.type) }}
          </template>
        </el-table-column>
        <el-table-column label="执法人员" width="200">
          <template #default="scope">
	          {{`${scope.row.inviterName} | ${scope.row.inviterNameDept}`}}
          </template>
        </el-table-column>
        <el-table-column
          label="入驻时间"
          prop="createTime"
          :formatter="dateFormatter"
          width="240"
        />
        <el-table-column label="操作选项" align="center" width="100px">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="
                push({
                  path: 'update',
                  query: {
                    id: scope.row.id
                  }
                })
              "
              v-hasPermi="['system:enterprise:update']"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :total="total"
        :show-page-size="false"
        layout="total, prev, pager, next"
        v-model:current-page="queryParams.pageNo"
        v-model:page-size="queryParams.pageSize"
        @change="getList"
        class="ml-auto"
      />
    </section>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE, getDictLabel } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { EnterprisesApi, EnterprisesVO } from '@/api/enterprises'
import { TagLibraryApi } from '@/api/system/taglibrary'
/** 企业 列表 */
defineOptions({ name: 'Enterprises' })

const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const list = ref<EnterprisesVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  departmentId: undefined,
  userId: undefined,
  type: undefined,
  region: undefined,
  enterprisesName: undefined,
  address: undefined,
  enterprisesStatus: undefined,
  contactName: undefined,
  environmentalContactPhone: undefined,
  registrationNumber: undefined,
  introduction: undefined,
  establishmentDate: [],
  gpsLocation: undefined,
  createTime: [],
  managerDeptId: undefined
})
//行业类别
const typeList = ref([])
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const { push } = useRouter() // 路由

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EnterprisesApi.getEnterprisesPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await EnterprisesApi.exportEnterprises(queryParams)
    download.excel(data, '企业.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const getTypList = async () => {
  const res = await TagLibraryApi.tagLibraryList('hy')
  typeList.value = res[0].children.map((t) => {
    return {
      label: t.tagName,
      value: t.id
    }
  })
}

getTypList()
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style lang="css" scoped>
.formClass {
  .el-form-item {
    width: calc(100% / 4 - 15px);
    &:last-child {
      margin-left: auto;
      width: fit-content;
    }
  }
}
</style>
