<template>
  <a-page-header :title="getPageTitle()" @back="() => $router.go(-1)">
    <a-card :bordered="false" :body-style="{ padding: '16px' }">
      <ele-pro-table
        ref="tableRef"
        row-key="applyId"
        :columns="columns"
        :datasource="datasource"
        :customRow="customRow"
        tool-class="ele-toolbar-form"
        class="sys-org-table"
      >
        <template #toolbar>
          <Search
            @search="reload"
            :selection="selection"
            @add="openEdit"
            @remove="removeBatch"
          />
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'image'">
            <a-image :src="record.image" :width="50" />
          </template>
          <template v-if="column.key === 'status'">
            <a-tag v-if="record.status === 0" color="blue">待审核</a-tag>
            <a-tag v-if="record.status === 1" color="green">审核通过</a-tag>
            <a-tag v-if="record.status === 2" color="red">审核驳回</a-tag>
          </template>
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="openEdit(record)">详情</a>
              <a-divider type="vertical" />
              <a v-if="record.status === 0" @click="openReview(record)">审核</a>
              <a-divider v-if="record.status === 0" type="vertical" />
              <a-popconfirm
                title="确定要删除此记录吗？"
                @confirm="remove(record)"
              >
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
  </a-page-header>

  <!-- 编辑弹窗 -->
  <ShopMerchantApplyEdit
    v-model:visible="showEdit"
    :data="current"
    @done="reload"
  />

  <!-- 审核弹窗 -->
  <ApplyReview v-model:visible="showReview" :data="current" @done="reload" />
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { useRoute } from 'vue-router';
  import { message } from 'ant-design-vue';
  import ShopMerchantApplyEdit from './components/shopMerchantApplyEdit.vue';
  import ApplyReview from './components/applyReview.vue';
  import Search from './components/search.vue';
  import {
    pageShopMerchantApply,
    removeShopMerchantApply,
    removeBatchShopMerchantApply
  } from '@/api/shop/shopMerchantApply';

  // 页面标题
  const getPageTitle = () => {
    return '商户入驻申请';
  };

  const route = useRoute();
  const tableRef = ref();

  // 表格列配置
  const columns = reactive([
    {
      title: 'ID',
      dataIndex: 'applyId',
      width: 80,
      align: 'center'
    },
    {
      title: '商户名称',
      dataIndex: 'merchantName',
      ellipsis: true
    },
    {
      title: '联系人',
      dataIndex: 'realName'
    },
    {
      title: '联系电话',
      dataIndex: 'phone'
    },
    {
      title: '店铺类型',
      dataIndex: 'shopType'
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      align: 'center',
      width: 120
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 180
    },
    {
      title: '操作',
      key: 'action',
      align: 'center',
      width: 150
    }
  ]);

  // 表格数据源
  const datasource = ({ page, limit, where }) => {
    return pageShopMerchantApply({ ...where, page, limit });
  };

  // 当前选中数据
  const current = ref(null);

  // 是否显示编辑弹窗
  const showEdit = ref(false);

  // 是否显示审核弹窗
  const showReview = ref(false);

  // 表格选中项
  const selection = ref([]);

  // 刷新表格
  const reload = (where) => {
    tableRef.value?.reload({ where });
  };

  // 打开编辑弹窗
  const openEdit = (record) => {
    current.value = record;
    showEdit.value = true;
  };

  // 打开审核弹窗
  const openReview = (record) => {
    current.value = record;
    showReview.value = true;
  };

  // 删除单条记录
  const remove = (record) => {
    removeShopMerchantApply(record.applyId)
      .then((msg) => {
        message.success(msg);
        reload();
      })
      .catch((e) => {
        message.error(e.message);
      });
  };

  // 批量删除
  const removeBatch = (records) => {
    const ids = records.map((item) => item.applyId);
    removeBatchShopMerchantApply(ids)
      .then((msg) => {
        message.success(msg);
        reload();
      })
      .catch((e) => {
        message.error(e.message);
      });
  };

  // 自定义行属性
  const customRow = (record) => {
    return {
      // 行点击事件
      onClick: () => {
        // console.log(record);
      },
      // 行双击事件
      onDblclick: () => {
        openEdit(record);
      }
    };
  };
</script>

<style lang="less" scoped>
  .sys-org-table {
    :deep(.ant-table-thead > tr > th) {
      background-color: #fafafa;
      font-weight: 600;
    }

    :deep(.ant-table-tbody > tr:hover > td) {
      background-color: #f8f9fa;
    }
  }
</style>
