<!--修改内容-->
<template>
    <!--事件定义的实际都是 在el-form下，ref 都是 eventForm  -->
    <el-form
            ref="eventForm"
            :model="eventData" label-width="120px"
            :validate-on-rule-change="false" size="small"
            style="width: 100%;height: 100%;padding:5px;box-sizing: border-box;">
        <el-row>
            <el-col :span="8">
                <el-form-item label="值类型" prop="dataSource" :rules="plValidate('required')">
                    <el-select v-model="eventData.dataSource" style="width: 100%" @change="changeDataSource(eventData)">
                        <el-option v-for="(item,index) in dataSourceArr"
                                   :key="index" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item label="数据" v-if="eventData.dataSource &&'data,formField'.includes(eventData.dataSource)" prop="valType"
                              :rules="plValidate('required')">
                    <!--数据字典-->
                    <el-select v-if="eventData.dataSource == 'data'"  style="width: 100%"
                               v-model="eventData.valType"  filterable @change="changeDic(eventData)">
                        <el-option v-for="item in dataTypeArr" :key="item.id" :value="item.id" :label="item.typeName">
                        </el-option>
                    </el-select>
                    <!--用于表单字段-->
                    <el-select v-if="eventData.dataSource == 'formField'" v-model="eventData.valType" style="width: 100%">
                        <el-option v-for="item in Object.keys(formBaseObj)"
                                   :key="item" :value="item"
                                   :label="formBaseObj[item].formName">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="8" v-if="eventData.dataSource != 'null'">
                <el-form-item label="值" v-if="eventData.dataSource" prop="value">
                    <!--数据字典-->
                    <tree-select v-if="eventData.dataSource == 'data'" style="width:100%;" @change="dataTypeChange(eventData)"
                                 v-model="eventData.value" :options="dataTreeObj[eventData.valType] || []"
                                 placeholder="请选择"  filterable :append-to-body="true"/>
                    <!--用于表单字段-->
                    <el-select v-if="eventData.dataSource == 'formField'" v-model="eventData.value" style="width: 100%">
                        <el-option v-for="item in formAndFieldObj[eventData.valType]"
                                   :key="item.id" :value="item.id"
                                   :label="item.chfieldName"></el-option>
                    </el-select>
                    <!--缺省值-->
                    <el-select v-if="eventData.dataSource == 'default'"
                               v-model="eventData.value" style="width: 100%">
                        <el-option v-for="item in defaultArr" :key="item.value"
                                   :value="item.value" :label="item.label">
                        </el-option>
                    </el-select>
                    <!--自定义-->
                    <el-input type="textarea" v-if="eventData.dataSource == 'diy'" v-model="eventData.value"></el-input>
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>
</template>

