import type { VxeGridProps } from '#/adapter/vxe-table';

import { getAdminList } from '#/views/system/admin/api';

import type { AdminFormType } from './role-meta';
import { ROLE_SALESPERSON } from './role-meta';

interface RowType {
  id: string;
  nick_name: string;
  email: string;
  avatar: string;
  open_id: string;
  code: string;
  phone: string;
  login_account: string;
  desc: string;
  created_at: string;
  store_id?: number;
  doctor_id?: number;
  pharmacist_id?: number;
  store?: { id?: number; name?: string; pic?: string; mobile?: string };
  doctor_profile?: {
    su_id?: number;
    name?: string;
    avatar?: string;
    mobile?: string;
  } | null;
  province_id?: number;
  city_id?: number;
}

function buildColumns(formType: AdminFormType, roleId: number) {
  const isSalespersonList = roleId === ROLE_SALESPERSON;
  const cols: VxeGridProps<RowType>['columns'] = [
    { type: 'checkbox', width: 60 },
    { field: 'id', align: 'left', title: 'ID', width: 100 },
  ];

  if (isSalespersonList) {
    cols.push({
      field: 'nick_name',
      align: 'left',
      title: '业务员',
      minWidth: 180,
      slots: { default: 'promoter_info' },
    });
  } else {
    cols.push(
      {
        field: 'nick_name',
        align: 'left',
        title: '名称',
        ...(formType === 'doctor' ? { slots: { default: 'nick_name' } } : {}),
      },
      {
        field: 'avatar',
        align: 'left',
        title: '头像',
        slots: { default: 'avatar' },
        width: 130,
      },
    );
  }

  cols.push({ field: 'open_id', title: 'Open ID' });

  if (formType === 'address' || formType === 'supplier') {
    cols.push({ field: 'code', title: '业务推广码' });
  }

  if (formType === 'address') {
    cols.push({
      field: 'region',
      title: '省市区',
      minWidth: 180,
      slots: { default: 'region' },
    });
  }

  if (formType === 'supplier') {
    cols.push({ field: 'supplier.name', title: '所属供应商' });
  }

  if (formType === 'deliveryWarehouse') {
    cols.push({ field: 'delivery_warehouse.name', title: '所属配送仓库' });
  }

  if (formType === 'clinic' || formType === 'doctor' || formType === 'pharmacist') {
    cols.push({
      field: 'store_id',
      title: '所属诊所',
      minWidth: 180,
      slots: { default: 'store_name' },
    });
  }

  if (formType === 'doctor') {
    cols.push({
      field: 'doctor_id',
      title: '关联医生',
      minWidth: 180,
      slots: { default: 'doctor_profile' },
    });
  }

  if (formType === 'pharmacist') {
    cols.push({ field: 'pharmacist_id', title: '药师ID', width: 100 });
  }

  if (formType === 'basic') {
    cols.push({ field: 'platform.name', title: '所属平台' });
  }

  cols.push(
    { field: 'phone', title: '手机号码', slots: { default: 'phone' } },
    {
      field: 'login_account',
      align: 'left',
      title: '登录账号',
      slots: { default: 'login_account' },
      width: 150,
    },
    { field: 'email', title: '邮箱' },
    { field: 'created_at', title: '注册时间' },
    { type: 'html', title: '操作', width: 200, slots: { default: 'action' } },
  );

  return cols;
}

export function createAdminGridOptions(
  roleId: number,
  formType: AdminFormType,
): VxeGridProps<RowType> {
  return {
    checkboxConfig: {
      highlight: true,
      labelField: '',
    },
    columnConfig: {
      useKey: true,
    },
    rowConfig: {
      useKey: true,
    },
    columns: buildColumns(formType, roleId),
    keepSource: true,
    pagerConfig: {},
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getAdminList({
            page: page.currentPage,
            pageSize: page.pageSize,
            role_id: roleId,
            ...formValues,
          });
        },
      },
    },
    height: 'auto',
    border: false,
    toolbarConfig: {
      search: true,
      refresh: true,
      print: false,
      export: false,
      zoom: true,
      slots: {
        buttons: 'toolbar-actions',
      },
      custom: {
        icon: 'vxe-icon-menu',
      },
    },
    showOverflow: false,
  };
}
