// 自定义码单

import type { adjustType } from '@/common/enum'
import type { CompanyItem } from '@/pages/codeSetting/components/payeeHead'

export interface customPrintProductColor {
  price: number
  product_color_id: number
  product_color_name: string
  weight: number
  weight_error: number
}

export interface customPrintProduct {
  custom_print_product_color: customPrintProductColor[]
  price: number
  product_id: number
  product_name: string
  weight: number
  weight_error: number
}
export interface adminParam {
  adjust_type: number
  custom_print_id: number
  sale_order_id: number
  custom_print_product: customPrintProduct[]
  price: number
  purchaser_form_title_id: number
  weight: number
  weight_error: number
}
export interface CodeParam {
  data: any // 码单数据
  adjust_type: adjustType// 码单调整类型
  custom_print_id: number // 自定义码单id
  sale_order_id: number // 销售码单id
  admin_data: adminParam|null // 需要传给后端的数据
  color_item_data: any // 当前编辑的颜色数据
  update_status: 0|1|2
  purchaser_form_title: CompanyItem // 码单抬头
}

type Action = {
  type: string
}&CodeParam

const INIT_USER: CodeParam = {
  data: null, // 码单数据
  adjust_type: 1, // 码单调整类型
  custom_print_id: 0, // 自定义码单id
  sale_order_id: 0, // 销售码单id
  admin_data: null, // 需要传给后端的数据
  color_item_data: null,
  update_status: 0, // 1代表修改了数量，2代表修改了名称
  purchaser_form_title: {
    title: '',
    purchaser_name: '',
    phone: '',
    id: 0,
  },
}

export default function counter(state = INIT_USER, action: Action) {
  const { type, data, admin_data } = action
  switch (type) {
    case 'setInitData':
      return onInitData(state, action)
    case 'updateData':
      return onUpdateData(state, action)
    case 'changeAdjustType':
      return { ...state, adjust_type: data }
    case 'setColorData':
      return { ...state, color_item_data: JSON.parse(JSON.stringify(data)) }
    case 'updateColorData':
      return onUpdateColorData(state, action)
    case 'setPurchaserFormTitle':
      return onUpdatePurchaserFormTitle(state, action)
    default:
      return state
  }
}

// 初始化数据
const onInitData = (state, action: Action) => {
  const { data = {}, custom_print_id, sale_order_id, adjust_type } = action
  const updateData: adminParam = {
    adjust_type: parseInt(adjust_type.toString()),
    custom_print_product: [],
    price: data?.price_admin || 0,
    weight: data?.weight_admin || 0,
    weight_error: data?.weight_error_admin || 0,
    custom_print_id: parseInt(custom_print_id.toString()),
    sale_order_id: parseInt(custom_print_id.toString()),
    purchaser_form_title_id: data?.default_title_id,
  }
  const custom_order_total = {
    total_weight_error: data?.total_weight_error,
    total_sale_weight: data?.total_sale_weight,
    total_amount: data?.total_amount,
  }
  data.weight_admin = 0
  data.price_admin = 0
  data.weight_error_admin = 0
  data?.product_details?.map((item, index) => {
    item.weight_admin = 0
    item.price_admin = 0
    item.weight_error_admin = 0
    item.index_str = index.toString()
    item.sale_mode_name = data.sale_mode_name
    item.sale_mode = data.sale_mode
    const product_item: customPrintProduct = {
      price: item.price_admin || 0,
      weight: item.weight_admin || 0,
      weight_error: item.weight_error_admin || 0,
      product_id: item.product_id,
      product_name: item.product_name,
      custom_print_product_color: [],
    }
    item?.product_color_details?.map((citem, cindex) => {
      citem.index_str = `${item.index_str}_${cindex}` // 通过这个可以快速定位数组位置
      citem.weight_setting = citem.weight
      citem.weight_error_setting = citem.weight_error
      citem.price_setting = citem.price
      citem.weight_admin = 0
      citem.price_admin = 0
      citem.weight_error_admin = 0
      citem.sale_mode_name = data.sale_mode_name
      citem.sale_mode = data.sale_mode
      product_item.custom_print_product_color.push({
        price: citem.price_admin || 0,
        weight: citem.weight_admin || 0,
        weight_error: citem.weight_error_admin || 0,
        product_color_id: citem.product_color_id,
        product_color_name: citem.product_color_name,
      })
    })
    updateData.custom_print_product.push(product_item)
  })
  // 抬头
  const purchaser_form_title = {
    id: data?.default_title_id,
    title: data?.title_name,
    purchaser_name: data?.purchaser_name,
    phone: data?.purchaser_phone,
  }
  return {
    ...state,
    data,
    custom_print_id,
    sale_order_id,
    custom_order_total,
    admin_data: updateData,
    init_state: true,
    update_status: 0,
    color_item_data: null,
    purchaser_form_title,
    adjust_type,
  }
}

// 后端所需数据
const onUpdateData = (state, action) => {
  const { data, update_status } = action
  const updateData: adminParam = {
    adjust_type: parseInt(state.adjust_type),
    custom_print_product: [],
    price: 0,
    weight: 0,
    weight_error: 0,
    custom_print_id: parseInt(state.custom_print_id!),
    sale_order_id: parseInt(state.sale_order_id!),
    purchaser_form_title_id: parseInt(state.purchaser_form_title.id),
  }
  updateData.weight = data?.weight_admin || 0
  updateData.price = data?.price_admin || 0
  updateData.weight_error = data?.weight_error_admin || 0
  data?.product_details?.map((item) => {
    const product_item: customPrintProduct = {
      price: item.price_admin || 0,
      weight: item.weight_admin || 0,
      weight_error: item.weight_error_admin || 0,
      product_id: item.product_id,
      product_name: item.product_name,
      custom_print_product_color: [],
    }
    item?.product_color_details?.map((citem) => {
      product_item.custom_print_product_color.push({
        price: citem.price_admin || 0,
        weight: citem.weight_admin || 0,
        weight_error: citem.weight_error_admin || 0,
        product_color_id: citem.product_color_id,
        product_color_name: citem.product_color_name,
      })
    })
    updateData.custom_print_product.push(product_item)
  })
  return { ...state, admin_data: updateData, data: { ...data }, update_status }
}

// 更新码单抬头
const onUpdatePurchaserFormTitle = (state, action) => {
  const { data } = action
  state.admin_data.purchaser_form_title_id = data.id
  return { ...state, purchaser_form_title: data }
}

// 修改颜色数据
const onUpdateColorData = (state, action: Action) => {
  const { data, update_status } = action
  return { ...state, color_item_data: data, update_status }
}
