<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="审批结果" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审批结果"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="变更前名称" prop="oldEnterpriseName">
        <el-input
          v-model="queryParams.oldEnterpriseName"
          placeholder="请输入变更前企业名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="变更前法人" prop="oldLegalPerson">
        <el-input
          v-model="queryParams.oldLegalPerson"
          placeholder="请输入变更前企业法定代表人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="企业名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="企业法人" prop="legalPerson">
        <el-input
          v-model="queryParams.legalPerson"
          placeholder="请输入企业法定代表人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="信用代码" prop="unifiedCreditCode">
        <el-input
          v-model="queryParams.unifiedCreditCode"
          placeholder="请输入统一社会信用代码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="经营状态" prop="operateState">
        <el-select
          v-model="queryParams.operateState"
          placeholder="请选择经营状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.ENTERPRISE_OPERATE_STATE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="企业类型" prop="enterpriseType">
        <el-select
          v-model="queryParams.enterpriseType"
          placeholder="请选择企业类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.ENTERPRISE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属行业" prop="enterpriseBelongingToIndustry">
        <el-input
          v-model="queryParams.enterpriseBelongingToIndustry"
          placeholder="请输入企业所属行业"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否涉危" prop="isSecure">
        <el-select
          v-model="queryParams.isSecure"
          placeholder="请选择是否涉及危化证"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.ENTERPRISE_IS_SECURITY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button plain type="primary" @click="handleCreate()">
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="审批结果" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="变更前企业名称" align="center" prop="oldEnterpriseName" />
      <el-table-column label="变更前企业法定代表人" align="center" prop="oldLegalPerson" />
      <el-table-column label="企业名称" align="center" prop="name" />
      <el-table-column label="企业法定代表人" align="center" prop="legalPerson" />
      <el-table-column label="统一社会信用代码" align="center" prop="unifiedCreditCode" />
      <el-table-column label="电话" align="center" prop="telephone" />
      <el-table-column label="邮箱" align="center" prop="mailbox" />
      <el-table-column label="注册地址" align="center" prop="registerAddress" />
      <el-table-column label="通信地址" align="center" prop="communicationAddress" />
      <el-table-column label="经营状态" align="center" prop="operateState">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ENTERPRISE_OPERATE_STATE" :value="scope.row.operateState" />
        </template>
      </el-table-column>
      <el-table-column label="成立日期" align="center" prop="establishDate">
        <template #default="scope">
          {{ formatDate(scope.row.establishDate, 'YYYY-MM-DD') }}
        </template>
      </el-table-column>
      <el-table-column label="企业类型" align="center" prop="enterpriseType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ENTERPRISE_TYPE" :value="scope.row.enterpriseType" />
        </template>
      </el-table-column>
      <el-table-column label="是否涉及危化证" align="center" prop="isSecure">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ENTERPRISE_IS_SECURITY" :value="scope.row.isSecure" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleDetail(scope.row)"
          >
            详情
          </el-button>
          <el-button
            link
            type="primary"
            @click="handleProcessDetail(scope.row)"
          >
            进度
          </el-button>
          <el-button
            v-if="scope.row.result === 1"
            link
            type="danger"
            @click="cancelLeave(scope.row)"
          >
            取消
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <EnterpriseChangeForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import {dateFormatter, formatDate} from '@/utils/formatTime'
import { EnterpriseChangeApi, EnterpriseChangeVO } from '@/api/fta/enterprisechange'
import EnterpriseChangeForm from './EnterpriseChangeForm.vue'

/** 企业信息变更 列表 */
defineOptions({ name: 'EnterpriseChange' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter() // 路由

const loading = ref(true) // 列表的加载中
const list = ref<EnterpriseChangeVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  status: undefined,
  oldEnterpriseName: undefined,
  oldLegalPerson: undefined,
  name: undefined,
  legalPerson: undefined,
  unifiedCreditCode: undefined,
  operateState: undefined,
  enterpriseType: undefined,
  enterpriseBelongingToIndustry: undefined,
  isSecure: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EnterpriseChangeApi.getEnterpriseChangePage(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 formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 添加操作 */
const handleCreate = () => {
  router.push({ name: 'FtaEnterpriseChangeCreate' })
}

/** 详情操作 */
const handleDetail = (row: EnterpriseChangeVO) => {
  router.push({
    name: 'FtaEnterpriseChangeDetail',
    query: {
      id: row.id
    }
  })
}

/** 审批进度 */
const handleProcessDetail = (row) => {
  console.log("审批进度当前行数据:",row.processInstanceId)
  router.push({
    name: 'BpmProcessInstanceDetail',
    query: {
      id: row.processInstanceId
    }
  })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await EnterpriseChangeApi.deleteEnterpriseChange(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
