import { connect } from 'dva';
import { Button, Row, Col, Form, Radio, Select, Table, Checkbox } from 'antd';
import { extendRule, initFilter, extendInclude, guid } from "../../../utils/common";
import EditComponent from '../../../baseComponents/EditComponent';
import _ from "lodash";

class ReworkPdtOrderBySnPage extends EditComponent {
  constructor(props) {
    super(props);
    this.state = {
      data: {},
    };
  };
  onAfterHeadDropDownSelect(params) {
    super.onAfterHeadDropDownSelect(params);

    let { value, record, colConfig, thisRef } = params ? params : {};
    if (colConfig.field === 'Nav_PdtOrder.PDT_CODE') {
      console.log('xxxxx');
      const pdtOrderId = this.getFieldValue('PDT_ORDER_ID');
      if (pdtOrderId) {
        const stateData = this.state.data;
        let json = initFilter(stateData.ORG_ID, stateData.ID, 'CREATE_TIME', 1);
        extendRule(json, 'PDT_ORDER_ID', 1, pdtOrderId);
        extendRule(json, 'STATE', 1, 13);
        this.props.dispatch({
          type: 'reworkPdtOrder/GetReworkSNByPdtOrder',
          payload: json,
          onComplete: (ret) => {
            const data = [];
            const snIds=[];
            if (ret && ret.length) {
              ret.forEach((t, index) => {
                snIds.push(t.ID);
                data.push({
                  ID: guid(),
                  SN_ID: t.ID,
                  Nav_SN: t,
                  ORG_ID: stateData.ORG_ID,
                  ROW_NO: (index + 1),
                  REWORK_PDT_ORDER_ID:stateData.ID,
                })
              });
            } 
            this.setEditTableData({ editCode: 'Nav_ReworkDetails', data: data, isAppend: false,onComplete:()=>{
                if(snIds&&snIds.length){ 
                   this.doGetReworkProcedure(snIds);
                }
            } });
          }
        }); 
      }
    }
  }
 

  doGetReworkProcedure=(snIds)=>{
    const stateData = this.state.data;
    let json = initFilter(stateData.ORG_ID, snIds.join(','), 'CREATE_TIME', 1);
    this.props.dispatch({
      type: 'reworkPdtOrder/GetReworkProcedure',
      payload: json,
      onComplete: (ret) => {
        const data = []; 
        const processData=this.state.data.Nav_Processs;
        if (ret && ret.Nav_ReworkProcessDetails&&ret.Nav_ReworkProcessDetails.length) {
          ret.Nav_ReworkProcessDetails.forEach((t, index) => { 
            if(processData&&processData.filter(t1=>t1.PDT_PROCESS_DETAIL_ID===t.ID).length)
              return;
            data.push({
              ID: guid(),
              PDT_PROCESS_DETAIL_ID: t.ID,
              Nav_PdtProcessDetail: t,
              ORG_ID: stateData.ORG_ID,
              ROW_NO: (index + 1),
              NUM:(index+1),
              REWORK_PDT_ORDER_ID:stateData.ID,
            })
          });
        } 
        this.setEditTableData({ editCode: 'Nav_Processs', data: data, isAppend: true,});
      }
    });
  }
  onAfterRowDropDownSelect(params){
    super.onAfterRowDropDownSelect(params);
    const {colConfig,data,}=params;
    if(colConfig.field==='Nav_SN.CODE'){
        if(data.SN_ID){
          this.doGetReworkProcedure([data.SN_ID]);
        }
    }
  }
    
}


export default connect(({ login, loading, editPage, custom }) => ({ login, loading, editPage, custom }))(Form.create()(ReworkPdtOrderBySnPage));
