<template>
    <el-table-column  v-bind="$attrs" v-on="$listeners" :align="align" v-if="isShowCol">
        <span slot="header" v-if="showTips || $attrs.headlink || $attrs.goldFlag || $attrs.goldFlagOnly || true" style="position:relative" :title="showTips">
            <el-link v-if="$attrs.headlink" type="primary" @click="toHeadLink" :underline="false" class="tips-blue">
                <span style="text-decoration:underline">{{showLabel}}</span>
            </el-link>
            <span v-else :class="showTips?'tips-blue':''" @dblclick="copyTips">{{showLabel}}</span>
            <i v-if="$attrs.goldFlag && $store.state.envCfg.goldBet!=1" class="fa fa-ge" :title="`该字段开启金币倍率转换,显示值为: 原值*${$store.state.envCfg.goldBet}`"
                style="font-size:6px;color:#66b1ff;position:absolute;left:-13px;top:-3px;"></i>
            <i v-if="$attrs.goldFlagOnly && $store.state.envCfg.goldBet!=1" class="fa fa-ge" :title="`该字段开启金币倍率转换(仅针对金币特殊处理),显示值为: 原值*${$store.state.envCfg.goldBet}`"
                style="font-size:6px;color:#e9a94a;position:absolute;left:-13px;top:-3px;"></i>
        </span>
        <exColumn v-for="(col,idx) in columns" :key="idx" v-bind="col"></exColumn>
        <template v-slot="s" v-if="columns.length==0">
            <slot v-bind="s"></slot>
            <template v-if="!isSlot">
                <template v-if="$attrs.edittype && (s.row.editing || $attrs.quickMode) && (!$attrs.canModify || $attrs.canModify(s))">
                    <template v-if="$attrs.edittype=='input'">
                        <el-input v-model.trim="s.row[$attrs.prop]" size="mini"></el-input>
                    </template>
                    <template v-if="$attrs.edittype=='textarea'">
                        <el-input v-model.trim="s.row[$attrs.prop]" type="textarea" size="mini" rows="3"></el-input>
                    </template>
                    <template v-else-if="$attrs.edittype=='number'">
                        <el-input v-model.number="s.row[$attrs.prop]" type="number" size="mini" @change="qucikModify(s)"></el-input>
                    </template>
                    <template v-else-if="$attrs.edittype=='moneyFen'">
                        <exFenInput v-model.number="s.row[$attrs.prop]" type="number" :pointLen="$attrs.pointLen" size="mini" @change="qucikModify(s)"></exFenInput>
                    </template>
                    <template v-else-if="$attrs.edittype=='select' && ($attrs.dictCode || $attrs.dictList || $attrs.handleDictCode)">
                        <exSelect v-model="s.row[$attrs.prop]" :dictCode="$attrs.dictCode" :dictList="$attrs.dictList" 
                            :handleDictCode="$attrs.handleDictCode" :dataInfo="s.row" @change="qucikModify(s)" :bool="bool" :showValue="$attrs.showValue" :allow-create="$attrs.allowCreate" :number="number" size="mini"></exSelect>
                    </template>
                    <template v-else-if="$attrs.edittype=='selects' && ($attrs.dictCode || $attrs.dictList)">
                        <exSelect v-model="s.row[$attrs.prop]" :dictCode="$attrs.dictCode" :dictList="$attrs.dictList" @change="qucikModify(s)" :multiple="true" collapse-tags  :bool="bool" :showValue="$attrs.showValue" :number="number" size="mini"></exSelect>
                    </template>
                    <template v-else-if="$attrs.edittype=='radio' && ($attrs.dictCode || $attrs.dictList)">
                        <exRadioGroup v-model="s.row[$attrs.prop]" :dictCode="$attrs.dictCode" :dictList="$attrs.dictList" @change="qucikModify(s)" :bool="bool" :number="number" size="mini"></exRadioGroup>
                    </template>
                    <template v-else-if="$attrs.edittype=='checkbox' && ($attrs.dictCode || $attrs.dictList)">
                        <exCheckBoxGroup v-model="s.row[$attrs.prop]" :dictCode="$attrs.dictCode" :dictList="$attrs.dictList" @change="qucikModify(s)" :bool="bool" :number="number" size="mini"></exCheckBoxGroup>
                    </template>
                    <template v-else-if="$attrs.edittype=='datetime'">
                        <exDateTime v-model="s.row[$attrs.prop]" size="mini" ></exDateTime>
                    </template>
                    <template v-else-if="$attrs.edittype=='date'">
                        <exDate v-model="s.row[$attrs.prop]" size="mini" :type="$attrs.datetype?$attrs.datetype:'date'"></exDate>
                    </template>
                    <template v-else-if="$attrs.edittype=='dateRange'">
                        <exDateRange v-model="s.row[$attrs.prop]" size="mini"  :type="$attrs.datetype?$attrs.datetype:'daterange'"></exDateRange>
                    </template>
                    <template v-else-if="$attrs.edittype=='cascader'">
                        <exCascader v-model="s.row[$attrs.prop]" size="small" :dictCode="$attrs.dictCode" :options="$attrs.options" :props="$attrs.props" :disabled="$attrs.disabled" :show-all-levels="$attrs['show-all-levels']"
                            :collapse-tags="$attrs['collapse-tags'] || false" :filterable="$attrs['filterable']" :isLastLv="$attrs['isLastLv']" :showValue="$attrs['showValue']" :isStrValue="$attrs['isStrValue']"
                        ></exCascader>
                    </template>
                    <template v-else-if="$attrs.edittype=='switch'">
                        <exSwitch v-model="s.row[$attrs.prop]"  @change="qucikModify(s)" :type="number?'number':''" size="mini"></exSwitch>
                    </template>
                    <template v-if="$attrs.edittype=='memberId'">
                        <playerSel v-model="s.row[$attrs.prop]" size="mini" type="memberId" elstyle="width:100%" :canSel="true"></playerSel>
                    </template>

                    
                </template>
                <template v-else>
                    <template v-if="$attrs.viewUser">
                        <i class="el-icon-view" :style="getViewClass(s)" @click.left="changeViewStatus(s)" @contextmenu.prevent="setPubUserInfo(s)"></i>
                    </template>
                    <template v-if="$attrs.vipFlag && s.row[$attrs.vipFlag===true?'vipLevel':$attrs.vipFlag] ">
                        <i class="vipFlag">V{{s.row[$attrs.vipFlag===true?"vipLevel":$attrs.vipFlag]}}</i>
                    </template>
                    <template v-else-if="$attrs.label=='玩家昵称' && s.row['vipLevel'] ">
                        <i class="vipFlag">V{{s.row['vipLevel']}}</i>
                    </template>
                    <template v-if="$attrs.edittype=='cascader' && !$attrs.showByText">
                        <exCascader v-model="s.row[$attrs.prop]" size="small" :dictCode="$attrs.dictCode" :options="$attrs.options" :props="$attrs.props"
                         :disabled="true" :show-all-levels="$attrs['show-all-levels']"
                          :collapse-tags="$attrs['collapse-tags'] || false" :filterable="$attrs['filterable']" :isLastLv="$attrs['isLastLv']"  :isStrValue="$attrs['isStrValue']"></exCascader>
                    </template>
                    <template v-else-if="$attrs.edittype=='selects' && ($attrs.dictCode || $attrs.dictList)">
                        <exSelect v-model="s.row[$attrs.prop]" :dictCode="$attrs.dictCode" :dictList="$attrs.dictList" @change="qucikModify(s)" :disabled="true" :multiple="true"  :bool="bool" :showValue="$attrs.showValue" :number="number" size="mini"></exSelect>
                    </template>
                    <el-link v-else-if="$attrs.link" type="primary" :title="getLinkName(s)" @click="toLink(s)" :underline="false">
                        <span v-html="getContent(s)" style="text-decoration:underline"></span>
                    </el-link>
                    <el-link v-else-if="$attrs.click" type="primary"  @click="$attrs.click(s)" :underline="false">
                        <span v-html="getContent(s)" style="text-decoration:underline"></span>
                    </el-link>
                    <span v-else-if="$attrs.showText" :title="getTitle(s)" @dblclick="ondblclick(s)">{{s.row[$attrs.prop]}}</span>
                    <span v-else v-html="getContent(s)" :title="getTitle(s)" @dblclick="ondblclick(s)"></span>
                    
                </template>
            </template>
        </template>
    </el-table-column>
