<template>
  <PageWrapper contentBackground>
    <div class="desc-wrap-BasicTable">
      <BasicTable @register="registerTable">
        <template #toolbar>
          <a-button type="primary" @click="openModal(true, {language})" v-if="getCheckPerm('dealer-add')">
            新增经销商</a-button
          >
        </template>
        <template #action="{ record }">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '权益设置',
                //icon: 'icon-park-outline:folder-withdrawal-one',
                ifShow: getCheckPerm('dealer-setequity'),
                onClick: handleWithdraw.bind(null, record),
              },
              {
                label: '编辑',
                //icon: 'ep:edit',
                ifShow: getCheckPerm('dealer-edit'),
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                //icon: 'ic:outline-delete-outline',
                ifShow: getCheckPerm('dealer-delete'),
                onClick: handleDelete.bind(null, record),
              },
            ]"
          />
        </template>
      </BasicTable>
    </div>
    <addNewModal @register="register" @update="reload" />
    <editNewModal @register="registerEdit" @update="reload" />
    <financeModal @register="registerFinance" @update="reload" />
  </PageWrapper>
</template>
<script lang="ts">
import { defineComponent, h, ref } from 'vue';
import {
  BasicTable,
  useTable,
  TableAction,
  BasicColumn,
  TableImg,
  FormProps,
} from '/@/components/Table';
import { PageWrapper } from '/@/components/Page';
import { Time } from '/@/components/Time';
import { agentNewList, agentNewDel } from '/@/api/dealer';
import {  Descriptions, Tabs } from 'ant-design-vue';
import { useModal } from '/@/components/Modal';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import addNewModal from './components/addModal.vue';
import editNewModal from './components/editModal.vue';
import financeModal from './components/financeModal.vue';
import { usePermissionStore } from '/@/store/modules/permission';
export default defineComponent({
  components: {
    BasicTable,
    TableAction,
    PageWrapper,
    TableImg,
    addNewModal,
    editNewModal,
    financeModal,
    [Descriptions.name]: Descriptions,
    [Descriptions.Item.name]: Descriptions.Item,
    [Tabs.name]: Tabs,
    [Tabs.TabPane.name]: Tabs.TabPane,
  },
  setup() {
    const { t } = useI18n();
    const { createMessage, createConfirm } = useMessage();
    const permissionStore = usePermissionStore();
    const { getCheckPerm } = permissionStore;
    const [register, { openModal }] = useModal();
    const [registerEdit, { openModal:openEditModal }] = useModal();
    const [registerFinance, { openModal:openFinanceModal }] = useModal();
    const language = ref<string>('cn'); //未处理，0已处理（默认1）
    const columns: BasicColumn[] = [
      {
        title: '经销商名称',
        dataIndex: 'name',
        ellipsis: true,
        width: 250,
      },
      {
        title: '联系人',
        dataIndex: 'nickName',
        ellipsis: true,
        width: 250,
      },
      {
        title: '账号',
        ellipsis: true,
        dataIndex: 'userName',
        width: 120,
      },
      {
        title: '创建人',
        ellipsis: true,
        dataIndex: 'sysUserName',
        width: 120,
      },
      {
        title: '创建时间',
        dataIndex: 'createTime',
        width: 150,
        customRender: ({ record }) => {
          return (
            record.createTime &&
            h(Time, {
              value: record.createTime,
              mode: 'datetime',
            })
          );
        },
      },
    ];
    const searchForm: Partial<FormProps> = {
      labelWidth: 100,
      autoSubmitOnEnter: true,
      schemas: [
        {
          field: 'name',
          label: '经销商名称',
          component: 'Input',
          colProps: {
            xl: 7,
            xxl: 7,
          },
        },
      ],
    };
    const [registerTable, { reload }] = useTable({
      api: agentNewList,
      title: '经销商账号列表',
      columns: columns,
      useSearchForm: true,
      formConfig: searchForm,
      showTableSetting: true,
      showIndexColumn: false,
      searchInfo: { language },
      rowKey: 'id',
      fetchSetting: {
        pageField: 'pageNum',
        sizeField: 'pageSize',
        listField: 'list',
        totalField: 'total',
      },
      beforeFetch: (T) => {
        if (T.ctivated) {
          T.publicTimeStart = T.ctivated[0];
          T.publicTimeEnd = T.ctivated[1];
        }
        return T;
      },
      actionColumn: {
        width: 220,
        title: '操作',
        dataIndex: 'action',
        slots: { customRender: 'action' },
      },
      canResize: true,
    });
    async function handleDelete(record: Recordable) {
      createConfirm({
          iconType: 'warning',
          title: () => h('span', '温馨提示'),
          content: () => h('span', '确定要删除吗?'),
          onOk: async () => {
            await agentNewDel({ id: record.id });
            createMessage.success(t('common.optSuccess'));
            reload();
          },
        });
    }
    async function handlePublish(record: Recordable) {
      console.log('点击了发布', record);
      await casePublicApi({ id: record.id, isPublic: 1 });
      createMessage.success(t('common.optSuccess'));
      reload();
    }
    function handleEdit(record: Recordable) {
      console.log('点击了编辑', record);
      openEditModal(true, {...record,language});
    }
    async function handleWithdraw(record: Recordable) {
      openFinanceModal(true, {...record});
      // await casePublicApi({ id: record.id, isPublic: 0 });
      // createMessage.success(t('common.optSuccess'));
      // reload();
    }
    function changeTable(val: string) {
        language.value = val;
        reload();
      }
    function hendleAddNew() {
      console.log('新增新闻');
    }
    return {
      registerTable,
      handleDelete,
      handleEdit,
      handleWithdraw,
      handlePublish,
      hendleAddNew,
      changeTable,
      reload,
      language,
      register,
      registerFinance,
      registerEdit,
      openModal,
      getCheckPerm,
    };
  },
});
</script>