/**
 * 看单交接模块
 */
import { Module, VuexModule, Mutation, Action, getModule } from 'vuex-module-decorators';
import store from '@/store';
import http from '@/utils/request';
import { url } from '@/utils/url';
import { session, User } from '@/store/modules/session';
import { pick } from 'lodash/fp';

export interface Order {
  poNo?: any;
  poLine?: any;
  orderOutId?: any;
  rawOrderInId?: any;
  hideKdOrderNo?: any;
  kdOrderNo: string;
  orderNo?: any;
  mrpCode: string;
  materialCode: string;
  wkposCode?: any;
  materialDesc?: any;
  amount?: any;
  factoryCode?: any;
  sapFactoryCode?: any;
  sendSpot: string;
  totalMoAmount?: any;
  totalHvAmount?: any;
  totalBackAmount?: any;
  totalFillAmount?: any;
  totalDisuseAmount?: any;
  totalVirtualOrderAmount?: any;
  unit: string;
  receiptAmount?: any;
  checkCode: string;
  checkResult: string;
  allAmount: number;
  firstHvDate?: any;
  prdOrder?: any;
  serialNo?: any;
  whCode?: any;
  requestAmount?: any;
  orderStatus?: any;
  accountingStatus?: any;
  lastModifiedBy?: any;
  groupId?: any;
  groupLine?: any;
  proOrder: string;
  availableAmount: number;
  poVoucher?: any;
  poType?: any;
  supplyCode?: any;
  sapText?: any;
  userDefined10?: any;
  schema?: any;
  olockFlag?: any;
  ilockFlag?: any;
}

export interface OrderDetail {
  materialCode: string;
  allAmount: string;
  amount: string;
  factoryCode: string;
  sendSpot: string;
  materialDesc: string;
  unit: string;
  wkposCode: string;
  checkCode: string;
  loginName: string;
  firstHvDate: string;
  kdOrderNo: string;
  mrpCode: string;
  orderNo: string;
  orderOutId: string;
  prdOrder: string;
  requestAmount: string;
  serialNo: string;
  totalHvAmount: string;
  whCode: string;
  checkResult: string;
  poNo: string;
  poLine: string;
  sapFactoryCode: string;
}

@Module({
  dynamic: true,
  store,
  namespaced: true,
  name: 'raw.handover.kanDan.order',
})
class OrderService extends VuexModule {
  orders: Array<Order> = [];

  @Action
  async takeOrder({
    hideKdOrderNo,
    factoryCode,
    loginName,
  }: {
    hideKdOrderNo: string;
    factoryCode: User['factoryCode'];
    loginName: User['loginName'];
  }): Promise<Order> {
    const r: any = await http.post(url.material.orderout.take, {
      hideKdOrderNo,
      factoryCode,
      loginName,
    });
    if (r.code !== '1') {
      throw new Error(r.msg);
    } else {
      return {
        ...r.list[0],
        orderDetails: [],
      };
    }
  }

  @Mutation
  add({ order }: { order: Order }) {
    this.orders = this.orders.concat([order]);
  }

  @Mutation
  clear() {
    this.orders = [];
  }

  @Action
  async getOrderDetails({
    list,
    factoryCode,
    loginName,
  }: {
    list: Array<Order['kdOrderNo']>;
    factoryCode: User['factoryCode'];
    loginName: User['loginName'];
  }): Promise<OrderDetail[]> {
    const r: any = await http.post(url.material.orderout.detail, {
      list,
      factoryCode,
      loginName,
    });
    if (r.code !== '1') {
      throw new Error(r.msg);
    } else {
      return r.list.map((d: any) => ({
        ...pick(
          [
            'materialCode',
            'allAmount',
            'factoryCode',
            'sendSpot',
            'materialDesc',
            'unit',
            'wkposCode',
            'checkCode',
            'kdOrderNo',
            'mrpCode',
            'checkResult',
            'poNo',
            'poLine',
            'sapFactoryCode',
          ],
          d,
        ),
        amount: '',
        loginName: session.loginName,
        firstHvDate: '',
        orderNo: '',
        orderOutId: '',
        prdOrder: '',
        requestAmount: '',
        serialNo: '',
        totalHvAmount: '',
        whCode: '',
      }));
    }
  }

  @Action
  async takeoutOrder({
    list,
    operatorPass,
    factoryCode,
    loginName,
  }: {
    list: Array<
      Pick<
        OrderDetail,
        | 'materialCode'
        | 'allAmount'
        | 'amount'
        | 'wkposCode'
        | 'materialDesc'
        | 'unit'
        | 'factoryCode'
        | 'sendSpot'
        | 'checkCode'
        | 'checkResult'
        | 'firstHvDate'
        | 'kdOrderNo'
        | 'mrpCode'
        | 'orderNo'
        | 'orderOutId'
        | 'prdOrder'
        | 'requestAmount'
        | 'serialNo'
        | 'totalHvAmount'
        | 'whCode'
        | 'poNo'
        | 'poLine'
        | 'sapFactoryCode'
      > & { loginName: User['loginName'] }
    >;
    operatorPass: string;
    factoryCode: string;
    loginName: string;
  }) {
    const r: any = await http.post(url.material.orderout.takeout, {
      list,
      operatorPass,
      factoryCode,
      loginName,
    });
    uni.showToast({ icon: 'none', title: r.msg });
    return r;
  }

  @Action
  async unlockOrder({
    list,
    factoryCode,
    loginName,
  }: {
    list: Array<
      Pick<
        OrderDetail,
        | 'materialCode'
        | 'allAmount'
        | 'amount'
        | 'wkposCode'
        | 'materialDesc'
        | 'unit'
        | 'factoryCode'
        | 'sendSpot'
        | 'checkCode'
        | 'checkResult'
        | 'firstHvDate'
        | 'kdOrderNo'
        | 'mrpCode'
        | 'orderNo'
        | 'orderOutId'
        | 'prdOrder'
        | 'requestAmount'
        | 'serialNo'
        | 'totalHvAmount'
        | 'whCode'
        | 'poNo'
        | 'poLine'
        | 'sapFactoryCode'
      > & { loginName: User['loginName'] }
    >;
    factoryCode: User['factoryCode'];
    loginName: User['loginName'];
  }) {
    const r: any = await http.post(url.material.orderout.unlock, {
      list,
      factoryCode,
      loginName,
    });
    if (r.code !== '1') {
      throw new Error(r.msg);
    } else {
      return r;
    }
  }
}

export const order = getModule(OrderService);
