import { Button, Row, Col, Form, Dropdown, message } from 'antd';
import { empty,getPropertyData, getDataFieldValue, setDataFieldValue } from "../../../utils/common";
import EditComponent from '../../../baseComponents/EditComponent';
import ProcessDetailEditByConfigPage from './ProcessDetailEditByConfigPage';
import styles from '../../../baseComponents/Component.css'; 

class ProcessEditByConfigPage extends EditComponent {
  constructor(props) {
    super(props);
    this.state = {
      data: {},
    };
  };
  
  onBeforeEdit(params) {
    const { isNew, isCopy, copySrcData } = params ? params : {};
    this.setTreeDataByTreeNav();
    this.setProcessVisiable();
    this.setSelectedNodeId({  id:null });
 }
 setTreeDataByTreeNav=(onComplete)=>{
   const treeConfig= this.getTreeConfig();
   const data=this.state.data;
   const details=getDataFieldValue(data,(treeConfig.treeNavByData+".Nav_Details"));
   const treeData=this.getTreeDataByList({data:details});
   this.setTreeData({treeCode:treeConfig.treeCode,data:treeData,onComplete});
 }
 onSelectedTreeNode(params){
  const { treeCode , id,data:treeData,isDelete,isAdd } = params ? params : {}; 
  let isGetData=false;
  if(this.getStateData&&!isDelete){
    const preData=this.getStateData();
    if(preData&&preData.ID){
      isGetData=true;
      const treeConfig= this.getTreeConfig();
      const data={...this.state.data};
      let details= getDataFieldValue(data,(treeConfig.treeNavByData+".Nav_Details"));
      if(!details){
        details=[];
        setDataFieldValue(data,(treeConfig.treeNavByData+".Nav_Details"),details);
      } 
      const index = details.findIndex(item => preData.ID === item.ID);
      const item = details[index];
      if (item) {
        details.splice(index, 1, {
          ...item,
          ...preData,
        });
      }
      else{
        details.push(preData);
      }
      this.setState({
        data,
      },()=>{
        this.setTreeDataByTreeNav(()=>{
          if(this.setStateData){
            this.setStateData(treeData);
          } 
        });
      });
    }
  } 
  if(isAdd&&treeData){
    const treeConfig= this.getTreeConfig();
      const data={...this.state.data};
      let details= getDataFieldValue(data,(treeConfig.treeNavByData+".Nav_Details"));
      if(!details){
        details=[];
        setDataFieldValue(data,(treeConfig.treeNavByData+".Nav_Details"),details);
      } 
      const index = details.findIndex(item => treeData.ID === item.ID);
      const item = details[index];
      if (!item) {  
        details.push(treeData);
      } 
      this.setState({
        data,
      },()=>{
        this.setTreeDataByTreeNav(()=>{
          if(this.setStateData){
            this.setStateData(treeData);
          } 
        });
      });
  }
  if(!isGetData){
    isGetData=true;
    if(this.setStateData){
      this.setStateData(treeData);
    } 
  }
  
  
}

onAfterDeleteTreeNode(params){
  super.onAfterDeleteTreeNode(params);
  const {treeCode,id}=params?params:{}; 
  const treeConfig= this.getTreeConfig();
  const data={...this.state.data};
  let details= getDataFieldValue(data,(treeConfig.treeNavByData+".Nav_Details"));
  if(!details){
    details=[];
    setDataFieldValue(data,(treeConfig.treeNavByData+".Nav_Details"),details);
  }
  const index = details.findIndex(item => id === item.ID);
  const item = details[index];
  if (item) {
    details.splice(index, 1);
  } 
  this.setState({
    data,
  },()=>{
    this.setTreeDataByTreeNav();
  });
}
onCheckBeforeSave(params){
  let ret=super.onCheckBeforeSave(params);
  if(ret)return true; 
  const data=this.state.data;
  const treeConfig= this.getTreeConfig(); 
  let processsData= getDataFieldValue(data,treeConfig.treeNavByData);
  if (!processsData || !processsData.Nav_Details || processsData.Nav_Details.length === 0) {
    message.error('请填写工艺路线信息');
    ret=true;
  };
  let details=getDataFieldValue(processsData,"Nav_Details"); 
  if(this.getStateData){
    const preData=this.getStateData();
    if(preData&&preData.ID){ 
      if(!details){
        if(!processsData){
          processsData={};
        }
        details=[];
        setDataFieldValue(processsData,"Nav_Details",details);
      }
      const index = details.findIndex(item => preData.ID === item.ID);
      const item = details[index];
      if (item) {
        details.splice(index, 1, {
          ...item,
          ...preData,
        });
      }
      this.saveHeadRecord({
        [treeConfig.treeNavByData]:processsData
      })
    }
  }  


  if(processsData) {
    if (empty(getDataFieldValue(processsData, 'NAME', ''))) {
      message.error('请填写工艺路线名称');
      ret = true;
    }
    if (empty(getDataFieldValue(processsData, 'CODE', ''))) {
      message.error('请填写工艺路线编号');
      ret = true;
    }
    if (getDataFieldValue(processsData, 'FQC_AUTO_INSPEC', false) && getDataFieldValue(processsData, 'FQC_INSPEC_TYPE', 0) === 1 && getDataFieldValue(processsData, 'FQC_INSPEC_QTY', 0) === 0) {
      message.error('请填写送检数量');
      ret = true;
    }

    if(processsData.Nav_Details){
      processsData.Nav_Details.forEach(t=>{
        if(t.PROCEDURE_ID===undefined|| t.PROCEDURE_ID===null||t.PROCEDURE_ID==='') {
          message.error('请填写工序' + t.NAME + '工序');
          ret = true;
        };
        if(t.UNIT_ID===undefined|| t.UNIT_ID===null||t.UNIT_ID==='') {
          message.error('请填写工序' + t.NAME + '单位');
          ret = true;
        };
        if (getDataFieldValue(t, 'PROCEDURE_TYPE', 0)===1 && empty(getDataFieldValue(t, 'ORDER_FIELD', 0))) {
          message.error('请填写工序'+t.NAME+'对应字段');
          ret = true;
        };
        if (getDataFieldValue(t, 'PQC_AUTO_INSPEC', false) && getDataFieldValue(t, 'PQC_INSPEC_TYPE', 0) === 1
          && getDataFieldValue(t, 'PQC_INSPEC_QTY', 0) === 0) {
          message.error('请填写工序'+t.NAME+'送检数量');
          ret = true;
        };
        if (getDataFieldValue(t, 'PROCEDURE_TYPE', 0)===0 &&getDataFieldValue(t, 'CONVERT_RATIO', 0) ===0) {
          message.error('请填写工序'+t.NAME+'换算比率');
          ret = true;
        };
      });
    }
  }
  return ret;
} 

onGetSaveData(params){
    const data=this.state.data;
    const saveData=super.onGetSaveData(params);
    const treeConfig= this.getTreeConfig(); 
    let processsData=getDataFieldValue(data,treeConfig.treeNavByData);
    

    const rP=getPropertyData(processsData) 
    setDataFieldValue(saveData,treeConfig.treeNavByData,rP);
    return saveData;
  }
  onAfterHeadControlChange(params){
    super.onAfterHeadControlChange(params);
    this.setProcessVisiable();
  }

