<template>
    <div class="edit-and-view-style clearfix" style="height: 100%;">
        <div v-if="businessType !== 2" class="title-line-outer">
            <div class="title-line-inner">
                <a-icon type="environment" class="position-style"/>
                <span class="menu-name-style">{{ menu_one }}</span>
                <template v-if="menu_two && menu_two!== ''">
                    <a-icon type="right" class="position-style" style="margin-left: 0.5rem"/>
                    <span class="menu-name-style">{{ menu_two }}</span>
                </template>
                <a-button v-if="parseInt(dataNumType) !== 2 || this.businessType !== 1" class="left-top-add-btn-style" @click="returnBack">返回</a-button>
                <template v-if="this.businessType === 1">
                    <!--上报业务-->
                    <template v-if="parseInt(dataNumType) === 2">
                        <!--一人一条的上报操作-->
                        <a-button class="left-top-add-btn-style" style="right: 12rem" :disabled="is_loading === true || type !== 3" type="primary" @click="editForm">编辑</a-button>
                        <a-button class="left-top-add-btn-style" style="right: 7rem" :disabled="is_loading === true || type === 3" type="primary" @click="submitFormData">提交</a-button>
                        <a-button class="left-top-add-btn-style" :disabled="type === 3" @click="cancelEdit">取消</a-button>
                    </template>
                    <template v-else-if="type < 3">
                        <!--一人多条的上报操作-->
                        <a-button class="left-top-add-btn-style" style="right: 7rem" :disabled="is_loading === true" type="primary" @click="submitFormData">提交</a-button>
                    </template>
                </template>
                <a-button v-if="businessType === 4 && type === 2" class="left-top-add-btn-style" style="right: 7rem" :disabled="is_loading === true" type="primary" @click="submitFormData">提交</a-button>
                <template v-if="this.businessType === 3 && this.type === 4">
                    <!--审核业务的审核页面-->
                    <a-button class="left-top-add-btn-style" style="right: 12.5rem" type="primary" :disabled="is_loading === true" @click="approval(1)">通过</a-button>
                    <a-button class="left-top-add-btn-style" style="background-color:#fc012c;right: 7rem;border-color:#fc012c" type="primary" :disabled="is_loading === true" @click="approval(-1)">未通过</a-button>
                </template>
            </div>
        </div>
<!--        <template v-if="is_loading === false">-->
        <div v-if="is_loading===false" class="clearfix" :style="showScroll?{height: businessType !== 2 ? 'calc(100% - 4rem)':'100%'}:null">
            <vue-scroll v-if="showScroll" :ops="ops">
                <div :style="{float:'left',width:isShowOperate?'65%':'100%',padding:'1rem 0'}">
                    <view-form :fieldsList="field_list?field_list:[]" :form_width="form_config?form_config.form_width:100" :is_edit="type < 3"/>
                </div>
                <template v-if="isShowOperate && operate_list && operate_list.length > 0">
                    <div style="float: right;width: 35%">
                        <BaseFlowLineItem v-for="operate in operate_list" :key="operate.operation_id" :data="operate" style="width: 100%"/>
                    </div>
                </template>
            </vue-scroll>
            <template v-else>
                <div :style="{float:'left',width:isShowOperate?'65%':'100%',padding:'1rem 0'}">
                    <view-form :fieldsList="field_list?field_list:[]" :form_width="form_config?form_config.form_width:100" :is_edit="type < 3"/>
                </div>
                <template v-if="isShowOperate && operate_list && operate_list.length > 0">
                    <div style="float: right;width: 35%">
                        <BaseFlowLineItem v-for="operate in operate_list" :key="operate.operation_id" :data="operate" style="width: 100%"/>
                    </div>
                </template>
            </template>
        </div>
<!--        </template>-->
        <a-spin v-if="is_loading" style="position:absolute;left: 46%;top: 25rem;" size="large" :tip="loadingTip"/>
        <modal-panel addChildType="slot" :modalTitle="'审核' + (approveType === 1?'通过':'未通过')" :show="showComment" modalWidth="50%" @callback="commentCallback">
            <ApprovalComment :type="approveType" ref="commentPanel"/>
        </modal-panel>
    </div>
</template>