<script>
    import EventParentConfig from "../../eventParentConfig";
    import {Replace} from "sasp-base";
    export default {
        name: "edit-content-config",
        extends: EventParentConfig,
        eventType: 'tableView',
        eventName:'修改内容',
        props:{
            dataTypeArr:{type:Array,default:() => {return [];}},
            dataTreeObj:{type:Object,default:() => {return {};}},
            formBaseObj:{type:Object,default:() => {return {};}},
            formAndFieldObj:{type:Object,default:() => {return {};}},
        },
        data(){
            return{
                dataSourceArr:[
                    {label:"缺省值",value:"default"},
                    {label:"数据字典",value:"data"},
                    {label:"表单字段",value:"formField"},
                    {label:"空值",value:"null"},
                    {label:"自定义",value:"diy"}
                ],

                //缺省值类型
                defaultArr:[
                    {label:"用户名",value:"LOGINUSER.userName"},
                    {label:"用户ID",value:"LOGINUSER.userId"},
                    {label:"用户账户",value:"LOGINUSER.userAccount"},
                    {label:"用户类型",value:"LOGINUSER.userType"},
                    {label:"关联ID",value:"LOGINUSER.linkId"},
                    {label:"组织名称",value:"LOGINUSER.mainOrgName"},
                    {label:"组织ID",value:"LOGINUSER.mainOrgId"},
                    {label:"部门名称",value:"LOGINUSERDEPT.orgName"},
                    {label:"部门ID",value:"LOGINUSERDEPT.id"},
                    {label:"组织岗位用户ID",value:"LOGINUSER.orgPosUserIds"},
                    {label:"用户所属省ID",value:"LOGINUSERDEPT.province"},
                    {label:"用户所属市ID",value:"LOGINUSERDEPT.city"},
                    {label:"用户所属区ID",value:"LOGINUSERDEPT.area"},
                    {label:"用户所属街道ID",value:"LOGINUSERDEPT.street"},
                    {label:"用户所属省名称",value:"LOGINUSERDEPT.provinceName"},
                    {label:"用户所属市名称",value:"LOGINUSERDEPT.cityName"},
                    {label:"用户所属区名称",value:"LOGINUSERDEPT.areaName"},
                    {label:"用户所属街道名称",value:"LOGINUSERDEPT.streetName"},
                    {label:"系统时间",value:"SYSTIME.yearMoNday"},
                ],
            }
        },
        created(){
            this.initData();
        },
        methods:{
            initData(){
                this.eventSource = "form";
                this.eventName = "修改标题";
                if(!this.eventData.dataSource){
                    this.$set(this.eventData,'dataSource','');//默认值
                }
            },
            /**
             * 切换值类型
             * @param eventData
             */
            changeDataSource(eventData){
                this.$set(eventData,'valType','');
                if(eventData.dataSource == 'data'){
                    this.$set(eventData,'value',null);
                }else{
                    this.$set(eventData,'value','');
                }
            },
            /**
             * 切换
             * @param eventData
             */
            dataTypeChange(eventData){
                this.$set(eventData,'value',null);
            },

            /**
             * 切换数据后值清空
             * @param eventData
             */
            changeDic(eventData){
                this.$set(eventData,'value',null);
            },

            /**
             * 校验规则，不写默认通过
             * @returns {boolean}
             */
            validateMethod(){
                let result = false;
                this.$refs.eventForm.validate(valid => {
                    result = valid;
                })
                return result;
            },

            /**
             * 如果是表单规则（eventType: 'tableView'）走此渲染方法
             * @vm 当前整个表单对象
             * @CTX 后台url前缀 如 http://localhost:8080
             * @axios axios 请求对象
             * @formData 响应字段配置
             * @affectContent 响应配置（表单规则配置好数据）
             *
             */
            renderTableViewConfig(vm,CTX,axios,formData,affectContent){
                let field = affectContent.field;
                let val = '';
                let value = affectContent.value;
                if(affectContent.dataSource == 'formField'){ //表单字段
                    val = vm.optFormData[value] || "";
                }else if(affectContent.dataSource == 'default'){ //缺省值
                    if(value.indexOf("LOGINUSERDEPT") > -1){
                        val = Replace.def(vm.userDept,value) || "";
                    }else if(value.indexOf("LOGINUSER") > -1){
                        val = Replace.def(vm.loginUser,value) || "";
                    }else {//系统时间
                        if(formData[field]){
                            val = vm.viewDateFormat(new Date(),
                                formData[field].displayFormatter || 'yyyy-MM-dd');
                        }
                    }
                }else if(affectContent.dataSource == 'null'){ //缺省值
                    val = null;
                }else { //自定义
                    val = value;
                }
                vm.$set(vm.optFormData,field,val); //修改内容
                //如果是 标题栏、分隔线、
                if(formData[field]){
                    if (formData[field].controlType == "titleBox"){
                        vm.$set(formData[field],'title',val); //修改内容
                    }else if(formData[field].controlType == "dividingLine"){
                        vm.$set(formData[field],'content',val); //修改内容
                    }else if(formData[field].controlType == 'displayBox'){ //展示框
                        vm.$set(formData[field],'defaultVal',val); //修改内容
                    }
                }
            }
        }
    }
</script>

<style scoped>

</style>