<template>
    <div class="edb-detail-wrap" v-show="edbData.DataList">
        <chartInfo
            :edbData="edbData"
            :lang="lang"
            :isAllowEditLimit="isAllowEditLimit"
            @refreshData="refreshData"
            ref="chartInfo"
        />
        <div class="info" style="display: flex;justify-content: flex-end;">
          <!--   <span>数据来源：{{edbData.EdbInfo.SourceName}}</span> -->
            <span>添加人：{{edbData.EdbInfo.SysUserRealName}}</span>
        </div>
        <ul
            class="value-ul"
            v-show="showData&&dataList.length"
            >
            <li class="value-item" style="background-color: #EBEFF6;">
                <span style="width:240px">日期（频度：{{EdbData.Frequency}}）</span>
                <span style="flex:1;">值</span>
            </li>
            <li
                class="value-item"
                v-for="item in dataList.slice(0,3)"
                :key="item.EdbDataId"
            >
                <span class="value-label date" style="width:240px">
                    <span style="position: relative;">
                        <i class="new-tag" v-if="EdbData.LatestDate===item.DataTime"></i>
                        {{ item.DataTime }}
                    </span>
                </span>
                <span
                    :class="['value-label',{'predict-act': item.isPredic}]"
                    style="flex:1;"
                >
                    <span :class="['value-style',{'predict-act': item.isPredic}]">{{item.Value}}</span>
                </span
                >
            </li>
            <li class="nodata value-item" v-if="!dataList.length">暂无数据</li>
        </ul>

    </div>
</template>

