<template>
<Card :bordered="false" :padding="0" dis-hover>
     <p  v-if="showTitle" @click="toggle" style="cursor:pointer;" >
        <Row type="flex" justify="start" align="middle" style="margin-top:14px;margin-bottom:14px">
             <Icon custom='iconfont icon-custom-pack' color="#f3b352" v-show="showGrid" size="14" />
            <Icon  v-show="!showGrid" color="#f3b352" custom='iconfont icon-custom-spread' size="14"/> 
            <span style="margin-left:10px;font-size: 14px;font-weight:bold" v-text="myConfig.title" />
        </Row>
     </p>
     <div style="padding-left:24px">
        <Divider  v-if="showTitle" style="margin-top:0px;margin-bottom:10px;"/>
        <div v-show="showGrid">
            <SimpleViewGrid ref="grid" :config="myConfig"></SimpleViewGrid>
        </div>
    </div>
</Card>
</template>
<script>
export default {
    name: 'moperlog',
    data () {
        return {
            myConfig:{
                dynamicHeight:false,
                resources:'operbilllog',
                height:360,
                title:'操作日志',
                method:'mss.sysconf.operationlog.search',
                generalOrientation:true,
                showPage:false,
                searchParam:{
                    order_field: 'operdate',
                    order_direction: 'desc'
                },
                response:'operationlog',
                items:[{
                    label:'操作类型',
                    name:'upstatus',
                    type:'ComboBox',
                    data:[{
                        code:'U',name:'修改'
                    }, {
                        code:'I',name:'新增'
                    }, {
                        code:'D',name:'删除'
                    }],
                    codeWithName:false,
                    width:100
                },{
                    label:'操作日期',
                    name:'operdate',
                    type:'DateBox',
                    vtype:'datetime',
                    width:150
                }, {
                    label:'操作人',
                    name:'username',
                    type:'TextBox',
                    width:150
                }, {
                    label:'操作主体',
                    name:'upmain',
                    type:'TextBox',
                    tooltip:true
                },{
                    label:'操作字段',
                    name:'upfield',
                    type:'TextBox',
                    tooltip:true
                },{
                    label:'操作内容',
                    name:'memo',
                    type:'TextBox',
                    tooltip:true
                }]
            },
            showGrid:true,
            idkey:'',
            masterslavekey:'',
            form:{},
            showTitle:false
        };
    },
    created() {
        this.myConfig = Object.assign({}, this.myConfig, this.config);
        this.myConfig.resources = this.OmdUrl[this.myConfig.resources];;
        this.showGrid = true;
    },
    props:{
        config: {
            type:Object,
            default:null
        }
    },
    methods: {
        setData(masterslavekey,data) {
            if (data.ph_key) {
               this.idkey = data.ph_key
            } else {
               this.idkey = ''; 
            }
            if (masterslavekey) {
               this.masterslavekey = masterslavekey
            } else {
               this.masterslavekey = ''; 
            }
            this.form = this.$Method.copy(data);
            this.initLogData();
        },
        clearData() {
            this.idkey = '';
            this.masterslavekey = '';
            this.form = {};
            this.$refs.grid.clearData();
        },
        toggle () {
            this.showGrid = !this.showGrid;
        },
        setShowGrid(value) {
            this.showGrid = value;
        },
        initLogData () {
            let obj = {};
            if (!this.idkey && !this.masterslavekey) {
                return;
            }
            if (this.masterslavekey) {
                obj['$or'] = [];
                obj['$or'].push({'masterslavekey':this.masterslavekey});
                obj['$or'].push({'idkey':this.idkey});
            } else {
                obj.idkey = this.idkey;
            }
            obj.page_size = '999999';
            Object.assign(obj,this.myConfig.searchParam);
            let self = this;
            self.synchroPost(self.myConfig.resources, self.myConfig.method, obj, function (data) {
                if (data[self.myConfig.response] && data[self.myConfig.response].length > 0) {
                   self.setLogData(data[self.myConfig.response]);
                } else {
                    self.setLogData([]);
                }
            });
        },
        setLogData(data) {
            let tmpData = [];
            data.forEach(el => {
                let bean = this.$Method.copy(el);
                if (bean.upstatus === 'I' || bean.upstatus === 'D') {
                    let tmp = this.config.logName.find(el2 => el2.code === bean.uptabname);
                    if (tmp) {
                        bean.upmain = tmp.name;
                        let memo = '';
                        if (tmp.isdetail) {
                            let detaildata = this.form[el.uptabname];
                            let rowInfo = '';
                            if (detaildata.length > 0) {
                                if (tmp.isdetail === '1') {
                                    let index = detaildata.findIndex(el3 => el3.ph_key === el.idkey);
                                    if (index > -1) {
                                        let rowNum = index + 1;
                                        rowInfo =  '第' + rowNum +'行';
                                    }
                                } else {
                                    let flag = false;
                                    let headcode = '';
                                    let index = detaildata.findIndex(el3 => el3.ph_key === el.idkey);
                                    if (index > -1) {
                                        headcode =detaildata[index][tmp.uniqueKeys];
                                    }
                                    let row = 1;
                                    detaildata.forEach(el => {
                                        if (el[tmp.uniqueKeys] === headcode) {
                                            if (el.ph_key === el.idkey) {
                                                flag = true;
                                                return true;
                                            } else {
                                                row++;
                                            }
                                        }
                                    });
                                    if (flag) {
                                       rowInfo = tmp.headtitle + '['+headcode+']第' + row +'行';
                                    }
                                }
                            }
                            if (rowInfo) {
                               memo =  memo + rowInfo;
                            } else {
                                memo = memo + '明细物理主健[' +bean.idkey+']';
                            }  
                        } else{
                            memo = memo + '主表';
                            if (bean.masterslavekey) {
                                memo = memo + '业务主键[' +bean.masterslavekey+']';
                            } else {
                                memo = memo + '物理主健[' +bean.idkey+']';
                            }
                        }
                        bean.memo = memo;
                    }
                    tmpData.push(bean);
                } else {
                    let up = JSON.parse(el.changevalue);
                    up.forEach(el2 => {
                        let bean2 = this.$Method.copy(bean);
                        let namekey = el.uptabname + '.' + el2.name;
                        let tmp = this.config.logName.find(el3 => el3.code === namekey);
                        if (tmp) {
                            bean2.upmain = tmp.title;
                            bean2.upfield = tmp.name;
                            let oldvalue = '';
                            if (el2.oldvalue){
                                oldvalue = el2.oldvalue
                            }
                            let newvalue = el2.newvalue;
                            if (tmp.type === 'DateBox') {
                                if (oldvalue) {
                                    oldvalue = this.$Method.formatDataBox(oldvalue,tmp);
                                } 
                                if (newvalue) {
                                    newvalue = this.$Method.formatDataBox(newvalue,tmp);
                                }
                            }
                            bean2.memo = '原值[' + oldvalue + '],新值[' + newvalue + ']';
                            if (tmp.isdetail) {
                                if (!tmp.headcode) {
                                    let detaildata = this.form[el.uptabname];
                                    if (detaildata && detaildata.length > 0) {
                                        if (tmp.isdetail === '1') {
                                            let index = detaildata.findIndex(el3 => el3.ph_key === el.idkey);
                                            if (index > -1) {
                                                let rowNum = index + 1;
                                                bean2.upfield =  '(第' + rowNum +'行)' + bean2.upfield
                                            }
                                        } else if(tmp.isdetail === '2') {
                                            let flag = false;
                                            let headcode = '';
                                            let index = detaildata.findIndex(el3 => el3.ph_key === el.idkey);
                                            if (index > -1) {
                                                headcode =detaildata[index][tmp.uniqueKeys];
                                            }
                                            let row = 1;
                                            detaildata.forEach(el => {
                                                if (el[tmp.uniqueKeys] === headcode) {
                                                    if (el.ph_key === el.idkey) {
                                                    flag = true;
                                                    return true;
                                                    } else {
                                                    row++;
                                                    }
                                                }
                                            });
                                            if (flag) {
                                                bean2.upfield = '(' + tmp.headtitle + '['+headcode+']第' + row +'行)' + bean2.upfield;
                                             }
                                        }
                                       
                                    }
                                }
                            }
                            tmpData.push(bean2);
                        }
                    });
                }
                
            });
            this.$refs.grid.setData(tmpData);
        }
    }
};
</script>
