
import { Button, Popconfirm ,InputNumber,DatePicker } from 'antd'
import {message} from "antd/lib/index";
import {extendRule, initQueryFilter} from "../../utils/common";
import DropDownPagination from "../../components/common/DropDownPaginationEx"
import moment from 'moment';
//用户列表页
export default {
  percentage:({record})=>{
    return <div><label>{Math.floor(record.OccRadio*1000)/10}</label><label>{'%'}</label></div>
  },
  colAssignOp:({record,close,loadData,getLoadParam,login,btn,dispatch, width})=>{

    const confirm=()=>{
      //dispatch()
      let val = {
        "Keyword":record.ID,
        "OrgId":record.ORG_ID,
      };
      dispatch({
        type: 'app/getDataByPost',
        payload: val,
        url:"PM/Assign/Revert",
        onComplete: () => {
          message.success('执行成功');
          //loadData();
          const param= getLoadParam();
         if(param&&param.data&&param.data.func){
           param.data.func.forEach((item,i)=>{
             if(typeof item==="function")
                item();
           })
         }
        }
      })
    };
    return  {
      content: <Popconfirm title="是否确定撤销?" onConfirm={confirm}>
        <Button>{btn.LABEL}</Button>
      </Popconfirm>
    }
  },
  colWorkStage:({record,close,loadData,getLoadParam,login,btn,dispatch, width})=>{
    const onSelectStage=(data)=>{
      if(data&&data.data&&data.data.length>0) {
        var workstageId = data.data[0];
        if (workstageId) {
          if (data.data[1] != record.Nav_PdtProcessDetail.PROCEDURE_ID) {
            message.error("工作中心所对应的工序不匹配");
            return;
          }
          if (workstageId != record.PLAN_WORKSTAGE_ID) {
            let val = {
              "Keyword": record.ID,
              "Parameter1": 1,//修改中心
              "Parameter2": workstageId,//中心ID
              "Parameter3": login.userId,//用户ID
              "OrgId": record.ORG_ID,
            };
            dispatch({
              type: 'app/getDataByPost',
              payload: val,
              url: "PM/Plan/Change",
              onComplete: (ret) => {
                if (ret) {
                  message.success('执行成功');
                  const param = getLoadParam();
                  if (param && param.data && param.data.func) {
                    param.data.func.forEach((item, i) => {
                      if (typeof item === "function")
                        item();
                    })
                  }
                }
              }
            })
          }
        }
      }
    };
    if(record.ORDER_STATUS==0||record.HANG_STATUS){
      return <DropDownPagination inputDataApi={'FM/WorkStage/OrderPaged'}
                                 inputLabelField={'NAME'}
                                 inputShowField={'CODE,NAME'}
                                 inputFilterField={'CODE,NAME'}
                                 width={width > 0 && width}
                                 value={record.Nav_PlanWorkstage?record.Nav_PlanWorkstage.NAME:''}
                                 onSelect={onSelectStage}
                                 inpuNavField={'ID,PROCEDURE_ID'}
                                 onFilter={({params})=>{ extendRule(params,"PROCEDURE_ID",1,record.Nav_PdtProcessDetail.PROCEDURE_ID) }}/>

    }
    else{
      return record.Nav_PlanWorkstage?record.Nav_PlanWorkstage.NAME:'';
    }

  },
  colPlanStart:({record,close,loadData,getLoadParam,login,btn,dispatch, width})=>{
    const dateFormat = 'YYYY-MM-DD';
    let def=moment(record.PLAN_START_TIME,dateFormat);
    const onChange=(date,dateString)=>{
      if(!date){
        return;
      }
      if(date.diff(def,'days')!=0 ){
        let val = {
          "Keyword":record.ID,
          "Parameter1":2,//开始时间
          "Parameter2":dateString,//开始时间
          "Parameter3":login.userId,//用户ID
          "OrgId":record.ORG_ID,
        };
        dispatch({
          type: 'app/getDataByPost',
          payload: val,
          url:"PM/Plan/Change",
          onComplete: (ret) => {
            if(ret){
              message.success('执行成功');
              const param= getLoadParam();
              if(param&&param.data&&param.data.func){
                param.data.func.forEach((item,i)=>{
                  if(typeof item==="function")
                    item();
                })
              }
            }
          }
        });
      }
    };

    if(record.ORDER_STATUS==0||record.HANG_STATUS){
      return <DatePicker style={{ width: width > 0 && width }} onChange={onChange} defaultValue={def}  />

    }
    else{
      return record.PLAN_START_TIME?record.PLAN_START_TIME.substring(0,10):'';
    }

  },
  colPlanEnd:({record,close,loadData,getLoadParam,login,btn,dispatch, width})=>{
    const dateFormat = 'YYYY-MM-DD';
    let def=moment(record.PLAN_END_TIME,dateFormat);
    const onChange=(date,dateString)=>{
      if(!date){
        return;
      }
      if(date.diff(def,'days')!=0 ){
        let val = {
          "Keyword":record.ID,
          "Parameter1":3,//结束时间
          "Parameter2":dateString,//开始时间
          "Parameter3":login.userId,//用户ID
          "OrgId":record.ORG_ID,
        };
        dispatch({
          type: 'app/getDataByPost',
          payload: val,
          url:"PM/Plan/Change",
          onComplete: (ret) => {
            if(ret){
              message.success('执行成功');
              const param= getLoadParam();
              if(param&&param.data&&param.data.func){
                param.data.func.forEach((item,i)=>{
                  if(typeof item==="function")
                    item();
                })
              }
            }
          }
        });
      }
    };

    if(record.ORDER_STATUS==0||record.HANG_STATUS){
      return <DatePicker style={{ width: width > 0 && width }} onChange={onChange} defaultValue={def}  />

    }
    else{
      return record.PLAN_END_TIME?record.PLAN_END_TIME.substring(0,10):'';
    }

  },
  revertAssign :({getSelectedRowKeys,login,loadData,getLoadParam,dispatch,clearSelectedRowKeys})=>{
    var keys=getSelectedRowKeys();
    if(!keys){
      message.error("未选择任何行");
    }else{
      let val = initQueryFilter(login.OrgId, 1, 100, "NUM", 0);
      let str=keys.join(",");
      val["Keyword"]=str;
      dispatch({
        type: 'app/getDataByPost',
        payload: val,
        url:"PM/Assign/Revert",
        onComplete: (ret) => {
          if (ret) {
            message.success("执行成功");
            clearSelectedRowKeys();
            const param= getLoadParam();
            if(param&&param.data&&param.data.func){
              param.data.func.forEach((item,i)=>{
                if(typeof item==="function")
                  item();
              })
            }
          }
        }
      });
    }
  },

  releasePlans :({getSelectedRowKeys,login,loadData,getLoadParam,dispatch,clearSelectedRowKeys})=>{
    var keys=getSelectedRowKeys();
    if(!keys){
      message.error("未选择任何行");
    }else{
      let val = initQueryFilter(login.OrgId, 1, 100, "NUM", 0);
      let str=keys.join(",");
      val["Keyword"]=str;
      dispatch({
        type: 'app/getDataByPost',
        payload: val,
        url:"PM/Plan/Release",
        onComplete: (ret) => {
          if (ret) {
            message.success("执行成功");
            clearSelectedRowKeys();
            //loadData();
            const param= getLoadParam();
            if(param&&param.data&&param.data.func){
              param.data.func.forEach((item,i)=>{
                if(typeof item==="function")
                  item();
              })
            }
            //this.loadFun("assign");
          }
        }
      });
    }
  },
  revertPlans :({getSelectedRowKeys,login,loadData,getLoadParam,dispatch,clearSelectedRowKeys})=>{
    var keys=getSelectedRowKeys();
    if(!keys){
      message.error("未选择任何行");
    }else{
      let val = initQueryFilter(login.OrgId, 1, 100, "NUM", 0);
      let str=keys.join(",");
      val["Keyword"]=str;
      dispatch({
        type: 'app/getDataByPost',
        payload: val,
        url:"PM/Plan/Revert",
        onComplete: (ret) => {
          if (ret) {
            message.success("执行成功");
            clearSelectedRowKeys();
            //loadData();
            //this.loadFun("assign");
            const param= getLoadParam();
            if(param&&param.data&&param.data.func){
              param.data.func.forEach((item,i)=>{
                if(typeof item==="function")
                  item();
              })
            }
          }
        }
      });
    }
  },

  genPlan:({getSelectedRowKeys,login,loadData,getLoadParam,dispatch,clearSelectedRowKeys})=>{
    let val = initQueryFilter(login.OrgId, 1, 100, "NUM", 0);
    var keys=getSelectedRowKeys();
    if(!keys) {
      message.error("未选择任何行");
    }else{
      let str=keys.join(",");
      val["Keyword"]=str;
      dispatch({
        type: 'app/getDataByPost',
        payload: val,
        url:"PM/Plan/Gen",
        onComplete: (ret) => {
          if (ret) {
            // this.loadFun("norelease");
            // this.loadFun("assign");
            clearSelectedRowKeys();
            //loadData();
            const param= getLoadParam();
            if(param&&param.data&&param.data.func){
              param.data.func.forEach((item,i)=>{
                if(typeof item==="function")
                  item();
              })
            }
          }
        }
      });
    }

  },
//自动分配
  AutoAssign:({getSelectedRowKeys,login,loadData,getLoadParam,dispatch,clearSelectedRowKeys})=>{
    const tmpLoadParam= getLoadParam();
    const customs=tmpLoadParam&&tmpLoadParam.data&& tmpLoadParam.data.customs?tmpLoadParam.data.customs:[];
    let val = initQueryFilter(login.OrgId, 1, 100, "NUM", 0);
    if(customs){
        customs.forEach(item => {
          val[item.key]=item.value;
        });
    }
    dispatch({
      type: 'app/getDataByPost',
      payload: val,
      url:"PM/Assign/Auto",
      onComplete: (ret) => {
        if (ret) {
          if(tmpLoadParam&&tmpLoadParam.data&&tmpLoadParam.data.func){
            tmpLoadParam.data.func.forEach((item,i)=>{
              if(typeof item==="function")
                item();
            })
          }

        }
      }
    });

  },
  assign:({getSelectedRowKeys,login,loadData,getLoadParam,dispatch,clearSelectedRowKeys})=>{
    const tmpLoadParam= getLoadParam();
    const customs=tmpLoadParam&&tmpLoadParam.data&& tmpLoadParam.data.customs?tmpLoadParam.data.customs:[];
    let val = initQueryFilter(login.OrgId, 1, 100, "NUM", 0);
    if(customs){
      customs.forEach(item => {
        val[item.key]=item.value;
      });
    }
    val["Keyword"]=0;
    dispatch({
      type: 'app/getDataByPost',
      payload: val,
      url:"PM/Assign/Auto",
      onComplete: (ret) => {
        if (ret) {
          if(tmpLoadParam&&tmpLoadParam.data&&tmpLoadParam.data.func){
            tmpLoadParam.data.func.forEach((item,i)=>{
              if(typeof item==="function")
                item();
            })
          }

        }
      }
    });

  },
  colAssignChange:({record,loadData,onRefreshTableData,getLoadParam,login,btn,dispatch, width})=>{
     //let qty=record.QTY;
     const onChange=(e)=>{
       record.QTY=e;
       onRefreshTableData();
     }
    const onBlur=(e)=>{
      // qty=e.target.value;
      // if(qty)
      //   qty=0;
      let val = {
        "Keyword":record.ID,
        "Parameter1": record.QTY,
        "OrgId":record.ORG_ID,
      };
      dispatch({
        type: 'app/getDataByPost',
        payload: val,
        url:"PM/Assign/Change",
        onlyData:false,
        onComplete: (ret) => {
         if(ret&&ret.IsSuccessful&&ret.Data)
         {
           //record.QTY=qty;
           loadData();
           const param= getLoadParam();
           if(param&&param.data&&param.data.func){
             param.data.func.forEach((item,i)=>{
               if(typeof item==="function")
                 item();
             });
           }
         }else{
           message.error(ret.ErrorMessage);
           // qty=record.QTY;
           e.target.focus();
         }
        }
      })
    };
    return  <InputNumber style={{ width: width > 0 && width }} onBlur={onBlur} onChange={onChange} value={record.QTY}  />
  },
  colWorkOp :({record,close,loadData,getLoadParam,login,btn,dispatch})=>{

    let val = initQueryFilter(login.OrgId, 1, 100, "NUM", 0);
    val["Keyword"] = record.PLAN_ID;
   const confirmRevert=()=> {
     dispatch({
       type: 'app/getDataByPost',
       payload: val,
       url: "PM/PlanBatch/RevertPlan",
       onComplete: (ret) => {
         if (ret) {
           message.success("执行成功");
           reLoad(getLoadParam);
         }
       }
     });
   };
    const confirmPreRevert=()=> {
      dispatch({
        type: 'app/getDataByPost',
        payload: val,
        url:"PM/PlanBatch/PreRevertPlan",
        onComplete: (ret) => {
          if (ret) {
            message.success("执行成功");
            loadData();
          }
        }
      });
    };
    const confirmCancelPreRevert=()=> {
      dispatch({
        type: 'app/getDataByPost',
        payload: val,
        url:"PM/PlanBatch/CancelPreRevertPlan",
        onComplete: (ret) => {
          if (ret) {
            message.success("执行成功");
            loadData();
          }
        }
      });
    };

    const reLoad=(getLoadParam)=>{
      const param= getLoadParam();
      if(param&&param.data&&param.data.func){
        param.data.func.forEach((item,i)=>{
          if(typeof item==="function")
            item();
        })
      }
    }
    return  <div>
        <Popconfirm title="是否确定撤销?" onConfirm={confirmRevert}>
          <Button type={'danger'} icon={'rollback'} size="small" title={'撤销'} />
      </Popconfirm>
      </div>
},


}
