
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 {

  revertOnePlan :({record,close,loadData,getLoadParam,login,btn,dispatch})=>{

    let val = initQueryFilter(login.OrgId, 1, 100, "NUM", 0);
    val["Keyword"]=record.ID;
    dispatch({
      type: 'app/getDataByPost',
      payload: val,
      url:"PM/PlanBatch/RevertPlan",
      onComplete: (ret) => {
        if (ret) {
          message.success("执行成功");
          loadData();
        }
      }
    });
  },
  revertOnePlans :({getSelectedRowKeys,login,loadData,getLoadParam,dispatch,clearSelectedRowKeys})=>{
    var keys=getSelectedRowKeys();
    let val = initQueryFilter(login.OrgId, 1, 100, "NUM", 0);
    val["Keyword"]=keys.join(",");;
    dispatch({
      type: 'app/getDataByPost',
      payload: val,
      url:"PM/PlanBatch/RevertPlan",
      onComplete: (ret) => {
        if (ret) {
          message.success("执行成功");
          loadData();
        }
      }
    });
  },

  cancelPreRevertOnePlans :({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/PlanBatch/CancelPreRevertPlan",
        onComplete: (ret) => {
          if (ret) {
            clearSelectedRowKeys();
            message.success("执行成功");
            loadData();
          }
        }
      });
    }
  },
  preRevertOnePlans :({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/PlanBatch/PreRevertPlan",
        onComplete: (ret) => {
          if (ret) {
            clearSelectedRowKeys();
            message.success("执行成功");
            loadData();
          }
        }
      });
    }
  },
  colRleaseData:({record,close,loadData,getLoadParam,login,btn,dispatch})=>{
     let works='';
     if(record.Nav_Works)
     {
       works=record.Nav_Works.map((item)=>{
         return item.CODE+",";
       });
       //works=works.substring(0,works.length-1);
     }
      return <div>{works}</div>;
},

  colPlanStart:({record,close,loadData,getLoadParam,login,btn,dispatch, width})=>{
    const dateFormat = 'YYYY-MM-DD';
    let def=moment(record.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
          "Parameter4":1,//变更类型
          "OrgId":record.ORG_ID,
        };
        dispatch({
          type: 'app/getDataByPost',
          payload: val,
          url:"PM/Plan/Change",
          onComplete: (ret) => {
            if(ret){
              message.success('执行成功');
              loadData();
            }
          }
        });
      }
    };

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

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

  },
  colPlanEnd:({record,close,loadData,getLoadParam,login,btn,dispatch, width})=>{
    const dateFormat = 'YYYY-MM-DD';
    let def=moment(record.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
          "Parameter4":1,//变更类型
          "OrgId":record.ORG_ID,
        };
        dispatch({
          type: 'app/getDataByPost',
          payload: val,
          url:"PM/Plan/Change",
          onComplete: (ret) => {
            if(ret){
              message.success('执行成功');
              const param= getLoadParam();
              loadData();
            }
          }
        });
      }
    };

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

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

  },
  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.WORKSTAGE_ID) {
            let val = {
              "Keyword": record.ID,
              "Parameter1": 1,//修改中心
              "Parameter2": workstageId,//中心ID
              "Parameter3": login.userId,//用户ID
              "Parameter4": 1,//类型
              "OrgId": record.ORG_ID,
            };
            dispatch({
              type: 'app/getDataByPost',
              payload: val,
              url: "PM/Plan/Change",
              onComplete: (ret) => {
                if (ret) {
                  message.success('执行成功');
                  loadData();
                }
              }
            })
          }
        }
      }
    };
    if(record.DO_STATUS==0){
      return <DropDownPagination inputDataApi={'FM/WorkStage/OrderPaged'}
                                 inputLabelField={'NAME'}
                                 inputShowField={'CODE,NAME'}
                                 inputFilterField={'CODE,NAME'}
                                 width={width > 0 && width}
                                 value={record.Nav_Workstage?record.Nav_Workstage.NAME:''}
                                 onSelect={onSelectStage}
                                 inpuNavField={'ID,PROCEDURE_ID'}
                                 onFilter={({params})=>{ extendRule(params,"PROCEDURE_ID",1,record.Nav_PdtProcessDetail.PROCEDURE_ID) }}/>

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

  },
}
