
import React from 'react';
import { Button,Popconfirm } from 'antd'

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

//生产订单确认
export default {
  btnFlowCard: ({record,close,loadData,custom,btn,dispatch,formId}) => {
    const closeF=()=>{
      loadData();
    };
    const regClick=async function () {
      await dispatch({
        type: 'custom/save',
        payload: {
          ['emOrderFlowCardClick'+(record ? record.ID : '')]:true
        },
      });
    };
    const data = {
      id:(record ? record.ID : ''),
      close:closeF,
      getIds:()=>{return record.ID;},
      emOrderFlowCardClick:custom.emOrderFlowCardClick
    };
    return {
      content: <FormPage formCode={'EM013'} data={data}/>,
      click: regClick,
      close:closeF,
    }
  },
  btnCancel:({record,close,loadData,login,btn,dispatch})=> {
    let isReturn=record.EM_CANCEL_STATUS==1;
    const confirm=()=>{
      const json={Keyword:record.ID};
      dispatch({
        type: 'EMemOrder/cancelEmOrder',
        payload:json,
        onComplete: (ret) => {
          if(ret) {
            message.success('执行成功');
            loadData();
          }
        }
      })
    };
    return {
      content:<ConfirmButton isConfirm={true} confirmTitle="是否确定撤销?" onClick={confirm} type={btn.CSS} icon={btn.ICON} title={btn.LABEL} size="small"  disabled={isReturn}/>,
    };
  },
  btnConfirm:({record,close,loadData,login,btn,dispatch})=> {
    let isReturn=record.EM_CONFIRM_STATUS==1||record.EM_CANCEL_STATUS==1||record.ORDER_STATUS==3;
    const confirm=()=>{
      const json={Keyword:record.ID};
      dispatch({
        type: 'EMemOrder/confirmEmOrder',
        payload:json,
        onComplete: (ret) => {
          if(ret) {
            message.success('执行成功');
            loadData();
          }
        }
      })
    };
    return {
      content:<ConfirmButton isConfirm={true} confirmTitle="是否确定确认?" onClick={confirm} type={btn.CSS} icon={btn.ICON} title={btn.LABEL} size="small" disabled={isReturn}/>,
    };
  },
  btnEdit: ({record,close,loadData,custom,btn,dispatch,formId}) => {
    let isReturn=record.EM_CONFIRM_STATUS==1||record.EM_CANCEL_STATUS==1||record.ORDER_STATUS==3;
    const closeF=()=>{
      close();
      loadData();
    };
    const regClick=async function () {
      await dispatch({
        type: 'custom/save',
        payload: {
          emOrderEditClick:true
        },
      });
    };
    const data = {
      id:(record ? record.ID : ''),
      close:closeF,
      emOrderEditClick:custom.emOrderEditClick
    };
    return {
      content: <FormPage formCode={'EM012'} data={data}/>,
      click: regClick,
      isReturn:isReturn,
    }
  },
  btnFlowCardByBatch: ({record,close,loadData,custom,btn,dispatch,getSelectedRecords}) => {
    const closeF=()=>{
      loadData();
    };
    const getIds=()=>{
      var selectRecords=getSelectedRecords();
      let ids=[];
      if(selectRecords){
        selectRecords.forEach(item => {
          ids.push(item.ID);
        });
      }
      return ids.join(',');
    }
    const regClick=async function () {
      const ids = getIds();
      let isError = false;
      if (!ids) {
        message.error('请选择记录');
        isError = true;
      }
      else if (ids) {
        await dispatch({
          type: 'custom/save',
          payload: {
            ['emOrderFlowCardClick' + (record ? record.ID : '')]: true,
          },
        });
      }
      return {
        isReturn: isError
      }
    };
    const data = {
      id:(record ? record.ID : ''),
      close:closeF,
      emOrderFlowCardClick:custom.emOrderFlowCardClick,
      getIds:getIds,
    };
    return {
      content: <FormPage formCode={'EM013'} data={data}/>,
      click: regClick,
      close:closeF,
    }
  },
  btnConfirmByBatch:({record,close,loadData,login,btn,dispatch,getSelectedRecords})=> {
    const selectRecords=getSelectedRecords();
    let ids=[];
    if(selectRecords){
      selectRecords.forEach(item => {
        if(item.EM_CONFIRM_STATUS==1||item.EM_CANCEL_STATUS==1||item.ORDER_STATUS==3)return;
        ids.push(item.ID);
      });
    }
    const confirm=()=>{
      const json={Keyword:ids.join(',')};
      dispatch({
        type: 'EMemOrder/confirmEmOrder',
        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={!ids.length} title={btn.LABEL}/>,
    };
  },
  getRowClassName:({record,pageTableCustomParams})=>{
    let className=null;
    if(record&&record.Nav_EmOrderProcess&&record.Nav_EmOrderProcess.Nav_Details){
      record.Nav_EmOrderProcess.Nav_Details.forEach(t=>{
          if(t.Nav_Procedure){
            const r=pageTableCustomParams[t.Nav_Procedure.CODE];
            if(r){
              className=r;
            }
          }
          const xs=pageTableCustomParams['rowClassNameKey'];
          if(xs&&t.REMARK&&t.REMARK.indexOf(xs)>-1){
            className='warning';
          }
      });
    }
    return className;
  },
  colFeedArea:({record})=> {
    if(record&&record.Nav_MrpOrderWork&&record.Nav_MrpOrderWork.Nav_MrpOrderEmProposal){
      return (record.FEED_QTY*
        record.Nav_MrpOrderWork.Nav_MrpOrderEmProposal.LENGTH*record.Nav_MrpOrderWork.Nav_MrpOrderEmProposal.WIDTH/1000000).toFixed(6);
    }
  },
  colProductArea:({record})=>{
    if (record.Nav_Product && record.Nav_Product.SPLIT_QTY>0){
				return  (record.QTY * record.Nav_Product.SPLIT_LENGTH * record.Nav_Product.SPLIT_WIDTH / record.Nav_Product.SPLIT_QTY/ 1000000).toFixed(6);
    }
  }
}
