<template>
    <div>

<!--    <el-form :rules="rulesObj" :model="currentPageDataObj[scope.$index]">-->
        <span v-if="showField.combine == '0' && showField.combineType != '1'" v-show="isShowRedact[scope.$index]">
<!--            {{scope.row[showField.fieldName + "_" + showField.formId]}}-->
          {{formatterColumnCard(scope.row,index,scope.$index)}}
        </span>

        <!--<el-form-item :prop="showField.fieldId">-->
        <!-- input 输入框-->
         <el-input
            v-if="showField.combine == '0' && showField.combineType != '1' && (showFieldType[showField.fieldId] ==='input' ||
                                            showFieldType[showField.fieldId] ==='textarea' || showFieldType[showField.fieldId] ==='password') && !isShowRedact[scope.$index]"
            v-model="scope.row[showField.fieldName + '_' + showField.formId]"
            :disabled="showContentField[showField.fieldId].editable != 'true'"
            :type="showContentField[showField.fieldId].controlType"
            :show-password="showContentField[showField.fieldId].controlType === 'password'"
            :size="tableviewsData.formSize"
            :defaultVal="showContentField[showField.fieldId].defaultVal">
         </el-input>



        <!-- time 时间控件-->
    <pl-date-picker
            v-if="showField.combine == '0' && showField.combineType != '1' && (showFieldType[showField.fieldId] ==='time' && !isShowRedact[scope.$index])"
            v-model="scope.row[showField.fieldName + '_' + showField.formId]"
            :type="formatterDateType(timeSpecialSet[showField.fieldId].displayFormatter)"
            :format="(timeSpecialSet[showField.fieldId].displayFormatter || 'yyyy-MM-dd')"
            :value-format="(timeSpecialSet[showField.fieldId].displayFormatter || 'yyyy-MM-dd')"
            :disabled="showContentField[showField.fieldId].editable !='true'"
            :editable="showContentField[showField.fieldId].editable ==='true'"
            :placeholder="showContentField[showField.fieldId].placeholder"
            :size="tableviewsData.formSize"
            :maxDate="getMaxDate(timeSpecialSet[showField.fieldId],scope.row)"
            :minDate="getMinDate(timeSpecialSet[showField.fieldId],scope.row)"
            :style="{'width':'100%'}">
    </pl-date-picker>



    <!--radio 自定义单选按钮-->
    <el-radio-group
            v-if="showField.combine == '0' && showField.combineType != '1' &&
            (showFieldType[showField.fieldId] ==='radio' && timeSpecialSet[showField.fieldId].radioType === 'radio' && timeSpecialSet[showField.fieldId].radioType != 'sysData' && !isShowRedact[scope.$index])"
            v-model="scope.row[showField.fieldName + '_' + showField.formId]"
            :disabled="showContentField[showField.fieldId].editable != 'true'">
        <el-radio  v-for="(option,index) in timeSpecialSet[showField.fieldId].optionSetting" :label="option.label" :key="index">{{option.value}}</el-radio>
    </el-radio-group>


    <!--radio 数据字典单选按钮-->
    <pl-data-radio-box
            v-if="showField.combine == '0' && showField.combineType != '1' &&
            (showFieldType[showField.fieldId] ==='radio' && timeSpecialSet[showField.fieldId].radioType === 'sysData' && !isShowRedact[scope.$index])"
            v-model="scope.row[showField.fieldName + '_' + showField.formId]"
            :field="showField.fieldId"
            :typeId="timeSpecialSet[showField.fieldId].dataDic"
            :disabled="showContentField[showField.fieldId].editable != 'true'">
    </pl-data-radio-box>



        <!--checkbox 自定义复选框-->
        <pl-checkbox-group
                v-if="showField.combine == '0' && showField.combineType != '1' &&
               (showFieldType[showField.fieldId] ==='checkbox' && timeSpecialSet[showField.fieldId].checkboxType === 'checkbox' && !isShowRedact[scope.$index])"
                v-model="checkboxValue[showField.fieldId]"
                :value="checkboxValue[showField.fieldId] || []"
                :disabled="showContentField[showField.fieldId].editable != 'true'"
                :checkboxArr="timeSpecialSet[showField.fieldId].optionSetting || []"
                :size="tableviewsData.formSize"
                @input="getCheckboxValue">
        </pl-checkbox-group>



    <!--checkbox 数据字典复选框-->
        <pl-data-checkbox
                v-if="showField.combine == '0' && showField.combineType != '1' &&
                (showFieldType[showField.fieldId] ==='checkbox' && timeSpecialSet[showField.fieldId].checkboxType === 'sysData' && !isShowRedact[scope.$index])"
                v-model="checkboxValue[showField.fieldId]"
                :value="checkboxValue[showField.fieldId] || []"
                :typeId="timeSpecialSet[showField.fieldId].dataDic"
                :size="tableviewsData.formSize"
                :disabled="showContentField[showField.fieldId].editable != 'true'"
                :field="showField.fieldId">
        </pl-data-checkbox>





        <!--contactParty 联系方-->
        <pl-org-select
                v-if="showField.combine == '0' && showField.combineType != '1' &&
                (showFieldType[showField.fieldId] ==='contactParty'  && !isShowRedact[scope.$index])"
                v-model="currentPageDataObj[scope.$index]"
                :value="currentPageDataObj[scope.$index]"
                :prop="showField.fieldId"
                :checkType="timeSpecialSet[showField.fieldId].muliti === '是' ? 'checkbox' : 'radio'"
                :backfillField="timeSpecialSet[showField.fieldId].callbackField"
                :contactType="timeSpecialSet[showField.fieldId].contactType"
                :showName:="timeSpecialSet[showField.fieldId].showName"
                :buttonName="timeSpecialSet[showField.fieldId].buttonName"
                :buttonIcon="timeSpecialSet[showField.fieldId].buttonIcon"
                :ignoreUper="true"
                :contactValue="getoptFormData(scope.$index,showField.fieldId)">
        </pl-org-select>



    <!--自定义下拉框-->
    <pl-data-select
            v-if="showField.combine == '0' && showField.combineType != '1' &&
                                            (showFieldType[showField.fieldId] ==='select' &&  timeSpecialSet[showField.fieldId].selectType != 'orgTree'
                                            &&  timeSpecialSet[showField.fieldId].selectType != 'posTree' && timeSpecialSet[showField.fieldId].selectType != 'sysData'
                                            && timeSpecialSet[showField.fieldId].selectType != 'remote' && !isShowRedact[scope.$index])"
            v-model="scope.row[showField.fieldName + '_' + showField.formId]"
            :disabled="showContentField[showField.fieldId].editable != 'true'"
            :multiple="timeSpecialSet[showField.fieldId].muliti === 'true'"
            :allow-create="(!timeSpecialSet[showField.fieldId].filterable || timeSpecialSet[showField.fieldId].filterable === 'true') && timeSpecialSet[showField.fieldId].alloCreate === 'true'"
            :filterable="!timeSpecialSet[showField.fieldId].filterable || timeSpecialSet[showField.fieldId].filterable === 'true'"
            :size="tableviewsData.formSize"
            :value="scope.row[showField.fieldName + '_' + showField.formId]"
            :isDiy="true"
            :valueArr="timeSpecialSet[showField.fieldId].optionSetting">
    </pl-data-select>


    <!--用户信息下拉框-->
    <pl-data-select
                    v-if="showField.combine == '0' && showField.combineType != '1' &&
                    (showFieldType[showField.fieldId] === 'userSelect' &&  timeSpecialSet[showField.fieldId].selectType === 'userTree'
                    && !isShowRedact[scope.$index])"
                    v-model="scope.row[showField.fieldName + '_' + showField.formId]"
                    :isUsertree="true"
                    :field="showField.fieldId"
                    :multiple="timeSpecialSet[showField.fieldId].muliti === 'true'"
                    :backfillField="timeSpecialSet[showField.fieldId].callbackField"
                    :formData="currentPageDataObj[scope.$index]"
                    :saveType="timeSpecialSet[showField.fieldId].saveType"
                    :placeholder="showContentField[showField.fieldId].placeholder"
                    :disabled="showContentField[showField.fieldId].editable != 'true'"
                    :filterable="true"
                    :size="tableviewsData.formSize"
                    :value="scope.row[showField.fieldName + '_' + showField.formId]"
                    :measurementShow="timeSpecialSet[showField.fieldId].measurement">
    </pl-data-select>


    <!--组织树-->
    <pl-data-select
            v-if="showField.combine == '0' && showField.combineType != '1' &&
                                            (showFieldType[showField.fieldId] === 'userSelect' &&  timeSpecialSet[showField.fieldId].selectType === 'orgTree'
                                            && !isShowRedact[scope.$index])"
            v-model="dataObj[showField.fieldId]"
            :isOrgtree="true"
            :orgDim="timeSpecialSet[showField.fieldId].orgDim"
            :orgLevel="timeSpecialSet[showField.fieldId].orgLevel"
            :saveType="timeSpecialSet[showField.fieldId].saveType"
            :backfillField="timeSpecialSet[showField.fieldId].callbackField"
            :formData="currentPageDataObj[scope.$index]"
            :field="showField.fieldId"
            :multiple="timeSpecialSet[showField.fieldId].muliti === 'true'"
            :placeholder="showContentField[showField.fieldId].placeholder"
            :disabled="showContentField[showField.fieldId].editable != 'true'"
            :filterable="timeSpecialSet[showField.fieldId].alloCreate =='true'?true:false"
            :size="tableviewsData.formSize"
            :changeField="changeField"
            :value="scope.row[showField.fieldName + '_' + showField.formId]"
            :openTree="true">
    </pl-data-select>

    <!--岗位树-->
    <pl-data-select
            v-if="showField.combine == '0' && showField.combineType != '1' &&
                                            (showFieldType[showField.fieldId] === 'userSelect' &&  timeSpecialSet[showField.fieldId].selectType === 'posTree'
                                            && !isShowRedact[scope.$index])"
            v-model="dataObj[showField.fieldId]"
            :is-postree="true"
            :orgDim="timeSpecialSet[showField.fieldId].orgDim"
            :orgLevel="timeSpecialSet[showField.fieldId].orgLevel"
            :saveType="timeSpecialSet[showField.fieldId].saveType"
            :backfillField="timeSpecialSet[showField.fieldId].callbackField"
            :formData="currentPageDataObj[scope.$index]"
            :field="showField.fieldId"
            :multiple="timeSpecialSet[showField.fieldId].muliti === 'true'"
            :placeholder="showContentField[showField.fieldId].placeholder"
            :disabled="showContentField[showField.fieldId].editable != 'true'"
            :filterable="timeSpecialSet[showField.fieldId].alloCreate =='true'?true:false"
            :size="tableviewsData.formSize"
            :changeField="changeField"
            :value="scope.row[showField.fieldName + '_' + showField.formId]"
            :openTree="true">
    </pl-data-select>


    <!--链接远程加载-->
    <pl-data-select
            v-if="showField.combine == '0' && showField.combineType != '1' &&
                                            (showFieldType[showField.fieldId] === 'select' &&  timeSpecialSet[showField.fieldId].selectType === 'remote' && !isShowRedact[scope.$index])"
            v-model="scope.row[showField.fieldName + '_' + showField.formId]"
            :typeId="''"
            :field="showField.fieldId"
            :parentId="timeSpecialSet[showField.fieldId].parentId"
            :multiple="timeSpecialSet[showField.fieldId].muliti ==='true'"
            :reqUrl="getReqUr(showField.fieldId)"
            :placeholder="showContentField[showField.fieldId].placeholder"
            :disabled="showContentField[showField.fieldId].editable != 'true'"
            :filterable="true"
            :allowCreate="timeSpecialSet[showField.fieldId].alloCreate === 'true'"
            :size="tableviewsData.formSize"
            :measurementShow="timeSpecialSet[showField.fieldId].measurement"
            :labelField="timeSpecialSet[showField.fieldId].textField"
            :valueField="timeSpecialSet[showField.fieldId].valueField"
            :defultValue="timeSpecialSet[showField.fieldId].defaultVal">
    </pl-data-select>


    <!--数据字典下拉框-->
    <pl-data-select
            v-if="showField.combine == '0' && showField.combineType != '1' &&
                                            (showFieldType[showField.fieldId] === 'select' &&  timeSpecialSet[showField.fieldId].selectType === 'sysData' && !isShowRedact[scope.$index])"
            v-model="scope.row[showField.fieldName + '_' + showField.formId]"
            :typeId="timeSpecialSet[showField.fieldId].dataDic || ''"
            :field="showField.fieldId"
            :parentId="timeSpecialSet[showField.fieldId].parentId"
            :multiple="timeSpecialSet[showField.fieldId].muliti ==='true'"
            :placeholder="showContentField[showField.fieldId].placeholder"
            :disabled="showContentField[showField.fieldId].editable != 'true'"
            :filterable="true"
            :allowCreate="timeSpecialSet[showField.fieldId].alloCreate === 'true'"
            :size="tableviewsData.formSize"
            :value="scope.row[showField.fieldName + '_' + showField.formId]"
            :measurementShow="timeSpecialSet[showField.fieldId].measurement">
    </pl-data-select>



        <!--级联下拉框(单下拉框) -->
    <pl-cascade-single
            v-if="showField.combine == '0' && showField.combineType != '1' &&
                                            (showFieldType[showField.fieldId] ==='cascadeSelectOne'  && !isShowRedact[scope.$index])"
            :dataTypeId="timeSpecialSet[showField.fieldId].dataTypeId"
            :parentId="timeSpecialSet[showField.fieldId].parentId"
            :fieldId="showField.fieldId"
            :formData="currentPageDataObj[scope.$index]"
            :isMuliti="timeSpecialSet[showField.fieldId].muliti ==='true'"
            :defaultValue="timeSpecialSet[showField.fieldId].defaultValue">
    </pl-cascade-single>


    <!--级联下拉框(多下拉框)-->
        <el-form>
        <pl-cascade-select-form
                v-if="(showFieldType[listFields[index].fieldId+','+listFields[index].combineCols] === 'cascadeSelect'  && !isShowRedact[scope.$index])"
                :formData="currentPageDataObj[scope.$index]"
                :field="listFields[index].fieldId+','+listFields[index].combineCols"
                :typeId="timeSpecialSet[listFields[index].fieldId+','+listFields[index].combineCols].dataTypeId"
                :parentId="timeSpecialSet[listFields[index].fieldId+','+listFields[index].combineCols].parentId"
                :hasParentJudge="timeSpecialSet[listFields[index].fieldId+','+listFields[index].combineCols].parentId ? 'true':'false'"
                :size="tableviewsData.formSize"
                :disabled="getDisabledCas(timeSpecialSet[listFields[index].fieldId+','+listFields[index].combineCols])"
                :rules="getRules(timeSpecialSet[listFields[index].fieldId+','+listFields[index].combineCols])">
        </pl-cascade-select-form>
        </el-form>
        <!--</el-form-item>-->
        <!-- 除以上定义外的控件  显示span -->
        <span  v-if="(showFieldType[listFields[index].fieldId+','+listFields[index].combineCols] ==='cascadeSelect' && isShowRedact[scope.$index])">
            {{currentPageDataObj[scope.$index][listFields[index].fieldId]}}
        </span>
    <!--</el-form>-->


    </div>