<script>
import * as preDictEdbInterface from '@/api/modules/predictEdbApi.js';
import { dataBaseInterface } from '@/api/api.js';
import chartInfo from './chartInfo.vue'
export default {
    props:{
        id: Number,
        lang: String,
        isAllowEditLimit:Boolean,
    },
    components:{chartInfo},
    watch:{
        id:{
            handler(n){
                if(n){
                    this.$nextTick(()=>{
                        this.getDetail()
                    })
                }
            },
            immediate:true
        },
        lang(newval){
            this.$emit('updateTit',{
                edb_name: this.edbData.EdbInfo.EdbName,
                edb_nameEn: this.edbData.EdbInfo.EdbNameEn,
                userid: this.edbData.EdbInfo.SysUserId
            })
        }
    },
    data() {
        return {
            edbData:{
                DataList:null,
                EdbInfo:{}
            },
            showData:false,
            dataList: [],
            EdbData:{}
        }
    },
    methods: {
        getDetail(type=''){
			let dateArray=[]
            let DateType,StartYear;
            
            if(this.$refs.chartInfo.chartInfo.ChartType==2){
                DateType=this.$refs.chartInfo.year_select_season
                StartYear=this.$refs.chartInfo.count_year_season
                if(!(this.$refs.chartInfo.season_year && this.$refs.chartInfo.season_year.length>0)){
                    // 初始化日期显示
                    let latestYear = parseInt(this.$refs.chartInfo.tableData[0].LatestDate.substring(0,4))
                    this.$refs.chartInfo.season_year = [`${latestYear-StartYear+1}-01-01`,`${latestYear}-12-31`];
                }
                dateArray = this.$refs.chartInfo.season_year
            }else{
                DateType=this.$refs.chartInfo.year_select
                StartYear=this.$refs.chartInfo.count_year
                dateArray = this.$refs.chartInfo.select_date
            }

            preDictEdbInterface.edbChartInfo({
				EdbInfoId: this.id,
                DateType,
				StartDate: dateArray[0]||'',
				EndDate: dateArray[1]||'',
                ChartType:this.$refs.chartInfo.chartInfo.ChartType,
                // ETA1.0.5 去除了这两个参数
                // SeasonStartDate:this.$refs.chartInfo.season_year[0]||'',
                // SeasonEndDate:this.$refs.chartInfo.season_year[1]||'',
                Calendar:this.$refs.chartInfo.calendar_type,
                StartYear:StartYear || 0
			}).then(res=>{
                if(res.Ret!=200) return
                this.edbData.DataList=res.Data.DataList||[]
                this.edbData.EdbInfo={
                    ...res.Data.EdbInfo,
                    ChartColor:'',
                    ChartStyle:'',
                    ChartType:0,
                    ChartWidth:0,
                    MaxData:type==='saveLimit' ? this.$refs.chartInfo.tableData[0].MaxData : res.Data.EdbInfo.MaxValue,
                    MinData:type==='saveLimit' ? this.$refs.chartInfo.tableData[0].MinData : res.Data.EdbInfo.MinValue,
                    EdbInfoCategoryType:1,
                    EdbInfoType:1,
                    EdbName:res.Data.EdbInfo.EdbName,
                    IsAxis:1
                }
                this.$emit('updateTit',{
                    edb_name: res.Data.EdbInfo.EdbName,
                    edb_nameEn: res.Data.EdbInfo.EdbNameEn,
                    userid: res.Data.EdbInfo.SysUserId
                })
                this.$emit('setOpera',res.Data.Button)

                this.$emit('setCurrentClassify',res.Data.ClassifyList||[])
                //更新图片
                if(type==='updateImg' || !res.Data.EdbInfo.ChartImage) {
                    console.log(type);
                    console.log(res.Data.EdbInfo.ChartImage);
                    this.$nextTick(() => {
                        this.setChartImage()
                    })
                }
                this.getData()
            })
        },
        refreshData(type=''){
            this.getDetail(type)
        },
        changeEnOptions() {
            this.$emit('openEnNameDia',this.edbData.EdbInfo)
        },

        /* 关联图片 */
        setChartImage() {
            let svg = this.$refs.chartInfo.$refs.chartRef.chart.getSVG({
                chart:{
                width: 340,
                height: 230,
                }
            });
            let form = new FormData();
            form.append('Img', svg);
            this.setImageHandle(form);
        },

        async setImageHandle(form) {
            let { Data } = await dataBaseInterface.uploadImgSvg(form);
            await preDictEdbInterface.setImg({
                EdbInfoId: this.id,
                ImageUrl: Data.ResourceUrl,
            });
        },
        getData(){
            preDictEdbInterface.edbDataInfo({
                EdbInfoId: this.id,
                CurrentIndex: 1
            }).then(res => {
                this.showData = true;
                if(res.Ret!==200) return 
                const {Item} = res.Data
                Item.PredictDataList && Item.PredictDataList.forEach(_ => {
                    _.isPredic = true;
                })
                this.EdbData = Item
                this.dataList = [...Item.PredictDataList,...Item.DataList]
            })
        },

    },

}
</script>

<style lang="scss" scoped>
.value-ul {
    margin-top: 10px;
    border-bottom: 1px solid #EBEFF6;
    .value-item {
        padding: 10px 0;
        border: 1px solid #EBEFF6;
        border-bottom: none;
        display: flex;
        justify-content: space-around;
        >span{
            padding:0 16px;
            box-sizing: border-box;
        }
        .value-label {
            position: relative;
            color: #666;
            .value-style{
                /* background-color: #ECF2FE;
                color: #0052D9; */
                padding:5px;
                border-radius: 4px;
                &.predict-act {
                    color: orange;
                }
            }
            &.date{
                &::after{
                    content: '';
                    position:absolute;
                    right:0;
                    top:-14px;
                    height:calc(100% + 28px);
                    width:1px;
                    background-color: #EBEFF6;
                }
            }
        }
        .predict-act {
            color: orange;
        }
        .new-tag {
            width: 6px;
            height: 6px;
            display: inline-block;
            position: absolute;
            left: -12px;
            top: 50%;
            transform: translateY(-50%);
            border-radius: 50%;
            background: #f00;
        }
    }
    .nodata {
        text-align: center;
        padding: 40px 0;
        color: #999;
    }
}
</style>