<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>

    <!-- 表单区域 -->
    <MerchantModel @register="registerModal" @success="handleSuccess"></MerchantModel>
    <UnionPayModel @register="registerUnplayModal" @success="handleSuccess"></UnionPayModel>
  </div>
</template>

<script lang="ts" setup>
  import { message, Switch } from 'ant-design-vue';
  import { ref, reactive, computed, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import MerchantModel from './components/MerchantModel.vue';
  import UnionPayModel from './components/UnionPayModel.vue';
  import { columns, searchFormSchema } from './Merchant.data';
  import { list, resetPassword, getDetaile } from './Merchant.api.';
  import { useUserStore } from '/@/store/modules/user';
  import { useMessage } from '/@/hooks/web/useMessage';
  const queryParam = reactive<any>({});
  const checkedKeys = ref<Array<string | number>>([]);
  const userStore = useUserStore();
  const { createMessage, createWarningModal } = useMessage();
  //注册model
  const [registerModal, { openModal }] = useModal();
  const [registerUnplayModal, { openModal: openUnPlayModal }] = useModal();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: 'Merchant',
      api: list,
      columns,
      canResize: false,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 250,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  /**
   * 编辑事件
   */
  async function handleEdit(record: Recordable) {
    const data = await getDetaile({ id: record.id });
    openModal(true, {
      data,
      isUpdate: true,
      showFooter: true,
    });
  }
  function handleUnplay(record: Recordable) {
    openUnPlayModal(true, {
      record,
      isUpdate: false,
      showFooter: false,
    });
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '银联账户',
        onClick: handleUnplay.bind(null, record),
      },
      {
        label: '重置密码',
        onClick: handleResetpassword.bind(null, record),
      },
    ];
  }
  async function handleResetpassword(record) {
    // await resetPassword({ id: record.id });
    createWarningModal({
      title: '安全提示',
      okText: '确认',
      cancelText: '取消',
      okCancel: true,
      content: `确认要重置该用户的密码吗？重置后的密码为：qlyd+用户后四位手机号。(qlyd${record.phone.slice(-4)})`,
      onOk: async () => {
        await resetPassword({ id: record.id });
        handleSuccess();
      },
      onCancel: () => {},
    });
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [];
  }
</script>

<style lang="less" scoped>
  :deep(.ant-picker),
  :deep(.ant-input-number) {
    width: 100%;
  }
</style>
