<script>
    import { oneOf } from '../../utils/assist';
    export default {
        name: 'conditionbase',
        data () {
            return {
                globalConfig:{
                },
                myConfig: {
                },
                clearInfo:'清空',
                conditionPrior:[],
                condition:[],
                dataType:{},
                hackReset:true,
                showCondition:true
            };
        },
        created () {
            this.init();
        },
        props:{
            config: {
                type: Object,
                default () {
                    return {};
                }
            },
            dictData: {
                type: Object,
                default:null
            },
            isPop: {
                type: Boolean,
                default:false
            },
            modelType: {
                validator (value) {
                    return oneOf(value, ['treeSearch', 'search','billSearch']);
                },
                default: 'search'
            }
        } ,
        methods:{
            init () {
                //获取初期默认值
                this.myConfig = Object.assign({}, this.globalConfig, this.myConfig);
            
                //获取配置
                if (this.config) {
                    this.myConfig = Object.assign({}, this.myConfig, this.config);
                }
                if (this.isPop) {
                    this.clearInfo = '清空';
                } else {
                    this.clearInfo = '重置';
                }

                if (this.myConfig.clearInfo) {
                    this.clearInfo = this.myConfig.clearInfo;
                }
                //处理下拉框
                if (this.dictData) {
                    if (this.myConfig.items) {
                        this.myConfig.items.forEach(el => {
                            this.initDictkey(el);
                        });
                    }
                }
               

                 //特定化处理
                if (this.customInit) {
                    this.customInit();
                }
            },
            initDictkey(el) {
                if (el.children && el.children.length > 0) {
                    for (let i=0;i<el.children.length; i++) {
                        this.initDictkey(el.children[i]);
                    } 
                } else {
                    if (el.dictkey) {
                        if (el.data === undefined) {
                            let dictkey = el.dictkey;
                            if (el.customDictkey) {
                                dictkey = el.customDictkey;
                            }
                            el.data = this.dictData[dictkey];
                        }
                    }
                }
            },
            initField() {
                let condition = [];
                let datatype = {};
                let conditionPrior = [];
                if (this.myConfig.items) {
                    this.myConfig.items.forEach(el => {
                        this.initComnonField(el,condition,datatype,conditionPrior);
                    });
                }
                this.condition = condition;
                this.dataType = datatype;
                this.conditionPrior = conditionPrior;
            },
            initComnonField(el,condition,datatype,conditionPrior) {
                if (el.children && el.children.length > 0) {
                    for (let i=0;i<el.children.length; i++) {
                        this.initComnonField(el.children[i],condition,datatype,conditionPrior);
                    }
                } else {
                    if (el.condition || el.conditionPrior) {
                        let obj = {};
                        obj.code = el.name;
                        if (el.label) {
                            obj.name = el.label;
                        } else {
                            obj.name = el.name;
                        }
                        let tempObj = this.$Method.copy(el);
                        tempObj.readOnly = false;
                        tempObj.visible = true;
                        tempObj.defaultValue = '';
                        tempObj.required = false;
                        obj.config = tempObj;
                        datatype[obj.code] = tempObj;
                        condition.push(obj);
                        if (el.conditionPrior) {
                            conditionPrior.push(obj);
                        }
                    }
                }
            },

            clear() {
                let temp = {};
                temp.action = 'condition_clear';
                if (this.customClear) {
                    this.customClear();
                }
                this.$emit('conditionAction',temp);
            },
            ok() {
                let temp = {};
                temp.action = 'condition_ok';
                this.$emit('conditionAction',temp);
            },
            cancel() {
                let temp = {};
                temp.action = 'condition_cancel';
                this.$emit('conditionAction',temp);
            },
            search() {
                let temp = {};
                temp.action = 'condition_search';
                temp.para = this.getQuerySearchParam();
                this.$emit('conditionAction',temp);
            },
            getQuerySearchParam() {
                let result={};
                if (!this.showCondition) {
                    return result;
                }
                if (this.customGetQuerySearchParam) {
                    return this.customGetQuerySearchParam();
                }

            },
            getCommomParam(name,value,type) {
                let param = {};
                if (this.myConfig.response === 'report') {
                    param[name] = value;
                    return param;
                }
                if (type === '2') {
                    param[name] = {'LIKE':'%' + value + '%'};
                } else if (type === '1') {
                    param[name] = {'==': value};
                } else if (type === '5') {
                    param[name] = {'<>': value};
                } else if (type === '3') {
                    param[name] = {'>=': value};
                } else if (type === '4') {
                    param[name] = {'<=': value};
                } else if (type === '6') {
                    param[name] = value;
                } 
                return param;
            },
        }
        
        
    };
</script>