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

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


//采购单列表
export default {

  colOpUsers:({record,loadData,getLoadParam,login,btn,dispatch})=>{
    let users=record.Nav_JobUsers;

    if(users){
      let ret="";
      users.forEach(function (n,i) {
         if(n.Nav_User){
           ret+=n.Nav_User.NAME+",";
         }
      });
      if(ret.length>0){
        ret=ret.substring(0,ret.length-1);
      }
      return ret;
    }else{
      return "";
    }
  },
  revert :({record,dispatch,loadData,login,custom,btn})=>{
    let isReturn=record.ORDER_STATUS!=3;
    let newRecord={...record};
    newRecord["ORDER_STATUS"]=3;
    const confirmRevert=()=> {
      dispatch({
        type: 'app/getDataByPost',
        payload: newRecord,
        url: "WO/Work/Update",
        onComplete: (ret) => {
          if (ret) {
            message.success("执行成功");
            loadData();
          }
        }
      });
    };

    return {
      content:<ConfirmButton isConfirm={true} confirmTitle="是否确认撤销?" onClick={confirmRevert}  type={btn.CSS} icon={btn.ICON} size="small" title={btn.LABEL} disabled={!isReturn}/>,
      isReturn:(record.ORDER_STATUS==3)
    }
  },
  unrevert :({record,dispatch,loadData,login,custom,btn})=>{
    let newRecord={...record};
    newRecord["ORDER_STATUS"]=record.IN_QTY==0?0:(record.BOOKING_QTY==record.QTY?2:1);
    const confirmRevert=()=> {
      dispatch({
        type: 'app/getDataByPost',
        payload: newRecord,
        url: "WO/Work/Update",
        onComplete: (ret) => {
          if (ret) {
            message.success("执行成功");
            loadData();
          }
        }
      });
    };

    return {
      content: <ConfirmButton isConfirm={true} confirmTitle="是否确认反撤销?" onClick={confirmRevert}   type={btn.CSS} icon={btn.ICON} size="small"
                              title={btn.LABEL} disabled={record.ORDER_STATUS!=3}/>,
      isReturn:(record.ORDER_STATUS!=3)
    }
  },
  checkRowClass:({record})=>{
    return isDelay(record)?'danger':null;
  },
  colIsDelay:({record})=>{
    return isDelay(record)?'是':'否';
  },
  btnRevertDeComplete:({record})=>{
    return {
      onBtnDisabled:(r)=>{
        return r.BOOKING_QTY>=r.QTY||r.ORDER_STATUS!==2;
      }
    }
  },
  btnEdit:({record})=>{
    return {
      onBtnDisabled:(r)=>{ 
        return !r.IS_MANUAL||r.ORDER_STATUS!==0;
      }
    }
  },
}
