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


function isDelay(record){
  if(record.ORDER_STATUS!=2&&record.ORDER_STATUS!=3){
    const dueTime=(record.DUE_TIME?moment(record.DUE_TIME):new moment()).format("YYYY-MM-DD");
    const nowTime=(new moment()).format("YYYY-MM-DD");
    if(dueTime==nowTime)return'否';
    return moment(dueTime).isBefore(nowTime)?true:false;
  }
  return false;
};


//销售订单
export default {
  colFqc: ({record, close, loadData, getLoadParam, login, btn, dispatch}) => {

    let val = initQueryFilter(login.OrgId, 1, 100, "NUM", 0);
    val["Keyword"] = record.PDT_CODE;
    val["Parameter2"] = login.userId;
    const confirmRevert = () => {
      dispatch({
        type: 'app/getDataByPost',
        payload: val,
        url: "QC/Order/FQC",
        onComplete: (ret) => {
          if (ret) {
            message.success("执行成功");
            loadData();
          }
        }
      });
    };

    return {
      content:<ConfirmButton isConfirm={true} confirmTitle="是否触发FQC?" onClick={confirmRevert}
      type={btn.CSS} icon={btn.ICON}  size="small" title={btn.LABEL} disabled={record.ORDER_STATUS == 3 || record.WAITING_SEND_TEST_QTY == 0}/>
    }
  },
  colQa: ({record, close, loadData, getLoadParam, login, btn, dispatch}) => {
    let val = initQueryFilter(login.OrgId, 1, 100, "NUM", 0);
    val["Keyword"] = record.PDT_CODE;
    val["Parameter2"] = login.userId;
    const confirmRevert = () => {
      dispatch({
        type: 'app/getDataByPost',
        payload: val,
        url: "QC/Order/QA",
        onComplete: (ret) => {
          if (ret) {
            message.success("执行成功");
            loadData();
          }
        }
      });
    };

    return {
      content: <ConfirmButton isConfirm={true} confirmTitle="是否触发QA?" onClick={confirmRevert}
      type={btn.CSS} icon={btn.ICON}  size="small" title={btn.LABEL} disabled={record.ORDER_STATUS == 3 || record.WAITING_SEND_QA_QTY == 0}/>
    }
  },
  btnCancel: ({record, close, loadData, login, btn, dispatch}) => {
    let isReturn = record.ORDER_STATUS == 3 || !record.EM_ORDER_ID;
    const confirm = () => {
      const json = {Keyword: record.ID};
      dispatch({
        type: 'WOPdtOrder/cancelPdtOrder',
        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}/>,
    };
  },
  btnComplete: ({record, close, loadData, login, btn, dispatch}) => {
    const enable=record.ORDER_STATUS==0||record.ORDER_STATUS==1;
    const confirm = () => {
      if(!enable)return;
      const json = {Keyword: record.PDT_CODE};
      dispatch({
        type: 'WOPdtOrder/completePdtOrder',
        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={!enable}/>,
    };
  },
  btnEdit: ({record, close, loadData, login, dispatch}) => {
    let isReturn = record.ORDER_STATUS == 3;
    const closeF = () => {
      close();
      loadData();
    };
    const regClick = async function () {
      await dispatch({
        type: 'custom/save',
        payload: {
          ['pdtOrderEditClick' + (record ? record.ID : '')]: true
        },
      });
    };
    const data = {
      id: (record ? record.ID : ''),
      close: closeF,
    };
    return {
      content: <FormPage formCode={'WO011'} data={data}/>,
      click: regClick,
      isReturn
    }
  },
  btnBom: ({record, close, loadData, login, dispatch}) => {
    let isReturn = record.ORDER_STATUS == 3||record.ORDER_STATUS == 2;
    const closeF = () => {
      close();
      loadData();
    };
    const regClick = async function () {
      await dispatch({
        type: 'custom/save',
        payload: {
          ['pdtOrderBomClick' + (record ? record.ID : '')]: true
        },
      });
    };
    const data = {
      id: (record ? record.ID : ''),
      close: closeF,
      isShow:isReturn
    };
    return {
      content: <FormPage formCode={'WO013'} data={data}/>,
      click: regClick,
    }
  },
  colLastBookPro: ({record, close, loadData, getLoadParam, login, btn, dispatch}) => {
    let isReturn =record.ORDER_STATUS == 2|| record.ORDER_STATUS == 3 ;
    if(isReturn)return '';
    if (record.Nav_PdtProcess && record.Nav_PdtProcess.Nav_Details) {
       record.Nav_PdtProcess.Nav_Details.sort((x, y) =>
        x.NUM > y.NUM ? 1 : -1
      );

      let r = '';
      let count=0;
      record.Nav_PdtProcess.Nav_Details.forEach((n, i) => {
        if (n.TOTAL_BOOK_QTY) {
          r = n.NAME;
        }
        count++;
      });
      if(count==record.Nav_PdtProcess.Nav_Details.length)
        return '';
      return r;
    }
  },
  colNextBookPro: ({record, close, loadData, getLoadParam, login, btn, dispatch}) => {
    let isReturn =record.ORDER_STATUS == 2|| record.ORDER_STATUS == 3 ;
    if(isReturn)return '';
    if (record.Nav_PdtProcess && record.Nav_PdtProcess.Nav_Details) {
       record.Nav_PdtProcess.Nav_Details.sort((x, y) =>
        x.NUM > y.NUM ? 1 : -1
      );

      let r = '';
      let isBook=false;
      record.Nav_PdtProcess.Nav_Details.forEach((n, i) => {
        if (n.TOTAL_BOOK_QTY) {
          isBook=true;
        }
        else if(isBook&&!r){
          r = n.NAME;
        }
      });
      return r;
    }
  },
  checkRowClass: ({record}) => {
    return isDelay(record) ? 'danger' : null;
  },
  colIsDelay: ({record}) => {
    return isDelay(record) ? '是' : '否';
  },
  btnBatchRevoke: ({record, loadData, login, dispatch, btn, getSelectedRowKeys, clearSelectedRowKeys, getSelectedRecords}) => {
    const selectRecords = getSelectedRecords();
    const selectedRowKeys = getSelectedRowKeys();
    let enable = true;
    const confirm = () => {
      const tmpSelectedRowKeys = getSelectedRowKeys();
      if (tmpSelectedRowKeys == null || tmpSelectedRowKeys.length == 0) {
        message.error('请选择生产订单记录');
        return;
      }
      if (selectRecords) {
        selectRecords.forEach(t=> {
          //record.ORDER_STATUS == 3 || !record.EM_ORDER_ID
          if (t.ORDER_STATUS == 3 || !t.EM_ORDER_ID) {
            message.error(t.PDT_CODE + '该生产订单编号无法执行,请核实！');
            enable = false;
            return;
          }
        });
        if (!enable)
          return;
      }
      const json = {Keyword: tmpSelectedRowKeys.join(',')};
      dispatch({
        type: 'WOPdtOrder/cancelPdtOrder',
        payload: json,
        onComplete: (ret) => {
          if (ret) {
            message.success('执行成功');
            loadData();
          }
        }
      })
    };
    return {
      content:<ConfirmButton isConfirm={true} confirmTitle="是否确认生产?" onClick={confirm}
                     type={btn.CSS} icon={btn.ICON} disabled={!selectedRowKeys.length} key='btnConfirm' title={btn.LABEL} />
    };
  },
  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);
    }
  }
}