<script>
    import StaticParams from '../../../global-llibs/staticParams'
    import {Icon,Button,Spin,Modal} from 'ant-design-vue'
    import TypesCheck from '../../../global-llibs/under-score/types-check'
    import ViewForm from './ViewForm'
    import FillingInterfaceConfig from '../teacherInfoFilling/interfaceConfig'
    import ApprovalInterfaceConfig from '../approve/interfaceConfig'
    import ManagementInterfaceConfig from '../teacherInfoManagement/interfaceConfig'
    import _ from 'lodash'
    import BaseFlowLineItem from '../../../components/common/baseFlowLineItem/BaseFlowLineItem'
    import ModalPanel from '../../../components/common/modal/ModalPanel'
    import ApprovalComment from '../approve/ApprovalComment'
    import CommonOperate from '../utils/commonOperate'
    import GUID_UUID from '../../../global-llibs/guid-uuid'
    /*
    * @props
    * pageType  1新建  2编辑  3查看 4审批
    * dataNumType 1一人多条  2一人一条
    * dataInfo 表单数据
    * showOperateList 是否显示操作过程列表
    * businessType  1上报业务   2查询业务   3审核业务
    *
    * event:
    * back 返回
    * submitSuccess 提交成功之后回调
    * approve 审批
    * */
    export default {
        name: "EditAndViewForm",
        props:{
            showScroll:{
                type:Boolean,
                default:true,
            },
            form_id:{
                type:Number
            },
            pageType:{
                type:Number,
                required:true
            },
            dataNumType:{
                type:Number,
                required:true
            },
            dataInfo:{
                type:Object,
                required:true
            },
            showOperateList:{
                type:Boolean,
                default:true
            },
            businessType:{
                type:Number,
                default:1
            },
            personInfo:{
                type:Object,
                default:function () {
                    return {
                        person_id:"",
                        person_name:"",
                        workers_no:"",
                        dept_name:"",
                        org_name:""
                    }
                }
            }
        },
        components:{
            AIcon:Icon,
            AButton:Button,
            ViewForm,
            ASpin:Spin,
            BaseFlowLineItem,
            ModalPanel,
            ApprovalComment
        },
        data:function () {
            return{
                ops:StaticParams.scrollOption,
                menu_one:StaticParams.menuLocation[0],
                menu_two:StaticParams.menuLocation[1],
                type:this.pageType,
                dataResource:_.cloneDeep(this.dataInfo),//原数据
                field_list:[],
                form_config:this.dataInfo.form_config_json && this.dataInfo.form_config_json !== ""?JSON.parse(this.dataInfo.form_config_json):null,
                form_data:this.dataInfo.form_data_list && this.dataInfo.form_data_list[0]?this.dataInfo.form_data_list[0]:{},
                operate_list:[],
                isShowOperate:TypesCheck.isBoolean(this.showOperateList)?this.showOperateList:false,
                is_loading:true,
                person_id:this.personInfo.person_id,
                org_name:this.personInfo.org_name,
                dept_name:this.personInfo.dept_name,
                dept_id:this.personInfo.dept_id,
                person_name:this.personInfo.person_name,
                workers_no:this.personInfo.workers_no,
                sonFormCount:0,//子表单数
                hasGetFormDataCount:0,//以获取数据的子表单数据
                mainFormErgodicComplete:false,//主表单是否遍历完成
                loadingTip:"加载数据中，请稍候...",
                approveType:1,
                showComment:false,
            }
        },
        methods:{
            approval:function(type){
                this.approveType = type;
                if (this.$refs.commentPanel){
                    this.$refs.commentPanel.content = undefined;
                    this.$refs.commentPanel.approveType = this.approveType;
                    this.$refs.commentPanel.showTip = false;
                }
                this.showComment = true;
            },
            commentCallback:function(type){
                if (type[0] === 'ok'){
                    if (this.approveType === -1 && (!this.$refs.commentPanel.content  || StaticParams.trim(this.$refs.commentPanel.content) === "")){
                        this.$refs.commentPanel.showTip = true;
                        return ;
                    }else {
                        this.is_loading = true;
                        this.InterfaceConfig.callInterface([{
                            url:ApprovalInterfaceConfig.saveCheckInfoByValueId.url,
                            method:ApprovalInterfaceConfig.saveCheckInfoByValueId.method,
                            params:{
                                org_id:this.BaseConfig.person_info_my.bureau_id,
                                system_id:10,
                                form_id:this.form_id,
                                value_ids:this.form_data.value_id,
                                check_flag:this.approveType,
                                check_info:this.$refs.commentPanel.content?this.$refs.commentPanel.content:"",
                                person_id:this.BaseConfig.userInfo.person_id_cookie,
                                dept_id:this.BaseConfig.person_info_my.dep_id,
                                update_ts:this.form_data.update_ts
                            },
                            isTestLogin:ApprovalInterfaceConfig.saveCheckInfoByValueId.isTestLogin
                        }],(data)=>{
                            let result = data[0].data;
                            if (result.code === 2000){
                                this.$toast("审核成功")
                                this.$emit("submitSuccess")
                            }
                        })
                    }
                }

                this.showComment = false
            },
            returnBack:function(){
                this.$emit("back");
            },
            getDicValue:function(value){
                let dicValue = ""
                if (value && value !== ""){
                    value = value.substring(5,value.length);//去掉"DICS#"
                    let ary = value.split("+");//分割多个数据
                    ary.forEach((item,index)=>{
                        let sonAry = item.split(",");//分割父子关系
                        sonAry.forEach((son,sonIndex)=>{
                            let dic = son.split("!");
                            dicValue += dic[1];
                            if (sonIndex < sonAry.length - 1){
                                dicValue += "，";
                            }
                        });
                        if (index < ary.length - 1){
                            dicValue += "，";
                        }
                    })
                }
                return dicValue;
            },
            //整理操作列表数据
            organizeOperateList:function(list){
                // eslint-disable-next-line no-debugger
                // debugger;
                let operate = [];
                if (list && TypesCheck.isArray(list) && list.length > 0){
                    list.forEach((item)=>{
                        //operation_type 1是已经审核过的数据（包括通过和未通过） 2是还未审核的变更数据  3是管理员导入或者变更的数据
                        if (!(this.businessType === 2 && item.operation_type === 2)){//查询功能页不展示未审核的变更数据
                            let record = {};
                            if (item.operation_type === 1){//审核信息
                                record.node_label = "审核人：" + item.check_person_name;
                                record.person_id = item.check_person_id;
                                record.node_status = "1";
                                record.time_stamp = item.check_time;
                                record.isHasStatusStamp = true;
                                record.statusStampText = item.check_flag === 1 ? "已通过":"未通过";
                                record.StatusStampBgColor = item.check_flag === 1 ? "green":"red";
                                record.isHasRemark = true;
                                record.remarkList = ["("+item.check_person_org_name+(item.check_person_dept_name && item.check_person_dept_name !== ""
                                && item.check_person_org_name !== item.check_person_dept_name?"/" + item.check_person_dept_name:"")+")"];
                                if (item.check_remark && item.check_remark !== ""){
                                    record.remarkList.push("审批意见：" + item.check_remark)
                                }
                            }else if (item.operation_type === 2){//待审核信息
                                record.node_label =  this.businessType === 1?"操作人：" + item.operation_person_name:"审核人：" + this.BaseConfig.userInfo.person_name_cookie;
                                record.person_id = this.businessType === 1?item.operation_person_id:this.BaseConfig.userInfo.person_id_cookie;
                                record.node_status = "0";
                                record.time_stamp = this.businessType === 1?item.operation_time:"";
                                record.isHasStatusStamp =  true;
                                record.statusStampText = "待审核";
                                record.StatusStampBgColor = "#f8b62b";
                                record.isHasRemark = true;
                                record.remarkList = this.businessType === 1?["("+item.operation_person_org_name+(item.operation_person_dept_name && item.operation_person_dept_name !== ""
                                && item.operation_person_org_name !== item.operation_person_dept_name?"/" + item.operation_person_dept_name:"")+")"]:
                                ["("+this.BaseConfig.person_info_my.bureau_name+(this.BaseConfig.person_info_my.dep_name && this.BaseConfig.person_info_my.dep_name !== ""
                                && this.BaseConfig.person_info_my.bureau_name !== this.BaseConfig.person_info_my.dep_name?"/" + this.BaseConfig.person_info_my.dep_name:"")+")"];
                            }else{//管理员导出/变更的数据
                                record.node_label =  "操作人：" + item.operation_person_name;
                                record.isAdmin = true;
                                record.person_id = item.operation_person_id;
                                record.node_status = "1";
                                record.time_stamp = item.operation_time;
                                record.isHasStatusStamp = false;
                                record.isHasRemark = true;
                                record.remarkList = ["("+item.operation_person_org_name+(item.operation_person_dept_name && item.operation_person_dept_name !== ""
                                    && item.operation_person_org_name !== item.operation_person_dept_name?"/" + item.operation_person_dept_name:"")+")"];
                            }

                            record.isHasChild = item.data_change_list && item.data_change_list.length > 0;//是否有数据变更

                            if (record.isHasChild === true){//有数据操作修改记录
                                item.data_change_list.forEach((field)=>{
                                    field.name = field.field_name;
                                    if ([8,9,10,11].some((item)=>{return item === field.field_type})){//不需要展示数据变化的字段
                                        field.old_value ="";
                                        field.new_value = "";
                                    }else if ([3,4,6,15,25,26].some((item)=>{return item === field.field_type})){//数据字典字段
                                        field.old_value = this.getDicValue(field.old_value);
                                        field.new_value = this.getDicValue(field.new_value);
                                    }else if([17,18].some((item)=>{return item === field.field_type})){// 部门  人员字段
                                        field.old_value = CommonOperate.getDeptPersonValue(field.old_value);
                                        field.new_value = CommonOperate.getDeptPersonValue(field.new_value);
                                    }
                                });
                                record.childList = [{childDataType:2,title:"操作记录",list:item.data_change_list}]

                                if (item.operation_type === 1 || (item.operation_type === 2 && this.businessType === 3)){
                                    record.childList[0].personInfo = {
                                        node_label:"操作人：" + item.operation_person_name,
                                        person_id:item.operation_person_id,
                                        time_stamp:item.operation_time,
                                        remarkList:["("+item.operation_person_org_name+(item.operation_person_dept_name && item.operation_person_dept_name !== ""
                                        && item.operation_person_org_name !== item.operation_person_dept_name?"/" + item.operation_person_dept_name:"")+")"]
                                    }
                                }
                            }
                            operate.push(record)
                        }
                    })
                }

                if (this.type < 3){
                    operate.push(this.addOperateRecord())
                }
                return operate
            },
            addOperateRecord:function(){
                let record = {
                    node_label:"操作人：" + this.BaseConfig.userInfo.person_name_cookie,
                    node_status:"0",
                    isAdmin:this.businessType === 4,
                    person_id:this.BaseConfig.userInfo.person_id_cookie,
                    time_stamp:"",
                    isHasStatusStamp:false,
                    isHasRemark:true,
                    remarkList: ["("+this.BaseConfig.person_info_my.bureau_name+(this.BaseConfig.person_info_my.dep_name && this.BaseConfig.person_info_my.dep_name !== ""
                    && this.BaseConfig.person_info_my.bureau_name !== this.BaseConfig.person_info_my.dep_name?"/" + this.BaseConfig.person_info_my.dep_name:"")+")"],
                    isHasChild:false,
                    // childList:[{childDataType:2,personInfo: {
                    //     node_label:"操作人：" + this.BaseConfig.userInfo.person_name_cookie,
                    //         person_id:this.BaseConfig.userInfo.person_id_cookie,
                    //         time_stamp:"2021-04-20 15:30:30",
                    //         remarkList: ["("+this.BaseConfig.person_info_my.bureau_name+(this.BaseConfig.person_info_my.dep_name && this.BaseConfig.person_info_my.dep_name !== ""
                    //         && this.BaseConfig.person_info_my.bureau_name !== this.BaseConfig.person_info_my.dep_name?"/" + this.BaseConfig.person_info_my.dep_name:"")+")"],
                    //     },title:"数据变更",list:[{name:"变更1",old_value:"旧值1",new_value:"新值1"},{name:"变更2",old_value:"旧值2",new_value:"新值2"},{name:"变更3",old_value:"旧值3",new_value:"新值3"},
                    //     {name:"变更4",old_value:"旧值4",new_value:"新值4"},{name:"变更5",old_value:"旧值5",new_value:"新值5"},{name:"变更6",old_value:"旧值6",new_value:"新值6"},
                    //     {name:"变更7",old_value:"旧值7",new_value:"新值7"},{name:"变更8",old_value:"旧值8",new_value:"新值8"}]}]
                }
                return record;
            },

            /*
            * 判断非boolean行的值是否为空
            * */
            isEmptyValue:function(value){
                if (value === null || value === undefined|| (TypesCheck.isString(value) && (StaticParams.trim(value) === "" || value === "[]" || value === "{}")) || (TypesCheck.isArray(value) && value.length === 0) ||
                    (TypesCheck.isObject(value) && Object.keys(value).length === 0)){
                    return true;
                }else{
                    return false;
                }
            },

            testFormData:function(){
                let b = true;
                for (let i = 0; i < this.field_list.length; i++){
                    let item = this.field_list[i];
                    if (item.tag !== "son_form"){
                        if (item.is_input_field  === 1 && item.field_must_input === 1){//是输入场并且是必填项，检测是否已填写
                            if(this.isEmptyValue(item.props.value)){
                                Modal.warning({
                                    centered:true,
                                    title:"提交提示",
                                    content:"请填写“" + item.field_name + "”后再提交"
                                })
                                b = false;
                                break;
                            }
                        }
                    }else{
                        if (item.props.tableData.some((data)=>{return data.isEdit === true})){
                            Modal.warning({
                                centered:true,
                                title:"提交提示",
                                content:"当前有子表单存在编辑状态的数据，请保存数据后再提交"
                            })
                            b = false;
                        }else{
                            for (let j = 0; j < item.props.tableData.length; j++){//检测子表单必填项
                                let rowData = item.props.tableData[j];
                                for (let k = 0; k < item.props.data.length; k++){
                                    let fieldData = rowData[item.props.data[k].id + "_json"];
                                    if (fieldData.is_input_field  === 1 && fieldData.field_must_input === 1){
                                        if(this.isEmptyValue(fieldData.props.value)){
                                            Modal.warning({
                                                centered:true,
                                                title:"提交提示",
                                                content:"请填写子表单中“" + fieldData.field_name + "”后再提交"
                                            })
                                            b = false;
                                            break;
                                        }
                                    }
                                }
                                if (b === false){
                                    break;
                                }
                            }
                        }
                    }
                }
                return b;
            },

            getSubmitDatas:function(item,obj){
                let type = parseInt(item.field_type)
                if (item.is_input_field  === 1 && type !== 27){//是输入场,但是不是关联其它表单字段的字段
                    if(type === 3){//单选
                        obj[item.column_name] = this.isEmptyValue(item.props.value)?"":"DICS#" + item.props.value + "!" +
                            (item.props.datas.filter((f)=>{return f.dic_code === item.props.value})[0]).dic_value;
                    }else if (type === 4 || type === 6){//多选，下拉选
                        if(this.isEmptyValue(item.props.value)){
                            obj[item.column_name] = "";
                        }else{
                            obj[item.column_name] = "DICS#";
                            if(TypesCheck.isArray(item.props.value)){
                                item.props.value.forEach((selected,index)=>{
                                    obj[item.column_name] += selected + "!" + (item.props.datas.filter((f)=>{return f.dic_code === selected})[0]).dic_value;
                                    if (index < item.props.value.length - 1){
                                        obj[item.column_name] += "+";
                                    }
                                });
                            }else{
                                obj[item.column_name] += item.props.value + "!" +
                                    (item.props.datas.filter((f)=>{return f.dic_code === item.props.value})[0]).dic_value;
                            }
                        }
                    }else if (type === 15 || type === 25 || type === 26){
                        if(this.isEmptyValue(item.props.value)){
                            obj[item.column_name] = "";
                        }else{
                            obj[item.column_name] = "DICS#";
                            let ary = item.props.default_value_name.split("，");
                            item.props.value.forEach((selected,index)=>{
                                obj[item.column_name] += selected + "!" + ary[index];
                                if (index < item.props.value.length - 1){
                                    obj[item.column_name] += ",";
                                }
                            })
                        }
                    }else if (type === 8 || type === 9 || type ===10){
                        if(item.props.value && this.TypesCheck.isArray(item.props.value) && item.props.value.length > 0){
                            obj[item.column_name] = JSON.stringify(item.props.value);
                        }else{
                            obj[item.column_name] = "";
                        }
                    }else if (type === 17 || type === 18){//选择部门/人员
                        if(this.isEmptyValue(item.props.value)){
                            obj[item.column_name] = "";
                        }else{
                            obj[item.column_name] = "";
                            item.props.value.forEach((selected,index)=>{
                                obj[item.column_name] += selected[type === 17?'id':'person_id'] + "!" + selected[type === 17?'name':'person_name'];
                                if (index < item.props.value.length - 1){
                                    obj[item.column_name] += "+";
                                }
                            })
                        }
                    }else{
                        obj[item.column_name] = item.props.value;
                    }
                }
            },
            submitFormData:function(){
                if (this.testFormData() === false){
                    return
                }

                this.loadingTip = "正在提交数据，请稍候...";
                this.is_loading = true;
                let dataValue = [{form_id:this.form_id,value_list:[]}]
                let obj = {};
                this.field_list.forEach((item)=>{
                    if (item.tag === "son_form"){//
                        let sonFormData = {
                            form_id:item.child_form_id,
                            value_list:[],
                        }
                        item.props.tableData.forEach((data)=>{
                            let sonObj = {};
                            item.props.data.forEach((title)=>{
                                this.getSubmitDatas(data[title.id + "_json"],sonObj);
                            });
                            sonFormData.value_list.push(sonObj)
                        })
                        dataValue.push(sonFormData);
                    }else{
                        this.getSubmitDatas(item,obj);
                    }
                });

                dataValue[0].value_list.push(obj);
                let params = {
                    org_id:this.BaseConfig.person_info_my.bureau_id,
                    system_id:10,
                    form_id:this.form_id,
                    // dept_id:this.BaseConfig.person_info_my.dep_id,
                    // person_id:this.BaseConfig.userInfo.person_id_cookie,
                    item_value_json:JSON.stringify(dataValue),
                }

                if (Object.keys(this.form_data).length > 0){
                    params.value_id = this.form_data.value_id;
                }

                if (this.businessType === 4){//管理员保存
                    params.dept_id = this.dept_id;
                    params.person_id = this.person_id;
                    params.create_person_dept_id = this.BaseConfig.person_info_my.dep_id;
                    params.create_person_id = this.BaseConfig.userInfo.person_id_cookie;

                    this.InterfaceConfig.callInterface([{
                        url:ManagementInterfaceConfig.saveTeacherInfo.url,
                        method:ManagementInterfaceConfig.saveTeacherInfo.method,
                        params:params,
                        isTestLogin:ManagementInterfaceConfig.saveTeacherInfo.isTestLogin
                    }],(data)=>{
                        let result = data[0].data;
                        if (result.code === 2000){
                            this.$toast("提交成功")
                            this.is_loading = false;
                            this.$emit("submitSuccess")
                        }
                    });
                }else{
                    params.dept_id = this.BaseConfig.person_info_my.dep_id;
                    params.person_id = this.BaseConfig.userInfo.person_id_cookie;
                    if (Object.keys(this.form_data).length > 0){
                        params.update_ts = this.form_data.update_ts;
                    }

                    this.InterfaceConfig.callInterface([{
                        url:FillingInterfaceConfig.savePersonalTeacherInfo.url,
                        method:FillingInterfaceConfig.savePersonalTeacherInfo.method,
                        params:params,
                        isTestLogin:FillingInterfaceConfig.savePersonalTeacherInfo.isTestLogin
                    }],(data)=>{
                        let result = data[0].data;
                        if (result.code === 2000){
                            if (this.dataNumType === 1){//每人多条，返回列表
                                this.$toast("提交成功")
                                this.is_loading = false;
                                this.$emit("submitSuccess")
                            }else{//每人一条，留在次页面单要重新获取一次详细接口去数据
                                let params = {
                                    org_id:this.BaseConfig.person_info_my.bureau_id,
                                    system_id:10,
                                    form_id:this.form_id,
                                    teacher_id:this.BaseConfig.userInfo.person_id_cookie
                                }

                                this.InterfaceConfig.callInterface([{
                                    url:FillingInterfaceConfig.getPersonalInfoBySingleFormId.url,
                                    method:FillingInterfaceConfig.getPersonalInfoBySingleFormId.method,
                                    params:params,
                                    isTestLogin:FillingInterfaceConfig.getPersonalInfoBySingleFormId.isTestLogin
                                }],(data)=>{
                                    let result = data[0].data;
                                    if (result.code === 2000){
                                        this.$toast("上报数据提交成功")
                                        this.is_loading = false;
                                        this.type = 3;
                                        this.dataResource = _.cloneDeep(result.data);
                                        this.operate_list = this.organizeOperateList(result.data.history_list)
                                        this.setFieldList(result.data.field_list,result.data.form_data_list?result.data.form_data_list[0]:null);
                                    }
                                });
                            }
                        }
                    });
                }
            },
            setFieldList:function(filedList,data){
                this.sonFormCount = 0;
                this.hasGetFormDataCount = 0;
                this.field_list = [];
                this.mainFormErgodicComplete = false;
                filedList.forEach((field)=>{
                    let json = JSON.parse(field.field_json);
                    json.form_id = field.form_id;
                    json.child_form_id = field.child_form_id;
                    let field_type = parseInt(json.field_type);
                    if (json.field_type === 1 && json.field_input_type === 2){//系统自动填充
                        if (json.filed_auto_input_info === "person_num"){
                            json.props.value = this.workers_no?this.workers_no:"";
                        }else if (json.filed_auto_input_info === "person_name"){
                            json.props.value = this.person_name?this.person_name:"";
                        }else{
                            json.props.value = this.org_name + (this.dept_name && this.dept_name !== "" && this.dept_name !== this.org_name?"(" + this.dept_name + ")":"")
                        }
                    }else if(json.is_input_field === 1){//输入场
                        this.organizeData(json,data)
                    }else if(field_type > 200){//子表单、表格 、栅格
                        json.field_type = parseInt(field_type.toString().substr(0,2))
                        if(parseInt(field_type.toString().substr(2,1)) === 1){//包含其它字段
                            if (data){//已经填写过数据的获取子表单填写的数据
                                this.sonFormCount += 1;
                                this.getChildFormData(json,data.data_id,data.version_no)
                            }else{//未填写过数据的 自己构造数据
                                if (json.tag === "son_form"){//子表单
                                    json.props.tableData = [];
                                    if (json.props.defaultLineCount > 0){//默认显示行数大于1
                                        for(let i = 0; i < json.props.defaultLineCount; i++){
                                            let data = {value_id:GUID_UUID.guid()}
                                            json.props.data.forEach((item)=>{
                                                // if (item.column_name && item.column_name !== ""){
                                                //     data[item.column_name + "_json"] = _.cloneDeep(item)
                                                // }else{
                                                data[item.id + "_json"] = _.cloneDeep(item);
                                                data[item.id + "_json_copy"] = _.cloneDeep(item)
                                                // }
                                            })
                                            json.props.tableData.push(data)
                                        }
                                    }
                                }
                            }
                        }
                    }
                    this.field_list.push(json);
                })

                this.mainFormErgodicComplete = true;//主表单遍历完成
                if (this.sonFormCount === this.hasGetFormDataCount){//子表单数据已经全部整理完毕
                    this.is_loading = false;
                }
            },
            editForm:function () {
                this.type = 2;
                this.operate_list.push(this.addOperateRecord())
                let copy = _.cloneDeep(this.dataResource)
                this.setFieldList(copy.field_list,copy.form_data_list?copy.form_data_list[0]:null);
            },
            cancelEdit:function () {
                Modal.confirm({
                    title:"取消确认",
                    content:"取消编辑后，数据会还原到初始(上一次保存)状态，确认取消吗？",
                    okText:"确认",
                    cancelText:"取消",
                    centered:true,
                    onOk:()=>{
                        this.type = 3;
                        let cory = _.cloneDeep(this.dataResource);
                        this.setFieldList(cory.field_list,cory.form_data_list?cory.form_data_list[0]:null);
                        this.operate_list.pop();
                    }
                });
            },
             getChildFormData:function(json,data_id,version_no){
                let params = {
                    org_id:this.BaseConfig.person_info_my.bureau_id,
                    system_id:10,
                    child_form_id:json.child_form_id,
                    data_id:data_id,
                    version_no:version_no
                }

                this.InterfaceConfig.callInterface([{
                    url:FillingInterfaceConfig.getTeacherInfoByChildFormId.url,
                    method:FillingInterfaceConfig.getTeacherInfoByChildFormId.method,
                    params:params,
                    isTestLogin:FillingInterfaceConfig.getTeacherInfoByChildFormId.isTestLogin
                }],(data)=>{
                    let result = data[0].data;
                    if (result.code === 2000){
                        this.hasGetFormDataCount += 1;
                        if (json.tag === "son_form"){
                            let tableData = []
                            result.data.form_data_list.forEach((formData)=>{
                                let data = {
                                    value_id:formData.value_id
                                }
                                json.props.data.forEach((field)=>{
                                    if (field.is_input_field === 1){
                                        this.organizeData(field,formData);
                                    }

                                    data[field.id + "_json"] = _.cloneDeep(field)
                                    data[field.id + "_json_copy"] = _.cloneDeep(field)

                                    // if (field.column_name && field.column_name !== ""){
                                    //     data[field.column_name + "_json"] = _.cloneDeep(field)
                                    // }else{
                                    //     data[field.id + "_json"] = _.cloneDeep(field)
                                    // }
                                })

                                tableData.push(data);
                            });
                            this.$set(json.props,"tableData",tableData);
                        }
                        if (this.mainFormErgodicComplete === true && this.sonFormCount === this.hasGetFormDataCount){//子表单数据已经全部整理完毕
                            this.is_loading = false;
                        }
                    }
                })
            },
            clearJsonValue:function(json){
                if ([1,2,3,11].some((num)=>{return num === json.field_type})){
                    json.props.value = ""
                }else if ([4,8,9,10,15,17,18,25,26].some((num)=>{return num === json.field_type})){
                    json.props.value = []
                }else if ([5,6].some((num)=>{return num === json.field_type})){
                    json.props.value = undefined
                }else if ([12].some((num)=>{return num === json.field_type})){
                    json.props.value = null
                }else if ([13,14].some((num)=>{return num === json.field_type})){
                    json.props.value = null
                }
            },
            organizeData:function(json,data){
                let field_type = json.field_type;
                if (data && data.value_id){//上报过表单数据（审核功能一定是上报过表单数据的）
                    let field_value = data[json["column_name"]];
                    if(field_value === "" || field_value === null){//字段上没有数据的时候
                        this.clearJsonValue(json)
                    }else{//字段上有数据的特殊处理
                        if(field_type <= 14 && ![3,4,6].some((type)=>{return type === field_type})){//可以直接获取值进行还原的字段
                            if (field_type === 13 || field_type === 14){
                                json.props.value = Number(field_value);
                            }else{
                                json.props.value = field_value;
                            }

                        }else if([3,4,6,15,25,26,27].some((type)=>{return type === field_type})){//包含数据字典的字段特殊处理
                            field_value = field_value.substring(5,field_value.length)//去掉"DICS#"
                            if (field_type === 3 || (field_type === 6 && json.props.mode === "default") || field_type === 27){//单选/下拉单选/关联字段
                                let ary = field_value.split("!")
                                if (field_type === 27){
                                    json.props.value = ary[1];
                                }else{
                                    json.props.value = ary[0];
                                    json.dics_value = ary[1];
                                }
                            }else if (field_type === 4 || (field_type === 6 && json.props.mode === "multiple")){//多选/下拉多选
                                let valueAry = field_value.split("+")
                                json.props.value = [];
                                json.dics_value = "";
                                valueAry.forEach((item,index)=>{
                                    let ary = item.split("!");
                                    json.props.value.push(ary[0]);
                                    json.dics_value += ary[1];
                                    if (index < valueAry.length - 1){
                                        json.dics_value += "，"
                                    }
                                })
                            }else{//地域、学段学科、专业
                                let valueAry = field_value.split(",")
                                json.props.value = [];
                                json.dics_value = "";
                                json.props.default_value_name = "";
                                valueAry.forEach((item,index)=>{
                                    let ary = item.split("!");
                                    json.props.value.push(ary[0]);
                                    json.dics_value += ary[1];
                                    json.props.default_value_name += ary[1];
                                    if (index < valueAry.length - 1){
                                        json.dics_value += "，"
                                        json.props.default_value_name +="，"
                                    }
                                })
                            }
                        }else if(field_type >=16 && field_type <= 18){//选择机构、部门、人员组件
                            let valueAry = field_value.split("+")
                            json.props.value = [];
                            json.dics_value = "";
                            valueAry.forEach((item,index)=>{
                                let ary = item.split("!");
                                if (field_type === 17){
                                    json.props.value.push({id:ary[0],name:ary[1]});
                                }else if(field_type === 18){
                                    json.props.value.push({person_id:ary[0],person_name:ary[1]});
                                }
                                json.dics_value += ary[1];
                                if (index < valueAry.length - 1){
                                    json.dics_value +="，"
                                }
                            })
                        }else if (field_type === 9 && field_type === 10){
                            json.props.value = field_value && field_value !== "" ? JSON.parse(field_value):"";
                        }
                    }
                }else{//未上报过表单数据
                    if(this.type === 3){//查看的时候把数据默认值都清理了
                        this.clearJsonValue(json)
                    }
                }
            }
        },

        mounted:function(){
            this.operate_list = this.organizeOperateList(this.dataInfo.history_list)
            this.setFieldList(this.dataInfo.field_list,this.dataInfo.form_data_list?this.dataInfo.form_data_list[0]:null);
        },
        watch:{
            dataInfo:function (newData) {
                this.form_config = newData.form_config_json && newData.form_config_json !== ""?JSON.parse(newData.form_config_json):null;
                this.form_data = newData.form_data_list && newData.form_data_list[0]?newData.form_data_list[0]:{};
                this.operate_list = this.organizeOperateList(newData.history_list);
                this.dataResource = _.cloneDeep(newData);
                this.setFieldList(newData.field_list,newData.form_data_list?newData.form_data_list[0]:null);
            },
            personInfo:function (newData) {
                this.person_id = newData.person_id;
                this.person_name = newData.person_name;
                this.workers_no = newData.workers_no;
                this.dept_name = newData.dept_name;
                this.org_name = newData.org_name;
                this.dept_id = newData.dept_id;
            }
        }
    }
</script>

<style scoped lang="scss">
    @import "style/editAndViewForm";
</style>
