"use strict";
import { base } from './base.js'
import { util } from '../../util/util.js'
import { tree } from '../page/query/tree.js'
import {khcModal} from '../alert/khcModal.js'
import {config} from "../../config.js";
import {page} from "../page/page.js";
import {fullMap} from "../../fullMap.js";
import {pageInfoMap} from "../../pageInfoMap.js";
import {httpUtil} from "../../util/httpUtil.js";
/**
 * 以页面的来展示选择内容
 * 不一定是select，也可以是checkbox
 * 和vPage的区别就是这个会取值（isValue=true）
 */
class inputSelectPage extends base {
    constructor(data) {
        super(data);
        this.type='selectPage';
        this.isValue=true;
        this.pageObj=null;
    }
    render(parentObjID) {
        let that=this;
        let data = this.data
        let pageCode=data.dict_code.code;
        let code = { pageCode: pageCode }
        let obj={url:config.getConfigJson().pageInfoUrl,params:code,isClientCache:true}
        httpUtil.post(obj, (response)=>{
            let pageType = response.data.data.page_type;
            if (pageType == 'ordinary') {
                let componentArray = response.data.data.componentData;
                for (let i = 0; i < componentArray.length; i++) {
                    let componentInfo = componentArray[i];
                    let code={ version: '1.0.0', dsCode: componentInfo.ds_code, formCode: componentInfo.form_code,queryCode: componentInfo.query_code };
                    let formCode = { parentObjID: util.getUUID() + 'div', type: 'form', code: code }
                    let queryCode = { parentObjID: util.getUUID() + 'div', type: 'query', code: code };
                    let formPageData = { parentObjID: parentObjID };
                    formPageData['parentFormObjID'] = data.formObjID;
                    formPageData['pageCode'] = pageCode;
                    formPageData['formCode'] = formCode;

                    formPageData['queryCode'] = queryCode;
                    formPageData['pkFieldName'] = 'id';
                    formPageData['isReadOnly'] = data.is_read_only;
                    obj={url:config.getConfigJson().metaUrl,params:code,isClientCache:true}
                    httpUtil.post(obj, (response)=>{
                        let thisFormPage = new page(formPageData);
                        thisFormPage.render();
                        fullMap.push(thisFormPage.objID, thisFormPage);
                        that.pageObj=thisFormPage;
                    });
                }
            }
        });
    }
    setDisabled(){
        let that=this;
        that.data.is_read_only='Y';//未创建对象时只需设置状态即可，是否渲染会根据此状态判断
        if(!util.isNull(that.pageObj)){
            that.pageObj.isReadOnly='Y';
            that.pageObj.queryComponent.tableView.setDisabled();//已创建的对象需调用方法
        }
    }
    removeDisabled(){

    }
    hide(){
        //document.getElementById(this.parentObjID).setAttribute('style', 'display:none');//隐藏
    }
    show(){
        //document.getElementById(this.parentObjID).setAttribute('style', '');//展示
    }
    getValue() {
        let that=this;
        let tableView=that.pageObj.queryComponent.tableView
        if(tableView.selectTableDataArray!=undefined&&tableView.selectTableDataArray.length>0){
            let value=tableView.getSelectTableDataStr();
            return ','+value+',';
        }else{
            return '';
        }
    }
    setValue(value){
        let that=this;
        let timeout=0;
        if(that.pageObj==null){//如果子表对象为空是因为子表还没渲染就调用formEdit,设置一个延迟时间
            timeout=2000
        }
        setTimeout( ()=>{
            let tableView=that.pageObj.queryComponent.tableView
            if(!util.isNull(value)){
                let selectArray=value.split(',');
                tableView.selectTableDataArray=util.removeArrayValue(selectArray,'');
            }else{
                tableView.selectTableDataArray=[];
            }
            tableView.getContentsFromServer().then(()=>{
                tableView.tUtil.getSelectTableBodyHtml().then((htmlTemplate)=>{
                    document.getElementById(tableView.eleID+'_select').querySelector('tbody').innerHTML = htmlTemplate;
                    tableView.tableButtonsView.renderSelectHtml();
                    tableView.tableButtonsView.addSelectEventListener();
                })
            })
        },timeout)
    }
    hideDiv(){

    }
    showDiv(){

    }
    refreshCanNull(){

    }

}
export { inputSelectPage }