<template>
  <div>
    <div class="title">门店审核</div>
    <el-form
      ref="searchFormRef"
      class="form"
      :inline="true"
      label-width="98px"
      :model="queryParams"
    >
      <el-form-item label="门店名称" prop="store_name">
        <el-input
          clearable
          placeholder="请输入"
          style="width: 220px"
          v-model="queryParams.store_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="store_name" show-overflow-tooltip />
        <el-table-column align="center" label="地区" prop="location" show-overflow-tooltip />
        <el-table-column align="center" label="地址详情" prop="address" show-overflow-tooltip />
        <el-table-column
          align="center"
          label="工作时间"
          prop="business_hours"
          show-overflow-tooltip
        />
        <el-table-column align="center" label="电话" prop="contact_phone" show-overflow-tooltip />
        <el-table-column align="center" label="业务类型" prop="tags" show-overflow-tooltip />
        <el-table-column
          align="center"
          width="180"
          label="注册日期"
          prop="created_at"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            {{ row.created_at ? dayjs(row.created_at * 1000).format('YYYY-MM-DD HH:mm:ss') : '' }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="150">
          <template #default="{ row }">
            <el-button link type="primary" @click="goDetail(row)"> 查看 </el-button>
            <el-button link type="primary" @click="review(row.id, 2)"> 通过 </el-button>
            <el-button link type="danger" @click="review(row.id, 3)"> 驳回 </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"
      :categoryList="categoryList"
      @closed="isShowDialog = false"
      @on-success="getList"
    />
  </div>
</template>

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

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

const review = (id, status) => {
  if (status === 2) {
    ElMessageBox.confirm('确认通过？', '通过', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    })
      .then(() => {
        RPC.post('/store/review', { id, status }).then(() => {
          ElMessage.success('操作成功')
          getList()
        })
      })
      .catch(() => {})
  } else {
    ElMessageBox.prompt('请输入驳回原因', '驳回', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      inputErrorMessage: '请输入驳回原因',
      inputValidator: (value) => {
        return value.trim() !== ''
      },
    })
      .then(({ value }) => {
        RPC.post('/store/review', { id, status, note: value }).then(() => {
          ElMessage.success('操作成功')
          getList()
        })
      })
      .catch(() => {})
  }
}

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

getList()
</script>

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