</template>


<script>



export default {
    props:["bool","number"],
    data() {
        return {
            dateRange:[],
        }
    },
    computed:{
        showLabel(){
            if(this.$store && this.$store.state.lang){
                return this.$attrs["label"+this.$store.state.lang] || this.$attrs.label;
            }
            return this.$attrs.label;
        },
        columns(){
            return this.$attrs.columns || []
        },
        isSlot(){
            return this.$slots.default || this.$scopedSlots.default?true:false
        },
        align(){
            if(this.$attrs.align){
                return this.$attrs.align;
            }else if(this.$attrs.formatter){
                return "right";
            }else {
                return ""
            }
            
        },
        showTips(){
            var envCfg = this.$store.state.envCfg || {};
            var envFlag = envCfg.envFlag;
            var tips = "";
            if(typeof(this.$attrs.tips) == "object"){
                tips = this.$attrs.tips[envFlag] || this.$attrs.tips["def"] || ""
            }else{
                tips = this.$attrs.tips;
            }
            if(!this.getCfgItem || !tips) return tips;
            tips = tips.replace(/cfgItem\(.*?\)/g,(item,idx,cont)=>{
                let key = item.substring(8,item.length-1);
                let val =  this.getCfgItem(key)
                return val;
            });
            tips = this.$dict.tranByPubMap(tips)
            return tips;
        },
        labelCfg(){
            var cfg = {};
            if(this.tableLabelCfg){
                cfg = this.tableLabelCfg;
            }else if(this.globalLyExFnCfg){
                if(this.$store && this.$store.state.lang){
                    cfg = this.globalLyExFnCfg["tableLabelCfg"+this.$store.state.lang] || this.globalLyExFnCfg.tableLabelCfg || {};
                }else{
                    cfg = this.globalLyExFnCfg.tableLabelCfg || {};
                }
            }
            return cfg;
        },
        fnCfg(){
            var cfg = {};
            if(this.globalLyExFnCfg){
                cfg = this.globalLyExFnCfg.fnCfg || {};
            }
            return cfg;
        },
        isShowCol(){
            if(this.$attrs.isShow===false){
                // console.log(this.$attrs.prop+" "+ this.$attrs.label +"  hide")
                return false;
            }
            if(this.$attrs.isShow){
                var funval = this.$attrs.isShow(this.$attrs);
                if(!funval){
                    // console.log(this.$attrs.prop+" "+ this.$attrs.label +"  hide")
                    return false;
                }
            }
            if(!this.isEnvShow(this.$attrs)){
                // console.log(this.$attrs.prop+" "+ this.$attrs.label +"  hide")
                return false;
            }
            // console.log(this.$attrs.prop+" "+ this.$attrs.label +"  show")

            
            return true;
        }
    },
    watch:{
    },

    mounted() {
    },
    inject: ['addTab','qryUserInfo'],
    methods: {
        copyTips(){
            this.copy(this.showTips);
        },
        isEnvShow($attr){
            if(!$attr.envFlag) return true;
            var envCfg = this.$store.state.envCfg || {};
            if($attr.envFlag[envCfg.envFlag]) return true;
            return false;
        },
        setPubUserInfo(s){
            var memberId=s.row.memberId || s.row.gameId;
            this.qryUserInfo("",memberId,null);
        },
        getViewClass(s){
            var memberId=s.row.memberId || s.row.gameId;
            if(memberId && this.$store.state && this.$store.state.viewUserMap){
                if(this.$store.state.viewUserMap[memberId]){
                    return "font-weight:bold;color:blue;"
                }
            }
            return ""
        },
        changeViewStatus(s){
            var memberId=s.row.memberId || s.row.gameId;
            this.$store.dispatch('changeViewUserInfo',{memberId:memberId})
        },
        getTitle(s){
            var title = "";
            if(this.$attrs.getTitle){
                title=this.$attrs.getTitle.call(this.$parent,s);
            }
            if(this.$attrs["show-overflow-tooltip"]){
                title += this.labelCfg.dblClickToCopy || "(双击复制)"
            }
            title += this.getTipsInfo(s);
            return title
        },
        getTipsInfo(s){
            var tips ="";
            if(this.$attrs.goldFlag && this.$store.state.envCfg.goldBet!=1){
                tips += `\n金币原值: ${this.reTranGold(s.row[this.$attrs.prop])}`;
            }
            var tipsProp=this.$attrs.prop+"__Tips";
            if(s.row[tipsProp]){
                tips += `\n字段合计明细:\n${s.row[tipsProp]}`;
            }
            return tips;
        },
        ondblclick(s){
            if(this.$attrs.ondblclick){
                var cont = this.getContent(s);
                this.$attrs.ondblclick.call(this.$parent,cont,s);
                return;
            }
            var cont = "";
            if(this.$attrs["show-overflow-tooltip"]){
                cont += this.getContent(s);
            }
            var tips = this.getTipsInfo(s);;
            cont += tips || "";
            if(cont){
                this.copy(cont);
                this.$message.info(this.labelCfg.copySuccToClip ||  "已复制到剪切板");
            }
        },
        qucikModify(scope){
            if(this.$attrs.onChange && scope.row.editing){
                this.$attrs.onChange.call(this.$parent,scope)
            }
            if(!this.$attrs.quickMode || this.$attrs.noQuickCommit) return;
            if(scope.row.editing) return;
            var args = {
                updateUrl: this.$attrs.updateUrl,
                saveFun : this.$attrs.saveRow
            }
            this.$emit("quickModify",scope,args);
        },
        getCascaderText(list,val){
            for(var i=0;i<list.length;i++){
                var item = list[i];
                if(item.children){
                    var name = this.getCascaderText(item.children,val);
                    if(name){
                        return name;
                    }
                }else{
                    if(item.value==val){
                        name = item.name;
                        if(this.$attrs.showValue){
                            name += `(${item.value})`;
                        }
                        return name;
                    }
                }
            }
        },
        getCascaderTextLevel(list,vallist,nameList,level){
            if(vallist.length==0) return;
            var val = vallist[level]
            for(var i=0;i<list.length;i++){
                var item = list[i];
                if(item.value==val){
                    var name = item.name;
                    if(this.$attrs.showValue){
                        name += `(${item.value})`;
                    }
                    nameList.push(name)
                    if(item.children){
                        this.getCascaderTextLevel(item.children,vallist,nameList,level+1);
                    }else{
                        nameList.concat(vallist)
                    }
                }
            }
        },
        getContent(s) {
            var cont = s.row[this.$attrs.prop];
            if(this.bool){
                cont = cont ? "1":"0";
            }
            var showText = "";
            var handleRet = null;
            var lang = "";
            if(this.$store && this.$store.state.lang) {
                lang = this.$store.state.lang;
            }
            if(this.$attrs.handleDictCode){
                handleRet = this.$attrs.handleDictCode(s.row);
            }
            if(this.$attrs.edittype=='cascader' && cont!=undefined){
                var list = [];
                if(this.$attrs.dictCode){
                    list = this.$dict.getList(this.$attrs.dictCode,null,null,lang);
                }else if(this.$attrs.options){
                    list = this.$attrs.options;
                }
                var valList = [];
                if(typeof(cont)=="string" && cont.startsWith("[")){
                    try{
                        valList = JSON.parse(cont);
                    }catch(ex){
                        valList = cont;
                    }
                }
                if(typeof(cont)=='object'){
                    valList = cont
                }else{
                    valList = [cont]
                }
                var namelist = [];
                if(valList.map && valList.length>0){
                    if(this.$attrs.isLastLv){
                        valList.map(item=>{
                            var name =this.getCascaderText(list,item);
                            if(name) namelist.push(name)
                            else namelist.push(item)
                        })
                    }else{
                        this.getCascaderTextLevel(list,valList,namelist,0);
                    }
                }
                if(namelist.length>0){
                    if(this.$attrs.isLastLv){
                        showText = namelist.join(",")
                        if(namelist.length>1){
                            showText = `(${namelist.length}) ${showText}`;
                        }
                    }else{
                        showText = namelist.join(" / ")
                    }
                }else{
                    showText = cont;
                }
            }else if((this.$attrs.dictCode || (handleRet && typeof(handleRet)==="string")) && cont!=undefined){
                var code = handleRet || this.$attrs.dictCode;
                var dictitem = this.$dict.getDictItem(code,cont);
                showText= dictitem["name"+lang] || dictitem.name;
                if(!showText){
                    showText = cont;
                }else if(this.$attrs.showValue && showText){
                    showText+=`(${cont})`;
                }
                if(dictitem.color && showText){
                    showText = `<span style="color:${dictitem.color}">${showText}</span>`
                }
                if(dictitem.remark && showText){
                    showText = `<span class="tips-blue" title="${dictitem.remark}">${showText}</span>`;
                }
            }else if (this.$attrs.formatter) {
                showText= this.$attrs.formatter.call(this.$parent,s.row,s.column,cont,s.$index)
                if (this.$attrs.rateBy) {
                    showText+= " ("+this.rateFormat(cont,s.row[this.$attrs.rateBy])+")";
                }else if (this.$attrs.avgBy) {
                    showText+= " ("+this.avgFormat(cont,s.row[this.$attrs.avgBy],2)+")";
                }else if (this.$attrs.wrapBy) {
                    var wrapText = s.row[this.$attrs.wrapBy];
                    if(this.$attrs.wrapFormat){
                        wrapText= this.$attrs.wrapFormat.call(this.$parent,s.row,s.column,wrapText,s.$index)
                    }
                    showText+=` (${wrapText + (this.$attrs.wrapByEndStr||"")})`;
                }
            }else if (this.$attrs.dictList || (handleRet && typeof(handleRet)==="object")) {
                var list = this.$attrs.dictList;
                if(handleRet && typeof(handleRet)==="object" && handleRet.map){
                    list = handleRet;
                }
                list.map(item=>{
                    var name = item["name"+lang] || item.name ;
                    if(item.value == cont) showText = this.$dict.tranByPubMap(name);
                    if(item.color && showText){
                        showText = `<span style="color:${item.color}">${showText}</span>`
                    }
                })
                if(!showText) showText = cont;
                else if(this.$attrs.showValue){
                    showText+=`(${cont})`;
                }
            }else if (this.$attrs.wrapRateBy) {
                showText = cont;
                showText+=` (${this.rateFormat(cont,s.row[this.$attrs.wrapRateBy])})`;
            }else if (this.$attrs.rateBy) {
                showText= this.rateFormat(cont,s.row[this.$attrs.rateBy]);
            }else if (this.$attrs.avgBy) {
                showText= this.avgFormat(cont,s.row[this.$attrs.avgBy],2);
            }else if (this.$attrs.wrapBy) {
                showText = cont;
                showText+=` (${s.row[this.$attrs.wrapBy] + (this.$attrs.wrapByEndStr||"")})`;
            }else {
                showText= cont;
            }

            if(showText){
                showText += this.$attrs.endStr || "";
                if (this.$attrs.getColor){
                    var color = this.$attrs.getColor(s);
                    if(color){
                        showText = `<span class="${color}">${showText}</span>`;
                    }
                }
                
                if(this.$attrs.getContentTitle){
                    var title=this.$attrs.getContentTitle.call(this.$parent,s.row);
                    if(title){
                        showText = `<span title="${title}">${showText}</span>`;
                    }
                }
            }
            else if(this.$attrs.def) showText = this.$attrs.def;
            return showText;
        },
        getLinkInfo(row){
            var linkinfo = this.$attrs.link;
            var link = {};
            if(typeof(linkinfo) =="function"){
                link = linkinfo(row);
            }else if(typeof(linkinfo) =="object"){
                link = linkinfo;
            }
            link = link || {};

            if(this.fnCfg.columnLinkHandle){
                this.fnCfg.columnLinkHandle.call(this,link)
            }
            return link;
        },
        getLinkName(s){
            var name = this.getLinkInfo(s.row).name;
            name = this.$dict.tranByPubMap(name)
            name = (this.labelCfg.linkTo ||  "链接->")+name;
            if(this.$attrs.goldFlag && this.$store.state.envCfg.goldBet!=1){
                name += `\n金币原值: ${this.reTranGold(s.row[this.$attrs.prop])}`;
            }
            return name;
        },
        toLink(s){
            var param = this.getLinkInfo(s.row);
            if(param===true) return;
            if(!param.code || ! param.name){
                console.info(`打开菜单参数不全(code:${param.code}&name:${param.name})`);
                return;
            }
            this.addTab("code"+param.code,param.name,param.initParam)
        },
        toHeadLink(){
            var linkinfo = this.$attrs.headlink;
            var link = {};
            if(typeof(linkinfo) =="function"){
                link = linkinfo();
            }else if(typeof(linkinfo) =="object"){
                link = linkinfo;
            }
            link = link || {};
            if(link===true) return;
            if(!link.code || ! link.name){
                console.info(`打开菜单参数不全(code:${link.code}&name:${link.name})`);
                return;
            }
            this.addTab("code"+link.code,link.name,link.initParam)
        }
    }
}
</script>