import { h } from 'vue';
import { NFlex, NImage, NTag } from 'naive-ui';
import { fetchGetAllStoreList } from '@/service/api/goods/desk-category';
import { fetchGetDevList } from '@/service/api/delivery/normal-orde';
import type { FormSchema } from '@/components/zt/Form/types/form';

export const SearchForm: FormSchema[] = [
  {
    label: '门店名称',
    component: 'ApiSelect',
    field: 'shopId',
    componentProps: {
      api: fetchGetAllStoreList,
      labelFeild: 'shopName',
      valueFeild: 'shopId'
    }
  },
  {
    label: '订单编号',
    component: 'NInput',
    field: 'orderNumber'
  },
  {
    label: '收货人姓名',
    component: 'NInput',
    field: 'receiver'
  },
  {
    label: '收货人手机号',
    component: 'NInput',
    field: 'userMobile'
  },
  {
    label: '售后状态',
    component: 'NSelect',
    field: 'refundStatus',
    componentProps: {
      options: [
        {
          label: '申请退款',
          value: 1
        },
        {
          label: '退款成功',
          value: 2
        },
        {
          label: '部分退款成功',
          value: 3
        },
        {
          label: '退款失败',
          value: 4
        }
      ]
    }
  },
  {
    label: '下单时间',
    component: 'NDatePicker',
    field: 'createTime',
    componentProps: {
      type: 'datetimerange'
    }
  }
];

export enum orderStatusEnum {
  /**
   *
   *  待支付
   */
  WAIT_PAY = 0,
  /**
   * 待发货
   */
  WAIT_DELIVERY = 1,
  /**
   * 订单已接单(待拣货)
   */
  ORDER_ACCEPT = 20,
  /**
   * 订单待配送（拣货完成/自提类订单为待自提）
   */
  ORDER_WAIT_DELIVERY = 30,
  /**
   * 订单配送中
   */
  ORDER_DELIVERY = 40,
  /**
   * 订单取消待审核
   */
  ORDER_CANCEL_WAIT_AUDIT = 50,
  /**
   * 订单已取消
   */
  ORDER_CANCEL = 60,
  /**
   * 订单已送达
   */
  ORDER_ARRIVE = 70,
  /**
   * 订单已完成
   */
  ORDER_COMPLETE = 80
}

/**
 *  // 0-待支付 1-待发货，20-订单已接单(待拣货),30-订单待配送（拣货完成/自提类订单为待自提）,40-订单配送中
        // ,50-订单取消待审核,60-订单已取消,70-订单已送达,80-订单已完成
 */
export const orderStatus = {
  [orderStatusEnum.WAIT_PAY]: '待支付',
  [orderStatusEnum.WAIT_DELIVERY]: '待发货',
  [orderStatusEnum.ORDER_ACCEPT]: '订单已接单(待拣货)',
  [orderStatusEnum.ORDER_WAIT_DELIVERY]: '订单待配送（拣货完成/自提类订单为待自提）',
  [orderStatusEnum.ORDER_DELIVERY]: '订单配送中',
  [orderStatusEnum.ORDER_CANCEL_WAIT_AUDIT]: '订单取消待审核',
  [orderStatusEnum.ORDER_CANCEL]: '订单已取消',
  [orderStatusEnum.ORDER_ARRIVE]: '订单已送达',
  [orderStatusEnum.ORDER_COMPLETE]: '订单已完成'
};

/**
 // 1:申请退款 2:退款成功 3:部分退款成功 4:退款失败
 *
 */

export const refundStatus = {
  1: '申请退款',
  2: '退款成功',
  3: '部分退款成功',
  4: '退款失败'
};
/**
 *
 * 配送方式
 */

export const dvyStatus = {
  1: '快递',
  2: '自提',
  3: '及时配送'
};

export const closeStatus = {
  1: '超时未支付',
  2: '退款关闭',
  3: '买家取消',
  4: '已通过货到付款交易'
};

export const orderColumns: NaiveUI.TableColumn<Api.delivery.OrderItemElement>[] = [
  {
    title: '商品',
    key: 'goods',
    width: 300,
    render: row => {
      const goodsNodes = [
        h(NImage, { src: row.pic, width: 80, height: 80 }),
        h('div', { class: 'ml-2 ' }, [
          h('div', { class: 'text-15px font-semibold' }, row.skuName),
          h('div', { class: 'text-gray' }, `规格：${row.spec || '--'}`)
        ])
      ];
      return h('div', { class: 'flex items-center' }, goodsNodes);
    }
  },
  {
    title: '单价（元）',
    key: 'price',
    width: 100
  },
  {
    title: '数量',
    key: 'prodCount',
    width: 250,
    render: row => {
      const nodes = [h('div', { class: 'mr-2' }, row.prodCount)];
      if (row.refundSuccessCount) {
        nodes.push(h(NTag, { class: 'ml2', type: 'success' }, () => `退款成功：${row.refundSuccessCount}`));
      }
      if (row.refundIngCount && !row.refundSuccessCount) {
        nodes.push(h(NTag, { class: 'ml2', type: 'error' }, () => `售后处理：${row.refundIngCount}`));
      }
      return h(NFlex, { align: 'center' }, () => nodes);
    }
  },
  {
    title: '小计/元',
    key: 'productTotalAmount',
    width: 100
  }
];

export const deliveryInfo: FormSchema[] = [
  {
    label: '配送方式',
    component: 'NGradientText',
    field: 'dvyMethod',
    render() {
      return h('div', {}, '快递');
    }
  },
  {
    label: '收货人姓名',
    component: 'NGradientText',
    field: 'receiver',
    render({ model, field }) {
      return h('div', {}, model[field]);
    }
  },
  {
    label: '收货人手机号',
    component: 'NInput',
    field: 'mobile',
    render({ model, field }) {
      return h('div', {}, model[field]);
    }
  },
  {
    label: '收货地址',
    component: 'NInput',
    field: 'address',

    render({ model, field }) {
      return h('div', {}, model[field]);
    }
  },
  {
    label: '发货方式',
    component: 'NRadioGroup',
    field: 'dvyType',
    componentProps: {
      options: [
        {
          label: '自己联系快递',
          value: 1
        },
        {
          label: '无需发货',
          value: 2
        }
      ]
    },
    defaultValue: 1,
    required: true
  },
  {
    label: '快递公司',
    component: 'ApiSelect',
    field: 'dvyId',
    componentProps: {
      api: fetchGetDevList,
      params: {
        current: 1,
        size: 100
      },
      labelFeild: 'dvyName',
      valueFeild: 'dvyId'
    },
    required: true,
    ifShow({ model }) {
      return model.dvyType === 1;
    }
  },
  {
    label: '快递单号',
    component: 'NInput',
    field: 'dvyFlowId',
    required: true,
    ifShow({ model }) {
      return model.dvyType === 1;
    }
  }
];
