<template>
  <div>
    <div class="title">企业管理</div>
    <el-form
      ref="searchFormRef"
      class="form"
      :inline="true"
      label-width="98px"
      :model="queryParams"
    >
      <el-form-item label="企业名称" prop="company_name">
        <el-input
          clearable
          placeholder="请输入"
          style="width: 220px"
          v-model="queryParams.company_name"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label-width="100px">
        <el-button icon="Search" type="primary" @click="handleQuery"> 搜索 </el-button>
        <el-button icon="RefreshRight" @click="resetQuery"> 重置 </el-button>
      </el-form-item>
    </el-form>

    <div style="margin-bottom: 20px">
      <!-- <el-button icon="Plus" type="primary" @click="add"> 新建 </el-button> -->
    </div>
    <div ref="tableContainerEl" class="table_border table">
      <el-table
        ref="tableRef"
        :data="tableData"
        v-loading="loading"
        header-row-class-name="table-header"
      >
        <el-table-column align="center" label="企业名称" prop="company_name" />
        <el-table-column align="center" label="姓名" prop="contact_person" />
        <el-table-column align="center" label="手机号码" prop="contact_phone" />
        <el-table-column align="center" label="邮箱" prop="contact_email" />
        <el-table-column align="center" label="地址" prop="address" />
        <el-table-column align="center" label="状态" prop="status" width="70">
          <template #default="{ row }">
            <span v-if="row.status == 1">待审核</span>
            <span v-if="row.status == 2">通过</span>
            <span v-if="row.status == 3">驳回</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="更新日期" prop="updated_at">
          <template #default="{ row }">
            <span>{{
              row.updated_at && dayjs(row.updated_at * 1000).format('YYYY-MM-DD HH:mm:ss')
            }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="驳回原因" prop="review_note" show-overflow-tooltip />
        <el-table-column align="center" label="操作">
          <template #default="{ row }">
            <el-button v-if="isAdmin && row.status == 2" link type="primary" @click="setAdmin(row)">
              设为管理员
            </el-button>
            <el-button v-if="row.status != 1" link type="primary" @click="edit(row)">
              修改
            </el-button>

            <el-button v-if="row.status != 1" link type="danger" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:limit="queryParams.limit"
      v-model:page="queryParams.page"
      @pagination="getList"
    />

    <HandleDialog
      v-if="isShowDialog"
      :type="rowType"
      :row="currentRow"
      @closed="isShowDialog = false"
      @on-success="getList"
    />
  </div>
</template>

<script setup>
import { useTableRPC } from '@/utils/hook'
import HandleDialog from './HandleDialog.vue'
import dayjs from 'dayjs'
import RPC from '@/utils/rpc'

import { useUserStore } from '@/stores/user'

const isAdmin =
  useUserStore().userInfo?.jurisdictions &&
  useUserStore().userInfo?.jurisdictions?.findIndex((item) => item.code === 'superadmin') !== -1

const setAdmin = (row) => {
  RPC.post('/enterprise/set_admin', { id: row.id }).then(() => {
    ElMessage.success('操作成功')
    getList()
  })
}

const rowType = ref('')
const currentRow = ref(null)
const add = () => {
  isShowDialog.value = true
  currentRow.value = null
  rowType.value = 'add'
}
const edit = (row) => {
  isShowDialog.value = true
  rowType.value = 'edit'
  currentRow.value = row
}

const {
  loading,
  tableData,
  tableRef,
  searchFormRef,
  total,
  queryParams,
  getList,
  handleQuery,
  resetQuery,
  handleDelete,
  isShowDialog,
  maxHeight,
  tableContainerEl,
} = useTableRPC({
  queryParams: {},
  remoteDataFn: '/enterprise/list',
  deleteConfig: {
    remote: '/enterprise/delete',
    key: 'id',
    message(row) {
      return `确认删除 “${row.company_name}”`
    },
  },
})

getList()
</script>

<style lang="scss" scoped>
.title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 20px;
}
</style>
