import { connect } from 'dva';
import { Button, Row, Radio , Form, message, Select, Table, Checkbox } from 'antd';
import { extendRule, initFilter, extendInclude, guid } from "../../../utils/common";
import EditComponent from '../../../baseComponents/EditComponent';
import _ from "lodash";
class SupplyFeedPdtOrderPage 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') {
      const pdtOrderId = this.getFieldValue('PDT_ORDER_ID');
      if (pdtOrderId) {
        const stateData = this.state.data;
        let json = initFilter(stateData.ORG_ID, '', 'CREATE_TIME', 1);
        extendRule(json, 'Nav_Job.PDT_ORDER_ID', 1, pdtOrderId); 
        extendInclude(json, 'Nav_Line');
        this.props.dispatch({
          type: 'reworkPdtOrder/getJobFeeds',
          payload: json,
          onComplete: (ret) => {
            const data = [];
            const lineIds=[];
            if (ret && ret.length) { 
              ret.forEach((t, index) => {
                if (t.Nav_Line&&!lineIds.includes(t.Nav_Line.ID)) {
                  let temp=data.filter(t1=>t1.EXT_PROCEDURE_CODE===t.Nav_Line.EXT_PROCEDURE_CODE)[0];
                  if(!temp){
                    temp={
                      ID: guid(),
                      EXT_PROCEDURE_CODE:t.Nav_Line.EXT_PROCEDURE_CODE,
                      //LINE_ID: t.LINE_ID,
                      ORG_ID: stateData.ORG_ID,
                    //  Nav_Line: t.Nav_Line,
                      ROW_NO: (index + 1),
                      REWORK_PDT_ORDER_ID:stateData.ID,
                      IS_SELECTED: false,
                      LINE_ID:t.Nav_Line.ID,
                    };
                    data.push(temp);
                    lineIds.push(t.Nav_Line.ID);
                  }
                  let tempLines=temp.CAN_SELECT_LINES?temp.CAN_SELECT_LINES:'';
                  if(tempLines!==''){
                    tempLines+=',';
                  }
                  temp.CAN_SELECT_LINES=tempLines+(t.Nav_Line.ID+':'+t.Nav_Line.NAME);
                }
              });
            }
            this.setEditTableData({ editCode: 'workline', data, isAppend: false,onComplete:()=>{
              if(lineIds.length){
                lineIds.forEach(t=>{
                    this.getWorkstageByLine(t);
                });
              }
            } });
          }
        });

        this.setEditTableData({ editCode: 'workstage', data: [], isAppend: false });
      }
    }
  }
 

  getWorkstageByLine=(lineId)=>{
    const stateData = this.state.data;
        let json = initFilter(stateData.ORG_ID, '', '', 1);
        extendRule(json, 'ID', 1, lineId); 
        extendInclude(json, 'Nav_LineStages.Nav_Workstage');
        this.props.dispatch({
          type: 'reworkPdtOrder/getWorkstageByLine',
          payload: json,
          onComplete: (ret) => {
            if(ret&&ret.Nav_LineStages&&ret.Nav_LineStages.length){
              const workstageData= this.getEditTableData({editCode:'workstage'});
                if(!workstageData.filter(t=>t.LINE_ID===lineId).length){
                  const stateData = this.state.data;
                  const workstateIds=[];
                    ret.Nav_LineStages.forEach(t=>{
                      workstateIds.push(t.STAGE_ID);
                    })
                    const data = [];
                  data.push({
                    ID: guid(),
                    LINE_ID: ret.ID,
                    REWORK_PDT_ORDER_ID:stateData.ID,
                    ORG_ID: stateData.ORG_ID,
                    Nav_Line: ret, 
                    DETAIL:workstateIds.join(','),
                  })
                  this.setEditTableData({ editCode: 'workstage', data, isAppend: true });
             }
            }
            
          }
        });
  }

  onRenderRowControl(params) { 
    const {data}=this.props;
    const { editCode, colConfig, record, text, parentId, parentRecord } = params ? params : {};
    if (editCode === 'workstage' && colConfig.field === 'Workstage') {
      if (record.Nav_Line && record.Nav_Line.Nav_LineStages && record.Nav_Line.Nav_LineStages.length) {
        const options = [];
        record.Nav_Line.Nav_LineStages.forEach(t => {
          options.push({ label: t.Nav_Workstage.NAME, value: t.STAGE_ID });
        });
        const values = record.DETAIL ? record.DETAIL.split(',') : [];
        return <div style={{overflow:'scroll',width:'900px'}} > 
          <Checkbox.Group
          options={options}
          value={values}
          disabled={data.isShow}
          onChange={(e) => {
            if (e) {
              record.DETAIL = e.join(',');
            }
            else {
              record.DETAIL = null;
            }
            this.saveRowRecord({ record, editCode });
          }}
        />
        </div>
      }
      return null;
    }
    else if (editCode === 'workline' && colConfig.field === 'WorkLine') {
      if (record.CAN_SELECT_LINES ) {
        const temps=record.CAN_SELECT_LINES.split(',');
        const options = [];
        temps.forEach(t => {
          if(t){
            options.push({ label: t.split(':')[1], value:  t.split(':')[0] });
          }
        });
         
        //const values = record.LINE_ID ? record.DETAIL.split(',') : [];
        return  <div style={{overflow:'scroll',width:'600px'}} >   <Radio.Group
          options={options}
          value={record.LINE_ID}
          disabled={data.isShow}
          onChange={(e) => {
            const v=e.target.value;
            const oldV=record.LINE_ID; 
            record.LINE_ID=v; 
            this.saveRowRecord({ record, editCode,onComplete:()=>{
              if(oldV){
                const workstageData= this.getEditTableData({editCode:'workstage'});
                const tempData=[];
                workstageData.forEach(t=>{
                  if(t.LINE_ID!==oldV){
                    tempData.push(t);
                  }
                });
                this.setEditTableData({ editCode: 'workstage', data: tempData, isAppend: false,onComplete:()=>{
                  if(v){
                    this.getWorkstageByLine(v);
                  }
                } });
              }
              else if(v){
                this.getWorkstageByLine(v);
              }
            }});
          }}
        />
        </div>
      }
      return null;
    }

    return super.onRenderRowControl(params);
  }

}


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