﻿import type { VbenFormApi, VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesWmPackageApi } from '#/api/mes/wm/packages';
import type { MesWmPackageLineApi } from '#/api/mes/wm/packages/line';

import { h, markRaw } from 'vue';

import {
  DICT_TYPE,
  MesAutoCodeRuleCode,
} from '@vben/constants';

import { Button } from 'ant-design-vue';

import { generateAutoCode } from '#/api/mes/md/autocode/record';
import { getItem } from '#/api/mes/md/item';
import { getSimpleUserList } from '#/api/system/user';
import { ErpSaleOrderSelect } from '#/views/erp/sale/order/components';
import { MdClientSelect } from '#/views/mes/md/client/components';
import { MdItemSelect } from '#/views/mes/md/item/components';
import { MdUnitMeasureSelect } from '#/views/mes/md/unitmeasure/components';
import { ProWorkOrderSelect } from '#/views/mes/pro/workorder/components';

/** 表单类型 */
export type FormType = 'create' | 'detail' | 'update';

/** 新增/修改的表单 */
export function useFormSchema(
  formType: FormType,
  formApi?: VbenFormApi,
): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'status',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'code',
      label: '装箱单编号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入装箱单编号',
      },
      rules: 'required',
      suffix:
        formType === 'detail'
          ? undefined
          : () =>
              h(
                Button,
                {
                  type: 'default',
                  onClick: async () => {
                    const code = await generateAutoCode(
                      MesAutoCodeRuleCode.WM_PACKAGE_CODE,
                    );
                    await formApi?.setFieldValue('code', code);
                  },
                },
                { default: () => '生成' },
              ),
    },
    {
      fieldName: 'packageDate',
      label: '装箱日期',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        placeholder: '请选择装箱日期',
        valueFormat: 'YYYY-MM-DD',
      },
      rules: 'required',
    },
    {
      fieldName: 'inspectorUserId',
      label: '检查员',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        placeholder: '请选择检查员',
        valueField: 'id',
      },
    },
    {
      fieldName: 'salesOrderCode',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'saleOrderId',
      label: '销售订单编号',
      component: markRaw(ErpSaleOrderSelect),
      componentProps: {
        placeholder: '请选择销售订单',
        onChange: (item: any) => {
          if (formApi) {
            formApi.setFieldValue('salesOrderCode', item?.no ?? '');
            formApi.setFieldValue('clientId', item?.customerId ?? undefined);
            formApi.setFieldValue('clientName', item?.customerName ?? '');
          }
        },
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'invoiceCode',
      label: '发票编号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入发票编号',
      },
    },
    {
      fieldName: 'clientId',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'clientName',
      label: '客户',
      component: 'Input',
      componentProps: {
        placeholder: '选择销售订单后自动反显',
        disabled: true,
      },
    },
    {
      fieldName: 'sizeUnitId',
      label: '尺寸单位',
      component: markRaw(MdUnitMeasureSelect),
      componentProps: {
        placeholder: '请选择尺寸单位',
      },
    },
    {
      fieldName: 'length',
      label: '箱长度',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        placeholder: '请输入箱长度',
        precision: 2,
      },
    },
    {
      fieldName: 'width',
      label: '箱宽度',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        placeholder: '请输入箱宽度',
        precision: 2,
      },
    },
    {
      fieldName: 'height',
      label: '箱高度',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        placeholder: '请输入箱高度',
        precision: 2,
      },
    },
    {
      fieldName: 'weightUnitId',
      label: '重量单位',
      component: markRaw(MdUnitMeasureSelect),
      componentProps: {
        placeholder: '请选择重量单位',
      },
    },
    {
      fieldName: 'netWeight',
      label: '净重',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        placeholder: '请输入净重',
        precision: 2,
      },
    },
    {
      fieldName: 'grossWeight',
      label: '毛重',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        placeholder: '请输入毛重',
        precision: 2,
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-3',
      componentProps: {
        placeholder: '请输入备注',
        rows: 3,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '装箱单编号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入装箱单编号',
      },
    },
    {
      fieldName: 'salesOrderCode',
      label: '销售订单编号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入销售订单编号',
      },
    },
    {
      fieldName: 'clientId',
      label: '客户',
      component: markRaw(MdClientSelect),
      componentProps: {
        placeholder: '请选择客户',
      },
    },
    {
      fieldName: 'inspectorUserId',
      label: '检查员',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        placeholder: '请选择检查员',
        valueField: 'id',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<MesWmPackageApi.Package>['columns'] {
  return [
    {
      field: 'code',
      title: '装箱单编号',
      minWidth: 250,
      fixed: 'left',
      treeNode: true,
      slots: { default: 'code' },
    },
    {
      field: 'packageDate',
      title: '装箱日期',
      width: 120,
      formatter: 'formatDate',
    },
    {
      field: 'salesOrderCode',
      title: '销售订单编号',
      minWidth: 140,
    },
    {
      field: 'invoiceCode',
      title: '发票编号',
      minWidth: 120,
    },
    {
      field: 'clientName',
      title: '客户名称',
      minWidth: 120,
    },
    {
      field: 'length',
      title: '箱长度',
      width: 80,
    },
    {
      field: 'width',
      title: '箱宽度',
      width: 80,
    },
    {
      field: 'height',
      title: '箱高度',
      width: 80,
    },
    {
      field: 'sizeUnitName',
      title: '尺寸单位',
      width: 90,
    },
    {
      field: 'netWeight',
      title: '净重',
      width: 80,
    },
    {
      field: 'grossWeight',
      title: '毛重',
      width: 80,
    },
    {
      field: 'weightUnitName',
      title: '重量单位',
      width: 90,
    },
    {
      field: 'inspectorName',
      title: '检查员',
      minWidth: 100,
    },
    {
      field: 'status',
      title: '单据状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_WM_PACKAGE_STATUS },
      },
    },
    {
      title: '操作',
      width: 160,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 子箱列表的字段 */
export function useSubPackageGridColumns(): VxeTableGridOptions<MesWmPackageApi.Package>['columns'] {
  return [
    {
      field: 'code',
      title: '装箱单编号',
      minWidth: 160,
      fixed: 'left',
    },
    {
      field: 'packageDate',
      title: '装箱日期',
      width: 120,
      formatter: 'formatDate',
    },
    {
      field: 'salesOrderCode',
      title: '销售订单编号',
      minWidth: 140,
    },
    {
      field: 'invoiceCode',
      title: '发票编号',
      minWidth: 120,
    },
    {
      field: 'clientName',
      title: '客户名称',
      minWidth: 120,
    },
    {
      field: 'netWeight',
      title: '净重',
      width: 80,
    },
    {
      field: 'grossWeight',
      title: '毛重',
      width: 80,
    },
    {
      field: 'weightUnitName',
      title: '重量单位',
      width: 90,
    },
    {
      field: 'inspectorName',
      title: '检查员',
      minWidth: 100,
    },
    {
      field: 'status',
      title: '单据状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_WM_PACKAGE_STATUS },
      },
    },
    {
      title: '操作',
      width: 100,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 组装尾箱预览文案：整除时显示每箱数量并标记满装 */
function buildTailPreview(quantity?: number, boxQuantity?: number): string {
  if (!quantity || quantity <= 0 || !boxQuantity || boxQuantity <= 0) {
    return '输入装箱数量后自动计算';
  }
  const boxCount = Math.ceil(quantity / boxQuantity - 1e-9);
  const tail = Number((quantity - (boxCount - 1) * boxQuantity).toFixed(2));
  const full = Math.abs(tail - boxQuantity) < 1e-6;
  return `预计装 ${boxCount} 箱，尾箱 ${full ? boxQuantity : tail} 件${
    full ? '（满装）' : ''
  }`;
}

/** 刷新装箱预览（按表单中的装箱数量与每箱数量实时计算） */
export async function refreshTailPreview(formApi?: VbenFormApi) {
  if (!formApi) {
    return;
  }
  const values = (await formApi.getValues()) as {
    boxQuantity?: number;
    quantity?: number;
  };
  await formApi.setFieldValue(
    'tailPreview',
    buildTailPreview(values.quantity, values.boxQuantity),
  );
}

/** 装箱明细的表单 */
export function useLineFormSchema(formApi?: VbenFormApi): VbenFormSchema[] {
  return [
    {
      fieldName: 'workOrderId',
      label: '生产订单',
      component: markRaw(ProWorkOrderSelect),
      componentProps: {
        placeholder: '请选择生产订单',
        // 选择生产订单后，自动带出该订单的产品物料
        onChange: async (order?: { productId?: number }) => {
          await formApi?.setFieldValue('itemId', order?.productId);
          // 带出产品物料的每箱数量，用于计算尾箱
          const item = order?.productId
            ? await getItem(order.productId).catch(() => undefined)
            : undefined;
          await formApi?.setFieldValue('boxQuantity', item?.boxQuantity);
          await refreshTailPreview(formApi);
        },
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'itemId',
      label: '产品物料',
      component: markRaw(MdItemSelect),
      componentProps: {
        disabled: true,
        placeholder: '选择生产订单后自动带出',
      },
      rules: 'selectRequired',
      help: '自动取生产订单的产品，无需手动选择。',
    },
    {
      fieldName: 'boxQuantity',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'quantity',
      label: '装箱数量',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0.01,
        placeholder: '请输入装箱数量',
        precision: 2,
        onChange: () => {
          refreshTailPreview(formApi);
        },
      },
      rules: 'required',
    },
    {
      fieldName: 'tailPreview',
      label: '装箱预览',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        disabled: true,
        placeholder: '输入装箱数量后自动计算',
      },
    },
    {
      fieldName: 'expireDate',
      label: '有效期',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        placeholder: '请选择有效期',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入备注',
        rows: 3,
      },
    },
  ];
}

/** 装箱明细列表的字段 */
export function useLineGridColumns(): VxeTableGridOptions<MesWmPackageLineApi.PackageLine>['columns'] {
  return [
    {
      field: 'itemCode',
      title: '产品物料编码',
      minWidth: 120,
    },
    {
      field: 'itemName',
      title: '产品物料名称',
      minWidth: 140,
    },
    {
      field: 'specification',
      title: '规格型号',
      minWidth: 120,
    },
    {
      field: 'unitMeasureName',
      title: '单位',
      width: 80,
    },
    {
      field: 'quantity',
      title: '装箱数量',
      width: 100,
    },
    {
      field: 'boxCount',
      title: '箱数',
      width: 80,
    },
    {
      field: 'tailBoxQuantity',
      title: '尾箱数量',
      width: 110,
      slots: { default: 'tailBoxQuantity' },
    },
    {
      field: 'workOrderCode',
      title: '生产订单编号',
      minWidth: 140,
    },
    {
      field: 'batchCode',
      title: '批次号',
      minWidth: 120,
    },
    {
      field: 'expireDate',
      title: '有效期',
      width: 120,
      formatter: 'formatDate',
    },
    {
      title: '操作',
      width: 120,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 选择弹窗的搜索表单 */
export function useSelectGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '装箱单编号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入装箱单编号',
      },
    },
    {
      fieldName: 'salesOrderCode',
      label: '销售订单编号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入销售订单编号',
      },
    },
    {
      fieldName: 'clientId',
      label: '客户',
      component: markRaw(MdClientSelect),
      componentProps: {
        placeholder: '请选择客户',
      },
    },
    {
      fieldName: 'inspectorUserId',
      label: '检查员',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        placeholder: '请选择检查员',
        valueField: 'id',
      },
    },
  ];
}

/** 选择弹窗的字段 */
export function useSelectGridColumns(
  multiple = true,
): VxeTableGridOptions<MesWmPackageApi.Package>['columns'] {
  return [
    {
      type: multiple ? 'checkbox' : 'radio',
      width: 50,
    },
    {
      field: 'code',
      title: '装箱单编号',
      minWidth: 160,
    },
    {
      field: 'packageDate',
      title: '装箱日期',
      width: 120,
      formatter: 'formatDate',
    },
    {
      field: 'salesOrderCode',
      title: '销售订单编号',
      minWidth: 140,
    },
    {
      field: 'invoiceCode',
      title: '发票编号',
      minWidth: 120,
    },
    {
      field: 'clientName',
      title: '客户名称',
      minWidth: 120,
    },
    {
      field: 'netWeight',
      title: '净重',
      width: 80,
    },
    {
      field: 'grossWeight',
      title: '毛重',
      width: 80,
    },
    {
      field: 'weightUnitName',
      title: '重量单位',
      width: 90,
    },
    {
      field: 'inspectorName',
      title: '检查员',
      minWidth: 100,
    },
    {
      field: 'status',
      title: '单据状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_WM_PACKAGE_STATUS },
      },
    },
  ];
}
