
import React from 'react';
import FormPage from '../../components/FormPage'
import { ConfirmButton } from '@woowalker/feui'
import {message} from "antd/lib/index";

//MRP运算
export default {
  btnBatchMrp:({record,close,loadData,custom,getSelectedRecords,dispatch})=> {
    const closeF=()=>{
      close();
      loadData();
    };
    const regClick=()=>{
      var selectRecords=getSelectedRecords();
      let isError=false;
      let ids=[];
      if(selectRecords){
        let productId='';
        selectRecords.map(item=>{
          if(item.MRP_STATUS==2)return;
          if(productId=='')
          {
            productId= item.PRODUCT_ID.toString().toUpperCase();
          }
          else if(productId!= item.PRODUCT_ID.toString().toUpperCase())
          {
            message.error('请选择相同的产品进行MRP');
            isError=true;
          }
          ids.push(item.ID);
        });
      };
      if(!ids||ids.length==0){
        message.error('请选择有效的销售订单明细记录');
        isError=true;
      }
      else if(!isError&&ids){
        dispatch({
          type: 'custom/save',
          payload: {
            ['mrpByProposalClick'+(record? record.ID:'')]:true,
            mrpByProposalSelectIds:ids.join(',')
          },
        });
      }
      return {
        isReturn:isError
      }
    };
    const data={
      id:(record? record.ID:''),
      close:closeF,
      mrpByProposalClick:custom.mrpByProposalClick,
      ids:custom.mrpByProposalSelectIds,
    };
    return {
      content: <FormPage formCode={'SO010'} data={data}/>,
      click: regClick,
    }
  },
  btnMrp:({record,close,loadData,custom,getSelectedRecords,dispatch})=> {
    const isReturn=record.MRP_STATUS==2;
    const closeF=()=>{
      close();
      loadData();
    };
    const regClick= async function () {
      await dispatch({
        type: 'custom/save',
        payload: {
          ['mrpByProposalClick'+(record? record.ID:'')]: true,
          mrpByProposalSelectIds: record.ID,
        },
      });
    };
    // const regClick= async function (cb) {
    //   await cb();
    //   dispatch({
    //     type: 'custom/save',
    //     payload: {
    //       mrpByProposalClick: true,
    //       mrpByProposalSelectIds: record.ID,
    //     },
    //   });
    // };
    const data={
      id: record ? record.ID : '',
      close: closeF,
      mrpByProposalClick: custom.mrpByProposalClick,
      ids: custom.mrpByProposalSelectIds,
    };
    return {
      content: <FormPage formCode={'SO010'} data={data}/>,
      click: regClick,
      isReturn
    }
  },
  checkRowClass:({record})=>{
    return record.VIR_HAS_PRODUCT_PROPOSAL?null:'danger';
  },
  colRemainMrpQty:({record})=>{
    return record.PLAN_QTY- record.MRPED_QTY;
  },
  btnComfirmMrpComplete:({record,close,loadData,login,btn,dispatch})=> {
    let enable=record.MRP_STATUS!==0&&record.MRP_STATUS!==1;
    const confirm=()=>{
      if(enable)return;
      const json={Keyword:record.ID};
      dispatch({
        type: 'SOOrder/confirmMrpComplete',
        payload:json,
        onComplete: (ret) => {
          if(ret) {
            message.success('执行成功');
            loadData();
          }
        }
      })
    };


    return  {
      content:<ConfirmButton isConfirm={true} confirmTitle="是否确定指定完成?" onClick={confirm} key={record ? (record.ID + btn.ID) : btn.ID}
                     type={btn.CSS} icon={btn.ICON}   disabled={enable} size={record?'small':null} title={btn.LABEL}/>
    };
  },

}
