<script lang="ts" setup>
import type { VxeGridListeners } from '#/adapter/vxe-table';

import { ref } from 'vue';

import { Page, useVbenModal } from '@vben/common-ui';

import { Button, Image, message, Tag } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { TableAction } from '#/components/table-action';

import { auditApi, openPcWindowsApi, refuseApi } from './api';
import BindStore from './components/BindStoreModal.vue';
import BindSubjects from './components/BindSubjectsModal.vue';
import FormModalDemo from './components/modal.vue';
import DoctorCardModal from './components/DoctorCardModal.vue';
import { formOptions } from './config/search';
import { gridOptions } from './config/table';

const hasTopTableDropDownActions = ref(false);

const gridEvents: VxeGridListeners<any> = {
  checkboxChange() {
    // eslint-disable-next-line no-use-before-define
    const records = gridApi.grid.getCheckboxRecords();
    hasTopTableDropDownActions.value = records.length > 0;
  },
  checkboxAll() {
    // eslint-disable-next-line no-use-before-define
    const records = gridApi.grid.getCheckboxRecords();
    hasTopTableDropDownActions.value = records.length > 0;
  },
};

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
  gridEvents,
});

const [FormModal, formModalApi] = useVbenModal({
  connectedComponent: FormModalDemo,
});

const [BindStoreModal, BindStoreModalApi] = useVbenModal({
  connectedComponent: BindStore,
});

const [BindSubjectsModal, BindSubjectsModalApi] = useVbenModal({
  connectedComponent: BindSubjects,
});

const [DoctorCardModals, DoctorCardModalApi] = useVbenModal({
  connectedComponent: DoctorCardModal,
});

const showModal = (data = {}, isUpdate = false) => {
  formModalApi.setData({
    // 表单值
    values: data,
    update: isUpdate,
    gridApi,
  });
  formModalApi.open();
};

const showBindStoreModal = (data: number) => {
  BindStoreModalApi.setData({
    // 表单值
    values: data,
    gridApi,
  });
  BindStoreModalApi.open();
};

const showBindSubjectsModal = (row: Record<string, any>) => {
  BindSubjectsModalApi.setData({
    values: row,
    gridApi,
  });
  BindSubjectsModalApi.open();
};

const showDoctorCard = (row: Record<string, any>) => {
  DoctorCardModalApi.setData({
    id: row.id,
    su_id: row.su_id,
    gridApi,
  });
  DoctorCardModalApi.open();
};

const openPcWindows = (id: number) => {
  openPcWindowsApi(id).then(() => {
    message.success('开通成功!');
    gridApi.query();
  });
};

const audit = (id: number) => {
  auditApi(id).then(() => {
    message.success('审核成功!');
    gridApi.query();
  });
};

const refuse = (id: number) => {
  refuseApi(id).then(() => {
    message.success('审核成功!');
    gridApi.query();
  });
};
</script>

<template>
  <Page auto-content-height title="医生管理">
    <FormModal />
    <BindStoreModal />
    <BindSubjectsModal />
    <DoctorCardModals />
    <Grid>
      <template #toolbar-buttons>
        <TableAction
          :actions="[
            {
              label: '新增',
              type: 'primary',
              icon: 'ant-design:plus-outlined',
              // auth: ['超级医生', 'sys:user:save'],
              onClick: showModal.bind(null),
            },
          ]"
          :drop-down-actions="[
            // {
            //   label: '删除',
            //   icon: 'ant-design:delete-outlined',
            //   ifShow: hasTopTableDropDownActions,
            //   // auth: ['超级医生', 'sys:user:save'],
            //   popConfirm: {
            //     title: '确定删除吗？',
            //     confirm: deleteApi.bind(null, false),
            //   },
            // },
          ]"
        >
          <template #more>
            <Button style="margin-left: 16px">
              批量操作
              <Icon icon="ant-design:down-outlined" />
            </Button>
          </template>
        </TableAction>
      </template>
      <template #avatar="{ row }">
        <Image :src="row.avatar" height="30" width="30" />
      </template>
      <template #name="{ row }">
        <Button type="link" size="small" @click="showDoctorCard(row)">
          {{ row.name }}
        </Button>
      </template>
      <template #stores="{ row }">
        <p v-for="item in row.stores" :key="item.store_id">
          {{ item?.store?.name }}
        </p>
      </template>
      <template #subjects="{ row }">
        {{ row.subjects?.name ?? '-' }}
      </template>
      <template #service-user="{ row }">
        <Tag v-if="row.service_user?.status === 2" color="success">已通过</Tag>
        <Tag v-else-if="row.service_user?.status === 0" color="blue">
          待激活
        </Tag>
        <Tag v-else-if="row.service_user?.status === 1" color="warning">
          待审核
        </Tag>
        <Tag v-else-if="row.service_user?.status === 3" color="error">
          已拒绝
        </Tag>
        <Tag v-else-if="row.service_user?.status === -1" color="error">
          已停用
        </Tag>
      </template>
      <template #toolbar-tools></template>
      <template #action="{ row }">
        <TableAction
          :actions="[
            {
              label: '医生卡片',
              type: 'link',
              icon: 'ant-design:idcard-outlined',
              size: 'small',
              onClick: showDoctorCard.bind(null, row),
            },
            {
              label: '编辑',
              type: 'link',
              icon: 'uil:edit',
              size: 'small',
              // auth: ['doctor', 'sys:role:detail'],
              onClick: showModal.bind(null, row),
            },
            {
              label: '绑定诊所',
              type: 'link',
              icon: 'uil:edit',
              size: 'small',
              // auth: ['doctor', 'sys:role:detail'],
              onClick: showBindStoreModal.bind(null, row),
            },
            {
              label: '绑定诊疗科目',
              type: 'link',
              icon: 'uil:edit',
              size: 'small',
              onClick: showBindSubjectsModal.bind(null, row),
            },
            {
              label: '开通PC端',
              type: 'link',
              icon: 'uil:edit',
              size: 'small',
              // auth: ['doctor', 'sys:role:detail'],
              onClick: openPcWindows.bind(null, row.id),
            },
          ]"
          :drop-down-actions="[
            {
              label: '审核通过',
              type: 'link',
              icon: 'uil:edit',
              size: 'small',
              ifShow: row.service_user?.status === 1,
              // auth: ['doctor', 'sys:role:detail'],
              onClick: audit.bind(null, row.id),
            },
            {
              label: '拒绝审核',
              type: 'link',
              icon: 'uil:edit',
              size: 'small',
              ifShow: row.service_user?.status === 1,
              // auth: ['doctor', 'sys:role:detail'],
              onClick: refuse.bind(null, row.id),
            },
          ]"
        />
      </template>
    </Grid>
  </Page>
</template>
