import React from 'react'
import {DtmplData, LtmplConfigRes} from '../tmpl/interface';
import Units from "../units";
import ActTable from '../table/act-table'
// import StatActTable from '../table/stat-act-table'
import HCDataSource from "../tmpl/hc-data-source";
import ModalDtmplView from "../detail/view/modal-dtmpl-view";
import ModalDtmplEdit from "../detail/edit/modal-dtmpl-edit";
import HcserviceV3 from "../tmpl/hcservice-v3";

//以列表为入口的，页面内列表，基本原则是所有交互在页面内完成，不会触发路由。

export interface CriteriaPageProps  {

};

export interface CriteriaPageState  {

};

class CriteriaPage extends React.PureComponent<CriteriaPageProps, CriteriaPageState> {

    state = {
        ltmplConfigRes: undefined,
        criteriaData:undefined,
        showDtmplView:false,
        showDtmplEdit:false,
        selectedCode:undefined,
    }

    async componentDidMount() {
        this.loadLtmplConfig();
    }

    async componentDidUpdate(prevProps) {
        const { sourceId,mainCode} = this.props;
        if(!prevProps || prevProps.sourceId!=sourceId){
            this.loadLtmplConfig();
        }else if(mainCode && prevProps.mainCode!=mainCode){//此处处理有效，但不高效
            this.loadLtmplConfig();
        }
    }

    loadLtmplConfig = async () => {
        const {sourceId} = this.props;
        let ltmplConfigRes:LtmplConfigRes = await HCDataSource.requestLtmplConfig(sourceId);
        let criteriaData;
        //提取 过滤条件的默认值
        if(ltmplConfigRes){
           let defaultCriteriaValue=ltmplConfigRes.ltmplConfig.defaultCriteriaValue;
           if(defaultCriteriaValue){
               criteriaData=this.toCriteriaData(defaultCriteriaValue);
           }
        }
        this.setState({
            ltmplConfigRes,criteriaData
        })
    }
    toCriteriaData =  (values:object) => {
       let  criteriaData={};
        if(values){
            for (let key in values) {
                criteriaData[key]=Units.configParamTrans(values[key]);
            }
        }
        criteriaData['sequence']=Units.getRandomNum(6);
        return criteriaData;
    }


    // async componentDidUpdate(prevProps) {
    //     const {location} = this.props;
    //     const { sourceId} = this.props.match.params;
    //     let { sourceId: preSourceId} = prevProps.match.params;
    //     let {location: preLocation} = prevProps;
    //
    //     if ( !sourceId ||
    //         ( !preSourceId && this.state.ltmplConfigRes)
    //         || (location.pathname == preLocation.pathname && location.search == preLocation.search)
    //     ) {
    //         return;
    //     }
    //     this.loadData();
    // }

    showView = (code: string) => {
        // const { sourceId} = this.props.match.params;
        // this.onRoute(`/${sourceId}/detail-view?code=${code}&menuId=${this.getMenuId()}`);
        this.setState({
            selectedCode:code,
            showDtmplView:true,
        })
    }

    doEdit = (code: string,mode:string) => {
        this.setState({
            selectedCode:code,
            showDtmplEdit:true,
        })
    }



    doCreate = () => {
        this.setState({
            selectedCode:null,
            showDtmplEdit:true,
        })
    }

    doSearch = (params_) => {
        const { sourceId,mainCode} = this.props;
        //const oldfliter = history.location.search.slice(1);

        let params = {...params_}
        //追加过滤条件，hiddenColIds，innerQueryKey
        if (mainCode) {
            params['mainCode'] = mainCode;
        }
        params['sourceId'] = sourceId;
       // const criteriaData = Units.transQueryParamsToStr(params);
       // let flag = true;
        //if (newfliter && oldfliter != newfliter) { //查询条件更新时
        this.setState({
            criteriaData:this.toCriteriaData(params)
        });

    }

    handleDtmplEdit =  (code: string) => {
        this.doSearch(this.state.criteriaData);
        this.setState({
            showDtmplEdit:false,
        })
    }


    render() {
        const {ltmplConfigRes,criteriaData,showDtmplView,selectedCode,showDtmplEdit} = this.state;
        const { sourceId,mainCode} = this.props;

        if (!ltmplConfigRes) {
            return <></>;
        } else {
            const {buttons}=ltmplConfigRes.ltmplConfig
            return <>
                <ActTable  mainCode={mainCode} showView={this.showView}
                             doSearch={this.doSearch} doEdit={this.doEdit} doCreate={this.doCreate}
                             sourceId={sourceId} criteriaData={criteriaData}
                             ltmplConfig={ltmplConfigRes.ltmplConfig}/>
                {buttons.includes('detail') ?
                    <ModalDtmplView open={showDtmplView} onCancel={()=>{
                    this.setState({
                        showDtmplView:false,
                    })}
                    }
                                    sourceId={sourceId}
                                    code={selectedCode}> </ModalDtmplView> : ""}
                {buttons.includes('dtmplEdit') || buttons.includes('dtmplAdd') ?
                    <ModalDtmplEdit open={showDtmplEdit} onCancel={()=>{
                        this.setState({
                            showDtmplEdit:false,
                        })}}
                        sourceId={sourceId}
                                    code={selectedCode}
                                    onOk={this.handleDtmplEdit} />
                    : ""}
              </>
        }

    }
}

export default CriteriaPage;

