
import { Button, Popconfirm,Input,InputNumber,Icon } from 'antd';
import ListPage from '../../components/Table/ListPage';
import FormPage from '../../components/FormPage'
import {extendRule, initFilter, extendFilterGroup,  initFilterGroup,  extendGroupRule} from "../../utils/common";
import {message} from "antd/lib/index";
import moment from 'moment';
import SelectInventoryPage from '../../components/CustomPages/WH/SelectInventoryPage';

//订单发货
export default {
  btnPackageProduct:({record,close,loadData,login,getSelectedRecords,dispatch})=> {//包装
    const selectedRecords = getSelectedRecords()
    const saveRecords = [];
    if (selectedRecords && selectedRecords.length > 0) {
      let isError = false;
      selectedRecords.forEach((n, i) => {
        if (n.DELIVERY_STATUS == 2) return;
        n.SaleOrderDetailID = n.ID;
        if (!n.BoxQty) {
          isError = true;
          message.error('请输入销售订单' + (n.Nav_Order == null ? '' : n.Nav_Order.CODE) + '的包装数量');
        }
        else if (!n.PerQty) {
          isError = true;
          message.error('请输入销售订单' + (n.Nav_Order == null ? '' : n.Nav_Order.CODE) + '的每箱数量');
        }
        else if (n.BoxQty != n.PerQty * n.BoxNumber) {
          isError = true;
          message.error('请输入销售订单' + (n.Nav_Order == null ? '' : n.Nav_Order.CODE) + '的包装数量不等于每箱数量*箱数');
        }
        saveRecords.push(n);
      });
      if (isError) return;
    }
    if (saveRecords == null || saveRecords.length == 0) {
      message.error('请选择记录');
      return;
    }

    const json = {
      OrgId: login.OrgId,
      UserId: login.userId,
      Nav_Details: saveRecords,
    };
    dispatch({
      type: 'WHPackageProduct/packageProductBySaleOrder',
      payload: json,
      onComplete: (ret) => {
        if (ret) {
          message.success('执行成功');
          loadData();
        }
      }
    });
  },
  btnBoxPrint:({record,close,loadData,login,getSelectedRecords,dispatch})=> {//打印
    const json=initFilter(login.OrgId,record.CODE,'',0,1,login.userId,'',record.PrintRemark);
    dispatch({
      type: 'WHPackageProduct/printBoxBySaleOrder',
      payload:json,
      onComplete: (ret) => {
        if(ret) {
          message.success('执行成功');
          loadData();
        }
      }
    })
  },
  onLoadParam:({pageCode,parantRow,loadParam})=>{
    if(pageCode=='boxs'){
      loadParam['Keyword']=parantRow.ID;
    }
    else if(pageCode=='saleOrder'){
      let isDeliveryStatus=false;
      for(var key in loadParam){
        if(key.indexOf('Field')>-1){
          var v=loadParam[key];
          if(v=='DELIVERY_STATUS'){
            isDeliveryStatus=true;
            break;
          }
        }
      }
      if(!isDeliveryStatus){
        var group=initFilterGroup(false);
        extendGroupRule(group,'DELIVERY_STATUS',1,0);
        extendGroupRule(group,'DELIVERY_STATUS',1,1);
        extendFilterGroup(loadParam,group);
      }
    }
  },
  btnBoxCancel:({record,close,loadData,login,getSelectedRecords,dispatch,btn})=> {//撤销
    const confirm=()=>{
      const json=initFilter(login.OrgId,record.CODE);
      dispatch({
        type: 'WHPackageProduct/cancelPackageProductBySaleOrder',
        payload:json,
        onComplete: (ret) => {
          if(ret) {
            message.success('执行成功');
            loadData();
          }
        }
      })
    };
    return {
      content:  <Popconfirm title="是否确定撤销?" onConfirm={confirm}>
        <Button type={'danger'}>{btn.LABEL}</Button>
      </Popconfirm>
    };
  },
  colIsDelay:({record,loadData,getLoadParam,login,btn,dispatch})=>{
    //return '否'

    const deliveryTime=(record.DELIVERY_TIME? new moment(record.DELIVERY_TIME):new moment()).format("YYYY-MM-DD");
    const planDeliveryTime=new moment(record.PLAN_DELIVERY_TMIE).format("YYYY-MM-DD");
    if(deliveryTime==planDeliveryTime)return'否';
    return moment(deliveryTime).isBefore(planDeliveryTime)?'否':'是';


  },
  colBoxQty:({record,loadData,getLoadParam,login,btn,dispatch,onRefreshTableData, width})=>{
    if(record.DELIVERY_STATUS==2)return;
    const data={
      onSave:function (data) {
        const tmp=[];
        let boxQty=0;
        if(data&&data.length){
          data.forEach((n,i)=>{
            if(n.UsedQty&&n.UsedQty>0) {
              tmp.push({InventoryId: n.InventoryId, Qty: n.UsedQty});
              boxQty+=n.UsedQty;
            }
          });
        }
        record.Nav_Inventorys=tmp;
        record.BoxQty=boxQty;
        record.PerQty=boxQty;
        onRefreshTableData();
      },
      onInventorys:()=>{
        return record.Nav_Inventorys;
      },
      productId:record.PRODUCT_ID,
      soOrderDetailId:record.ID,
      soDetailRecord:record,
  };



    return <Input style={{ width: width > 0 && width }} value={record.BoxQty} disabled={true} addonAfter={<SelectInventoryPage Nav_Product={record.Nav_Product} data={data} >
      <Icon type={'plus-square'}></Icon>
    </SelectInventoryPage>}/>
  },
  colBoxNumber:({record,loadData,getLoadParam,login,btn,dispatch,onRefreshTableData, width})=>{
    if(record.DELIVERY_STATUS==2)return;
    if(!record.BoxNumber){
      record.BoxNumber=1;//默认为1张
    }
    const onChange=(e)=>{
      record.BoxNumber=e;
      if(record.BoxQty&&record.BoxNumber){
        record.PerQty=record.BoxQty/ record.BoxNumber;
      }
      onRefreshTableData();
    };
    return  <InputNumber style={{ width: width > 0 && width }} value={record.BoxNumber} min={0} onChange={(e) => onChange(e)}/>
  },
  colPerQty:({record,loadData,getLoadParam,login,btn,dispatch,onRefreshTableData, width})=>{
    if(record.DELIVERY_STATUS==2)return;
    const onChange=(e)=>{
      record.PerQty=e;
      if(record.BoxQty&&record.PerQty){
        record.BoxNumber=record.BoxQty/ record.PerQty;
      }
      onRefreshTableData();
    };
   return  <InputNumber style={{ width: width > 0 && width }} value={record.PerQty} min={0} onChange={(e) => onChange(e)}/>
  },
  colRemark:({record,loadData,getLoadParam,login,btn,dispatch,onRefreshTableData, width})=>{
    if(record.DELIVERY_STATUS==2)return;
    const onChange=(e)=>{
      record.PrintRemark=e.target.value;
      onRefreshTableData();
    }
    return <Input style={{ width: width > 0 && width }} value={record.PrintRemark} onChange={onChange} />
  },
  colBoxDetailSumQty:({record,loadData,getLoadParam,login,btn,dispatch})=>{
    if(record.Nav_Details){
      let sumQty=0;
      record.Nav_Details.forEach((n,i)=>{
        sumQty+=n.QTY;
      });
      return sumQty;
    }
  },
  colPrintBoxRemark:({record,loadData,getLoadParam,login,btn,dispatch,onRefreshTableData, width})=>{
    const onChange=(e)=>{
      record.PrintRemark=e.target.value;
      onRefreshTableData();
    }
    return <Input style={{ width: width > 0 && width }} value={record.PrintRemark} onChange={onChange} />
  },
}