  setProcessVisiable=()=>{
    const data=this.state.data;
    const treeConfig= this.getTreeConfig(); 
    const isFqc= getDataFieldValue(data,(treeConfig.treeNavByData+'.IS_FQC'),false);
    const autoInspec=getDataFieldValue(data,(treeConfig.treeNavByData+'.FQC_AUTO_INSPEC'),false);
    const fs={}; 
    fs[treeConfig.treeNavByData+'.FQC_TYPE']=isFqc;
    fs[treeConfig.treeNavByData+'.Nav_FQCWorkStage.NAME']=isFqc;
    fs[treeConfig.treeNavByData+'.FQC_AUTO_INSPEC']=isFqc;
    fs[treeConfig.treeNavByData+'.FQC_INSPEC_TYPE']=(isFqc&&autoInspec);
    fs[treeConfig.treeNavByData+'.FQC_INSPEC_QTY']=(isFqc&&autoInspec);
    fs[treeConfig.treeNavByData+'.FQC_INSPEC_QTY_OVER']=(isFqc&&autoInspec); 
    this.setHeadFieldVisible(fs);
  }

  render() {
    const editConfig = this.getEditConfig();
    const btns = editConfig ? editConfig.btns : null;
    const selectTreeId=this.getSelectedNodeId();
    return (
      <div className={styles.editWrap}>
        <div className={styles.topBar}>
          <div style={{ float: "left" }}>
            {this.getRenderBtns({ btns, record: this.state.data })}
          </div>
          <div style={{ float: "right" }}>
            <Dropdown overlay={this.getCustomFieldMange(undefined)} trigger={['click']} placement="bottomRight">
              <Button icon="bars" />
            </Dropdown>
          </div>
        </div>
        <Form className={styles.formBox}>
          <Row className={styles.controlBlock}>
            {this.onRenderHeadControls()}
          </Row>
          {this.onRenderPanel()}
          <Row gutter={32} style={{ 'marginTop': '15px' }}>
              <Col span={8}>
                  {
                   this.state.data.ID?this.getRenderTreeByConfig():null
                  }
              </Col>
              <Col span={16}>
               {selectTreeId? <ProcessDetailEditByConfigPage 
                    {...{...this.props,formCode:'WO022'}} 
                    getStateData={(v)=>this.getStateData=v}
                    setStateData={(v)=>this.setStateData=v}
                  />:null}
              </Col>
          </Row>
        </Form>
      </div>
    );
  }
} 
export default ProcessEditByConfigPage;
