<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="套餐名称" prop="name" v-hasPermi="['menduner:system:enterprise-package:query']">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入套餐名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" v-hasPermi="['menduner:system:enterprise-package:query']">
        <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>
      <div class="text-center mb-12px">
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" v-hasPermi="['menduner:system:enterprise-package:query']" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" v-hasPermi="['menduner:system:enterprise-package:query']" /> 重置</el-button>
      </div>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="text-right">
      <el-button
        type="primary"
        plain
        v-hasPermi="['menduner:system:enterprise-package:create']"
        @click="openForm('create')"
      >
        <Icon icon="ep:plus" class="mr-5px" /> 新增
      </el-button>
      <el-button
        type="success"
        plain
        v-hasPermi="['menduner:system:enterprise-package:export']"
        @click="handleExport"
        :loading="exportLoading"
      >
        <Icon icon="ep:download" class="mr-5px" /> 导出
      </el-button>
    </div>
    <el-table v-loading="loading" :data="list" :stripe="true">
      <el-table-column label="套餐名称" align="center" prop="name" />
      <el-table-column label="原价(元)" align="center" prop="originalPrice">
        <template #default="{ row }">
          {{ row.originalPrice ? row.originalPrice / 100 : '' }}
        </template>
      </el-table-column>
      <el-table-column label="价格(元)" align="center" prop="price">
        <template #default="{ row }">
          {{ row.price ? row.price / 100 : '' }}
        </template>
      </el-table-column>
      <el-table-column label="激活VIP" align="center" prop="activateVip">
        <template #default="{ row }">{{ row.activateVip ? '是' : '否' }}</template>
      </el-table-column>
      <el-table-column label="会员时效（天）" align="center" prop="day" />
      <el-table-column label="查看简历数" align="center" prop="lookCvCount" />
      <el-table-column label="发布职位数" align="center" prop="publishJobCount" />
      <!-- <el-table-column label="搜索人才数" align="center" prop="searchCount" /> -->
      <el-table-column label="开启人才地图" align="center" prop="personMap">
        <template #default="{ row }">{{ row.personMap ? '是' : '否' }}</template>
      </el-table-column>
      <el-table-column label="发布众聘职位" align="center" prop="hireJob">
        <template #default="{ row }">{{ row.hireJob ? '是' : '否' }}</template>
      </el-table-column>
      <!-- <el-table-column label="开启新任命" align="center" prop="newAppointment">
        <template #default="{ row }">{{ row.newAppointment ? '是' : '否' }}</template>
      </el-table-column> -->
      <el-table-column label="参加招聘会" align="center" prop="jobFair">
        <template #default="{ row }">{{ row.jobFair ? '是' : '否' }}</template>
      </el-table-column>
      <el-table-column label="说明文本" align="center" prop="text" width="160px" />
      <!-- <el-table-column label="状态" align="center" prop="status">
        <template #default="{ row }">
          <el-tag :type="row.status === '0' ? 'success' : row.status === '1' ? 'danger' : 'info'">
            {{ row.status === '0' ? '正常' : row.status === '1' ? '停用' : '隐藏' }}
          </el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="160px" />
      <el-table-column label="操作" align="center" min-width="100" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            v-hasPermi="['menduner:system:enterprise-package:update']"
            @click="openForm('update', scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            link
            v-if="!scope.row.defaultPackage"
            type="danger"
            v-hasPermi="['menduner:system:enterprise-package:delete']"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

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

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { EnterprisePackageApi } from '@/api/menduner/system/enterprisePackage'
import PackageForm from './PackageForm.vue'

/** 企业套餐 列表 */
defineOptions({ name: 'EnterprisePackage' })

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

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EnterprisePackageApi.getPackagePage(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 handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await EnterprisePackageApi.deletePackage(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

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

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