</template>

<script>

    export default {
        name: "list-view-preview-redact",
        //mixins:[formMixins],
        components: {
            PlActiveCount: () => import("../../../components/public/plActiveCount"),
            PlCascadeSelectForm: () => import("../../../components/public/plCascadeSelectForm"),
            PlCascadeSingle: () => import("../../../components/public/plCascadeSingle"),
            PlOrgSelect: () => import("../../../components/public/plOrgSelect"),
            PlDataRadioBox: () => import("../tableViewsDialog/tableviewRenderComp/plDataRadioBox"),
            TableFormRender: () => import("../formRender/tableFormRender"),
            PlDateTimePicker: () => import("../../../components/public/plDateTimePicker"),
            PlCascadeSelect: () => import("../../../components/public/plCascadeSelect"),
            PlDataCheckbox: () => import("../tableViewsDialog/tableviewRenderComp/plDataCheckbox"),
            plDataSelect: () => import("../../mix/plDataSelect"),
            PlCheckboxGroup: () => import("../tableViewsDialog/tableviewRenderComp/plCheckboxGroup")
        },
        props: {
            scope: {type: Object, required: true},//行对象
            showField: {type: Object, required: true},//显示的列
            isShowRedact: {type: Array,required: true},//是否可编辑
            showFieldType: {type: Object, required: true},//字段的类型
            showContentField: {type: Object, required: true},//字段配置
            tableviewsData:{type: Object, required: true},//样式
            timeSpecialSet:{type: Object, required: true},//字段内容配置
            currentPageDataObj:{type: Object, required: true},//当前行数据
            listFields:{type: Array,required: false},//级联多需要的配置
            checkboxValue:{type: Object, required: false},//复选框值
            formFieldObj:{type: Object, required: false},//字段信息
            index:0,//当前下标
            formatterColumnCard:{type:Function,default:null},
            parentOptFormData:{type: Object, required: false},
        },
        data(){
            return {
              dataObj:this.currentPageDataObj[this.scope.$index],
            }
        },
        watch:{
        },
        methods:{

            /*
             * 获取是否只读
             * */
            getDisabledCas(specialSet){
                let disabledCas = "";//是否只读
                let cascadeSetArr = [];
                let cascadeSpecial = specialSet || "";
                if(cascadeSpecial){
                    cascadeSetArr = cascadeSpecial.cascadeSetArr || [];
                }
                let flowDisable = false;
                cascadeSetArr.forEach(setObj => {
                    disabledCas += "," + (setObj.readOnly == '1' || flowDisable);

                });
                if(disabledCas){
                    disabledCas = disabledCas.substring(1);
                }
                return disabledCas;
            },
            /*
            * 获取校验规则
            * */
            getRules(specialSet){
                let cascadeRules = {};//校验规则
                let cascadeSetArr = [];
                let cascadeSpecial = specialSet || "";
                if(cascadeSpecial){
                    cascadeSetArr = cascadeSpecial.cascadeSetArr || [];
                }
                cascadeSetArr.forEach(setObj => {
                    if(setObj.isNull == '1' && setObj.readOnly != '1'){
                        cascadeRules[setObj.fieldId] = {required: true, message: '必填'};
                    }
                });
                return cascadeRules;
            },
            /**
             * 格式化日期type
             **/
            formatterDateType(displayFormatter){
                let type = "date";
                if(displayFormatter === 'yyyy-MM-dd'){
                    type = 'date';
                }else if(displayFormatter === 'yyyy-MM-dd HH:mm:ss'){
                    type = 'datetime';
                }else if(displayFormatter === 'yyyy'){
                    type = 'year';
                }else if(displayFormatter === 'yyyy-MM'){
                    type = 'month';
                }else{
                    type = 'datetime';
                }
                return type;
            },
            getMaxDate(dataSetting,optFormData){

                let endTime = "";  //结束时间   最大时间
                let endTimeArr = []; //结束时间数组

                //计算结束时间(取最小日期 系统时间、比较字段,自定义时间)
                if(dataSetting.maxDate.includes('自定义时间')){
                    endTime = dataSetting.diyTimeMax; //自定义最大时间
                }
                if(dataSetting.maxDate.includes('系统时间')){
                    endTime = this.viewDateFormat(new Date(),dataSetting.displayFormatter || 'yyyy-MM-dd');
                }
                if(dataSetting.maxDate.includes('参考表单字段')){
                    (dataSetting.referFieldMax || []).forEach(refField =>{
                        let refId = this.showContentField[refField].field + "_" +this.showContentField[refField].formId;
                        if(optFormData[refId]){
                            let maxTime = optFormData[refId];
                            if(isNaN(maxTime) && !isNaN(Date.parse(maxTime))){
                                endTime =   this.viewDateFormat(maxTime,dataSetting.displayFormatter || 'yyyy-MM-dd');
                            }
                        }
                    });
                }
                if(endTime){
                    endTimeArr.push(endTime);//最大时间
                }
                //获取最大时间数组中最的最小值
                let endDate =  this.getMinDateInMaxDateArr(endTimeArr);

                return endDate;
            },
            getMinDate(dataSetting,optFormData){
                let startTime = ""; //开始时间
                let startTimeArr = []; //开始时间数组

                if(dataSetting.minDate.includes('自定义时间')){
                    startTime = dataSetting.diyTimeMin; //自定义最小时间
                }
                if(dataSetting.minDate.includes('系统时间')){
                    startTime = this.viewDateFormat(new Date(),dataSetting.displayFormatter || 'yyyy-MM-dd');
                }
                if(dataSetting.minDate.includes('参考表单字段')){
                    (dataSetting.referFieldMin || []).forEach(refField =>{
                        let refId = this.showContentField[refField].field + "_" +this.showContentField[refField].formId;
                        if(optFormData[refId]){
                            let minTime = optFormData[refId];
                            if(isNaN(minTime) && !isNaN(Date.parse(minTime))){
                                startTime =   this.viewDateFormat(minTime,dataSetting.displayFormatter || 'yyyy-MM-dd');
                            }
                        }
                    });
                }
                if(startTime){
                    startTimeArr.push(startTime);//最大时间
                }
                //获取最大时间数组中最的最小值
                let startDate = this.getMaxDateInMinDateArr(startTimeArr);

                return startDate;
            },

            /**
             * 获取日期数组中的最小值
             * */

            getMinDateInMaxDateArr(maxTimeArr){
                let maxTime = '';
                if(maxTimeArr && maxTimeArr.length > 0){
                    maxTime = maxTimeArr.reduce(function (a,b) {
                        return new Date(a).getTime() <= new Date(b).getTime() ? a:b;

                    });
                }
                return maxTime;
            },

            getMaxDateInMinDateArr(minTimeArr){
                let minTime = '';
                if(minTimeArr && minTimeArr.length > 0){
                    minTime =     minTimeArr.reduce(function (a,b) {
                        return   new Date(a).getTime() >= new Date(b).getTime() ? a:b;
                    });
                }
                return minTime;
            },
            /**
             * 时间格式化方法（查看表单视图时用到）
             * @param d
             * @param fmt
             * @return {string|void}
             */
            viewDateFormat(d, fmt) {
                if (d) {
                    let date = new Date(d);
                    let o = {
                        "M+": date.getMonth() + 1,                 //月份
                        "d+": date.getDate(),                    //日
                        "H+": date.getHours(),                   //小时
                        "m+": date.getMinutes(),                 //分
                        "s+": date.getSeconds(),                 //秒
                        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                        "S": date.getMilliseconds()             //毫秒
                    };
                    if (/(y+)/.test(fmt)){
                        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
                    }
                    for (let k in o){
                        if (new RegExp("(" + k + ")").test(fmt)){
                            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                        }
                    }
                } else {
                    fmt = '';
                }
                return fmt;
            },
            /*
             * 获取字段id对应的数据
             * */
            getoptFormData(index,id){
                let opt =  this.currentPageDataObj[index];
                return opt[id];
            },
            getCheckboxValue(val){//获取复选框值
                this.checkboxValue[this.showField.fieldId] = val;
            },
          getReqUr(fieldId){
            let reqUrl = this.timeSpecialSet[fieldId].reqUrl;
            if(reqUrl.indexOf("${parentOptFormData.") > -1){
              let param =  reqUrl.substring(reqUrl.indexOf("${parentOptFormData.")+20,reqUrl.indexOf('}'));
              let value = "";
              if(param && this.parentOptFormData[param]){
                value = this.parentOptFormData[param];
                param = "${parentOptFormData." + param + "}";
                reqUrl = this.platStringUtil.replaceAll(reqUrl,param,value);
              }
            }
            return reqUrl;
          },
          changeField(field,result){
            this.$set(this.currentPageDataObj[this.scope.$index],field,result);
            this.$forceUpdate();
          },
        }
    }
</script>

<style scoped>

</style>