<template>
  <div class="page">
    <div class="ele-body">
      <a-card :bordered="false">
        <!-- 表格 -->
        <ele-pro-table
          ref="tableRef"
          row-key="certificateId"
          :columns="columns"
          :datasource="datasource"
          v-model:selection="selection"
          :customRow="customRow"
          tool-class="ele-toolbar-form"
          class="sys-org-table"
          :striped="true"
        >
          <template #toolbar>
            <search
              @search="reload"
              :selection="selection"
              @add="openEdit"
              @remove="removeBatch"
            />
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'nickname'">
              <a-tooltip :title="`ID：${record.userId}`">
                <a-avatar
                  :size="30"
                  :src="`${record.avatar}`"
                  style="margin-right: 4px"
                >
                  <template #icon>
                    <UserOutlined />
                  </template>
                </a-avatar>
                {{ record.nickname }}
              </a-tooltip>
            </template>
            <template v-if="column.key === 'files'">
              <a-image-preview-group>
                <a-image
                  v-for="(item, index) in JSON.parse(record.files)"
                  :key="index"
                  :width="50"
                  :src="item.url"
                />
              </a-image-preview-group>
            </template>
            <template v-if="column.key === 'status'">
              <a-tag v-if="record.status === 10" color="orange">待审核</a-tag>
              <a-tag v-if="record.status === 20" color="error">已驳回</a-tag>
              <a-tag v-if="record.status === 30" color="green">已通过</a-tag>
            </template>
            <template v-if="column.key === 'action'">
              <a-space>
                <a-button @click="openEdit(record)">管理</a-button>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- 编辑弹窗 -->
    <CertificateEdit
      v-model:visible="showEdit"
      :form="current"
      @done="reload"
    />
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro';
  import Search from './components/search.vue';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { toDateString } from 'ele-admin-pro';
  import CertificateEdit from './components/certificate-edit.vue';
  import {
    pageCertificate,
    removeBatchCertificate
  } from '@/api/love/certficate';
  import type {
    Certificate,
    CertificateParam
  } from '@/api/love/certficate/model';
  import useFormData from '@/utils/use-form-data';

  defineProps<{
    activeKey?: boolean;
    data?: any;
  }>();

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      title: '编号',
      dataIndex: 'certificateId',
      key: 'certificateId',
      align: 'center'
    },
    {
      title: '会员信息',
      dataIndex: 'nickname',
      key: 'nickname',
      align: 'center'
    },
    {
      title: '真实姓名',
      dataIndex: 'realName',
      align: 'center'
    },
    {
      title: '身份证号码',
      dataIndex: 'certificateCode',
      align: 'center'
    },
    {
      title: '证件',
      dataIndex: 'files',
      key: 'files',
      align: 'center'
    },
    {
      title: '审核状态',
      dataIndex: 'status',
      key: 'status',
      align: 'center'
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      align: 'center',
      sorter: true,
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '操作',
      key: 'action',
      width: 100,
      align: 'center'
    }
  ]);

  // 表格选中数据
  const selection = ref<Certificate[]>([]);
  // 当前编辑数据
  const current = ref<Certificate | null>(null);
  // 是否显示编辑弹窗
  const showEdit = ref(false);

  // 表格数据源
  const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
    // 搜索条件
    if (where.certificateType == undefined) {
      where.certificateType = 'idCard';
    }

    return pageCertificate({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  // 表单数据
  const { form } = useFormData<Certificate>({
    certificateId: undefined,
    certificateType: undefined,
    status: undefined
  });

  /* 搜索 */
  const reload = (where?: CertificateParam) => {
    if (where?.certificateType == 'idCard') {
      columns.value = [
        {
          title: '编号',
          dataIndex: 'certificateId',
          key: 'certificateId',
          align: 'center'
        },
        {
          title: '会员信息',
          dataIndex: 'nickname',
          key: 'nickname',
          align: 'center'
        },
        {
          title: '真实姓名',
          dataIndex: 'realName',
          align: 'center'
        },
        {
          title: '身份证号码',
          dataIndex: 'certificateCode',
          align: 'center'
        },
        {
          title: '证件',
          dataIndex: 'files',
          key: 'files',
          align: 'center'
        },
        {
          title: '审核状态',
          dataIndex: 'status',
          key: 'status',
          align: 'center'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          key: 'createTime',
          align: 'center',
          sorter: true,
          customRender: ({ text }) => toDateString(text)
        },
        {
          title: '操作',
          key: 'action',
          width: 100,
          align: 'center'
        }
      ];
    }
    if (where?.certificateType == 'house') {
      columns.value = [
        {
          title: '编号',
          dataIndex: 'certificateId',
          key: 'certificateId',
          align: 'center'
        },
        {
          title: '会员信息',
          dataIndex: 'nickname',
          key: 'nickname',
          align: 'center'
        },
        {
          title: '房产所在城市',
          dataIndex: 'cityByHouse',
          align: 'center'
        },
        {
          title: '证件',
          dataIndex: 'files',
          key: 'files',
          align: 'center'
        },
        {
          title: '审核状态',
          dataIndex: 'status',
          key: 'status',
          align: 'center'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          key: 'createTime',
          align: 'center',
          sorter: true,
          customRender: ({ text }) => toDateString(text)
        },
        {
          title: '操作',
          key: 'action',
          width: 100,
          align: 'center'
        }
      ];
    }
    if (where?.certificateType == 'education') {
      columns.value = [
        {
          title: '编号',
          dataIndex: 'certificateId',
          key: 'certificateId',
          align: 'center'
        },
        {
          title: '会员信息',
          dataIndex: 'nickname',
          key: 'nickname',
          align: 'center'
        },
        {
          title: '学历',
          dataIndex: 'education',
          align: 'center'
        },
        {
          title: '认证方式',
          dataIndex: 'educationType',
          align: 'center'
        },
        {
          title: '毕业院校',
          dataIndex: 'college',
          align: 'center'
        },
        {
          title: '证件',
          dataIndex: 'files',
          key: 'files',
          align: 'center'
        },
        {
          title: '审核状态',
          dataIndex: 'status',
          key: 'status',
          align: 'center'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          key: 'createTime',
          align: 'center',
          sorter: true,
          customRender: ({ text }) => toDateString(text)
        },
        {
          title: '操作',
          key: 'action',
          width: 100,
          align: 'center'
        }
      ];
    }
    if (where?.certificateType == 'house') {
      columns.value = [
        {
          key: 'index',
          width: 48,
          align: 'center',
          fixed: 'left',
          hideInSetting: true,
          hideInTable: true,
          customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
        },
        {
          title: '编号',
          dataIndex: 'certificateId',
          key: 'certificateId',
          align: 'center'
        },
        {
          title: '会员信息',
          dataIndex: 'nickname',
          key: 'nickname',
          align: 'center'
        },
        {
          title: '车辆品牌',
          dataIndex: 'carBrand',
          align: 'center'
        },
        {
          title: '证件',
          dataIndex: 'files',
          key: 'files',
          align: 'center'
        },
        {
          title: '审核状态',
          dataIndex: 'status',
          key: 'status',
          align: 'center'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          key: 'createTime',
          align: 'center',
          sorter: true,
          customRender: ({ text }) => toDateString(text)
        },
        {
          title: '操作',
          key: 'action',
          width: 100,
          align: 'center'
        }
      ];
    }
    selection.value = [];
    tableRef?.value?.reload({ where: where });
  };

  /* 打开编辑弹窗 */
  const openEdit = (row?: Certificate) => {
    console.log(row);
    current.value = row ?? null;
    showEdit.value = true;
  };

  /* 批量删除 */
  const removeBatch = () => {
    console.log(selection.value);
    if (!selection.value.length) {
      message.error('请至少选择一条数据');
      return;
    }
    Modal.confirm({
      title: '提示',
      content: '确定要删除选中的记录吗?',
      icon: createVNode(ExclamationCircleOutlined),
      maskClosable: true,
      onOk: () => {
        const hide = message.loading('请求中..', 0);
        removeBatchCertificate(selection.value.map((d) => d.certificateId))
          .then((msg) => {
            hide();
            message.success(msg);
            reload();
          })
          .catch((e) => {
            hide();
            message.error(e.message);
          });
      }
    });
  };

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

  reload();
</script>

<script lang="ts">
  export default {
    name: 'CertificateIndex'
  };
</script>

<style lang="less" scoped>
  p {
    line-height: 0.8;
  }
  .sys-org-table :deep(.ant-table-body) {
    overflow: auto !important;
    overflow: overlay !important;
  }

  .sys-org-table :deep(.ant-table-pagination.ant-pagination) {
    padding: 0 4px;
    margin-bottom: 0;
  }
  .price-edit {
    padding-right: 5px;
  }
  .comments {
    max-width: 200px;
  }
</style>
