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

//MRP单编辑
export default {
  btnEdit:({record,close,loadData,custom,btn,dispatch})=> {
    const regClick=async function (){
      await dispatch({
        type: 'custom/save',
        payload: {
          ['showMrpByProposalClick'+(record? record.ID:'')]:true
        },
      });
    };
    const data={
      id:(record? record.ID:''),
      showMrpByProposalClick:custom.showMrpByProposalClick,
    };

    return {
      content: <FormPage formCode={'SO012'} data={data}/>,
      click: regClick,
    }
  },
  btnCancel:({record,close,loadData,login,btn,dispatch})=> {
    let isReturn=record.CONFIRM_STATUS==1||record.ORDER_STATUS==1;
    const confirm=()=>{
      //dispatch()
      const json={Keyword:record.ID};
      dispatch({
        type: 'SOMrpEdit/cancelMrp',
        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} title={btn.LABEL} size="small"  disabled={isReturn} />,
    };
  },
  btnMrpConfirm:({record,close,loadData,login,btn,dispatch})=> {
    let enable=record.CONFIRM_STATUS==0&&record.IS_CONFIRM_ENABLE;
    const confirm=()=>{
      const json={Keyword:record.ID};
      dispatch({
        type: 'SOMrpEdit/mrpConfirm',
        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} title={btn.LABEL} size="small" disabled={!enable} />,
    };
  },
  btnBatchConfirm:({record,loadData,login,dispatch,btn,getSelectedRowKeys,clearSelectedRowKeys, getSelectedRecords})=> {
    const selectRecords=getSelectedRecords();
    const selectedRowKeys=getSelectedRowKeys();
    let enable=true;
    const confirm=()=>{
      const tmpSelectedRowKeys=[];
      if(selectRecords){
        selectRecords.forEach(t=>{
          if(t.CONFIRM_STATUS !=0 || !t.IS_CONFIRM_ENABLE){
            message.error(t.CODE+ '该MRP单号无法执行,请核实！');
            enable=false;
            return;
          }
          tmpSelectedRowKeys.push(t.ID);
        });
        if(!enable)
          return;
      }
      if(tmpSelectedRowKeys==null||tmpSelectedRowKeys.length==0){
        message.error('请选择MRP记录');
        return;
      }
      const json={Keyword:tmpSelectedRowKeys.join(',')};
      dispatch({
        type: 'SOMrpEdit/mrpConfirmWorkByEmOrder',
        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={!selectedRowKeys.length} title={btn.LABEL}/>
    };
  },
  btnMrpConfirmByEmOrder:({record,close,loadData,login,btn,dispatch})=> {
    let enable=record.CONFIRM_STATUS==0&&record.IS_CONFIRM_ENABLE;
    const confirm=()=>{
      if(!enable)return;
      const json={Keyword:record.ID};
      dispatch({
        type: 'SOMrpEdit/mrpConfirmWorkByEmOrder',
        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} title={btn.LABEL} size="small"/>
    };
  },
  colFeedQty:({record})=>{
    if(record.Nav_EmProposals){
      let r='';
      record.Nav_EmProposals.sort((x,y)=>{x.PROPOSAL_TYPE>y.PROPOSAL_TYPE?1:-1});
      record.Nav_EmProposals.forEach((n,i)=>{
        if(n.PROPOSAL_TYPE==1){
          r+=('A料:'+n.FEED_QTY);
        }
        else if(n.PROPOSAL_TYPE==1){
          r+=('B料:'+n.FEED_QTY);
        }
      });
      return r;
    }
  }


}
