<template>
  <modulen>
    <template #title class="title">通行量环比</template>
    <template #end>
      <date-switcher
        :type="3"
        v-model="selectType"
        :selectTime="2"
        @change="handleChangeDate"
      ></date-switcher>
    </template>
    <div class="traff-tb-md">
      <div class="tj-left">
        <el-carousel
          class="txtb-carousel"
          direction="vertical"
          indicator-position="none"
          :loop="true"
          :interval="3000"
        >
          <!-- 这里循环两次是让他可以看起来像无限循环 -->
          <div v-for="item in 2" :key="item">
            <el-carousel-item>
              <div class="tj-left-item tj-left-item-a">
                <div class="tjli-text1">
                  <span class="tjli-text1-l">{{huanbiText}}车辆</span>
                  <span class="tjli-text1-r">
                    <i
                      :class="
                        dataInfo.bncl > dataInfo.qncl
                          ? 'el-icon-top'
                          : 'el-icon-bottom'
                      "
                    ></i>
                    {{
                      percentage.vehicle
                    }}%
                  </span>
                </div>
                <div class="tjli-text2">
                  <span class="tjli-text2-l">{{
                    formatThousand(dataInfo.bncl || 0)
                  }}</span
                  ><span class="tjli-text2-r">辆</span>
                </div>
              </div>
              <div class="tj-left-item tj-left-item-b">
                <div class="tjli-text1">
                  <span class="tjli-text1-l">{{ huanbilastText }}车辆</span>
                </div>
                <div class="tjli-text2">
                  <span class="tjli-text2-l">{{
                    formatThousand(dataInfo.qncl || 0)
                  }}</span
                  ><span class="tjli-text2-r">辆</span>
                </div>
              </div>
              <div class="tj-left-item tj-left-item-a">
                <div class="tjli-text1">
                  <span class="tjli-text1-l">{{ huanbiText }}金额</span>
                  <span class="tjli-text1-r">
                    <i
                      :class="
                        dataInfo.bnje > dataInfo.qnje
                          ? 'el-icon-top'
                          : 'el-icon-bottom'
                      "
                    ></i>
                    {{
                     percentage.money
                    }}%
                  </span>
                </div>
                <div class="tjli-text2">
                  <span class="tjli-text2-l">{{
                    formatThousand(dataInfo.bnje || 0)
                  }}</span
                  ><span class="tjli-text2-r">万元</span>
                </div>
              </div>
              <div class="tj-left-item tj-left-item-b">
                <div class="tjli-text1">
                  <span class="tjli-text1-l">{{huanbilastText }}金额</span>
                </div>
                <div class="tjli-text2">
                  <span class="tjli-text2-l">{{
                    formatThousand(dataInfo.qnje || 0)
                  }}</span
                  ><span class="tjli-text2-r">万元</span>
                </div>
              </div>
            </el-carousel-item>
            <el-carousel-item>
              <div class="tj-left-item tj-left-item-a">
                <div class="tjli-text1">
                  <span class="tjli-text1-l  tjli-text1-zmd">{{ huanbiText}}断面流量</span>
                  <span class="tjli-text1-r">
                    <i
                      :class="
                        dataInfo.bndmll > dataInfo.qndmll
                          ? 'el-icon-top'
                          : 'el-icon-bottom'
                      "
                    ></i>
                    {{percentage.flow }}%
                  </span>
                </div>
                <div class="tjli-text2">
                  <span class="tjli-text2-l">{{
                    formatThousand(dataInfo.bndmll || 0)
                  }}</span
                  ><span class="tjli-text2-r">辆</span>
                </div>
              </div>
              <div class="tj-left-item tj-left-item-b">
                <div class="tjli-text1">
                  <span class="tjli-text1-l tjli-text1-zmd">{{ huanbilastText }}断面流量</span>
                </div>
                <div class="tjli-text2">
                  <span class="tjli-text2-l">{{
                    formatThousand(dataInfo.qndmll || 0)
                  }}</span
                  ><span class="tjli-text2-r">辆</span>
                </div>
              </div>
            </el-carousel-item>
          </div>
        </el-carousel>
      </div>
      <div class="chart-box">
        <Ect :options="ectOpts" />
      </div>
    </div>
  </modulen>
</template>

<script>
import { formatThousand } from "@/utils/util";
import {
  inOutSummary,
  inOutSummaryChild,
  gateSummary,
  gateSummaryChild,
} from "@/api/chargeInfo.js";
import moment from "moment";
export default {
  data() {
    return {
      selectType: 2,
      ectOpts: {},
      categoryData: [], // 类目轴
      bnclData: [], // 本月车辆 (统计图)
      qnclData: [], // 上月车辆 (统计图)
      bnjeData: [], // 本月金额 (统计图)
      qnjeData: [], // 上月金额 (统计图)
      bndmllData: [], // 本月断面流量 (统计图)
      qndmllData: [], // 上月断面流量 (统计图)
      query: {
        timemode: "month",
        timeflag:  String( moment(new Date()).subtract("year").format("YYYY")+moment(new Date()).subtract(1, "month").format("MM"))   ,
        level: 0,
        dimension: "",
      },
      dataInfo: {
        bncl: "",
        qncl: "",
        bnje: "",
        qnje: "",
        bndmll: "",
        qndmll: "",
      },
      percentage:{
        money:"",
        vehicle:"",
        flow:""
      },
      huanbiText:moment(new Date()).subtract(1,"month").format("MM月"),
      huanbilastText:moment(new Date()).subtract(2,"month").format("MM月"),//上月同比
      nowText: '上月',
      lastText: '上上月',
     lastyQuery:{}
    };
  },
  computed: {
    getDate() {
      this.query.timeflag = moment(this.$store.state.appDate).subtract(1,"month").format("yyyyMM")
      return this.$store.state.appDate;
    },
    selectRoad() {
      return this.$store.state.road.selectRoadId;
    },
  },
  watch: {
    getDate(newVal) {
      this.changeData(this.selectType, newVal)
      // this.getData(this.selectType, newVal)
     
    },
    selectRoad(newVal) {
      const level = this.$store.state.road.selectLevel;
      const roadId = newVal ? newVal[level - 1] : "";
      this.query.level = level;
      this.query.dimension = roadId;
      this.getData();
    },
  },
  // mounted() {
  //   this.getData();
  // },
  methods: {
    formatThousand,
    handleChangeDate(value) {
      this.selectType = value;
      this.changeData(value, this.getDate)
    },
    changeData(value, date) {
       let loseMonth=  new Date(this.getDate).getMonth()+1
       // 年   不需要用到
       if (value === 1) {
        this.query.timemode = "year";
        // 上一年上一月
        this.query.timeflag = moment(date).subtract("year").format("YYYY") ;
       }
       //月
       if (value === 2) {
        this.query.timemode = "month";
       // //如果选择月份为1月时   就年份减一 ，月份变成12月   
        if(loseMonth===1){
        let year = moment(date).subtract(1,"year").format("YYYY")
        let month = moment(date).subtract(1,"month").format("MM")
        // let days = moment(new Date()).subtract(1,"days").format("DD")
        this.query.timeflag = year + month;
        }
          // //如果选择月份不为为1月时   月份减一 
        if(loseMonth!==1){
        let year = moment(date).subtract("year").format("YYYY")
        let month = moment(date).subtract(1,"month").format("MM")
        // let days = moment(new Date()).subtract(1,"days").format("DD")
        this.query.timeflag = year + month;
        }
        this.nowText='上月',
        this.huanbiText = moment(this.query.timeflag).format("MM月")
        } 
        //日
        if(value==3) {
        this.query.timemode = "day";
       // //如果选择月份为1月时   就年份减一 ，月份变成12月  
        if(loseMonth===1){
        let year = moment(date).subtract(1,"year").format("YYYY")
        let month = moment(date).subtract("month").format("MM")
        let days = moment(new Date()).subtract(1,"days").format("DD")
        this.query.timeflag = year + month +days;

        console.log(254,  this.query.timeflag );
        }

          // //如果选择月份不为为1月时   月份减一 
        if(loseMonth!==1){
        let year = moment(date).subtract("year").format("YYYY")
        let month = moment(date).subtract("month").format("MM")
        let days = moment(new Date()).subtract(1,"days").format("DD")
         if(moment(new Date()).subtract("days").format("DD") =="01"){
          month = moment(date).subtract(1,"month").format("MM")
         };
        this.query.timeflag = year + month+days;
        }
        this.nowText= '当前月日';
        this.huanbiText = moment(this.query.timeflag).format("MM月DD日");
        if( this.huanbiText=='Invalid date'){
                this.huanbiText="异常/特殊日期"
              }
        
      }
      this.getData();
    },

            async getData(date) {
              // 获取去年的年份
            let loseMonth=  new Date(this.getDate).getMonth()+1
            let lastY =  moment(this.getDate).subtract("year").format("YYYY")+moment(this.getDate).subtract( "month").format("MM");
            this.lastyQuery = Object.assign({}, this.query);
            //如果月份为一月时 年份减一
            if( loseMonth==1){ lastY =  moment(this.getDate).subtract(1,"year").format("YYYY")+moment(this.getDate).subtract(2, "month").format("MM");  } 
             //如果月份为二月时 年份减一
            if( loseMonth==2){ lastY =  moment(this.getDate).subtract("year").format("YYYY")+moment(this.getDate).subtract(1, "month").format("MM");  } 
            this.lastyQuery.timeflag = lastY +  this.lastyQuery.timeflag.substr(6);
       

             if( this.lastyQuery.timeflag.length ==6){
             lastY =  moment(this.getDate).subtract("year").format("YYYY")+moment(this.getDate).subtract(2, "month").format("MM");
             if( loseMonth==1){ lastY =  moment(this.getDate).subtract(1,"year").format("YYYY")+moment(this.getDate).subtract(2, "month").format("MM");  } 
             if( loseMonth==2){ lastY =  moment(this.getDate).subtract(1,"year").format("YYYY")+moment(this.getDate).subtract(2, "month").format("MM");  } 
             this.lastyQuery = Object.assign({}, this.query);
             this.lastyQuery.timeflag = lastY +  this.lastyQuery.timeflag.substr(6);
             this.lastText= '上上月'
             this.huanbilastText =moment( this.lastyQuery.timeflag).format("MM月")
             }

              else  if( this.lastyQuery.timeflag.length ===8){   
               //上面传来的值 并减一 为环比上一个月
              let lastY =  moment(this.getDate).subtract("year").format("YYYY")+moment(this.getDate).subtract(1,"month").format("MM");
              if(moment(new Date()).subtract("days").format("DD") =="01"){
              lastY=  moment(this.getDate).subtract("year").format("YYYY")+moment(this.getDate).subtract(2, "month").format("MM");
           
              };      
              if( loseMonth==1){  
               //如果月份为一月时 年份减一
              lastY =  moment(lastY).subtract(1,"year").format("YYYY")+moment(lastY).subtract("month").format("MM");
             
              } 
              else if( 
              //如果月份为二月时 
              loseMonth==2){
                 lastY =  moment(lastY).subtract("year").format("YYYY")+moment(lastY).subtract("month").format("MM");
                 console.log(311,lastY);
               } 
               else{ 
              //如果月份为不为二月时 
              lastY =  moment(lastY).subtract("year").format("YYYY")+moment(lastY).subtract("month").format("MM"); 
              }
              
              this.lastyQuery.timeflag = lastY +  this.lastyQuery.timeflag.substr(6);
              this.lastText= '上月日'
              this.huanbilastText =moment( this.lastyQuery.timeflag).format("MM月DD日")
              if( this.huanbilastText=='Invalid date'){
                this.huanbilastText="异常/特殊日期"
              }
             }
  
         
      const data = await inOutSummary(this.query);
      const datadm = await gateSummary(this.query);
    
      this.dataInfo.bncl = data.msgContent[0]?.flowCount||0; //本月车辆
      this.dataInfo.bnje = data.msgContent[0]?.feeSum||0; //本月金额
      this.dataInfo.bndmll = datadm.msgContent[0]?.flowCount||0; //本月断面流量
      //上月
      const data1 = await inOutSummary( this.lastyQuery);
      const datadm1 = await gateSummary( this.lastyQuery);

      this.dataInfo.qncl = data1.msgContent[0]?.flowCount||0; //上月车辆
      this.dataInfo.qnje = data1.msgContent[0]?.feeSum||0; //上月金额
      this.dataInfo.qndmll = datadm1.msgContent[0]?.flowCount||0; //上月断面流量

         if(this.huanbilastText=='Invalid date'){
           this.dataInfo.qncl=0
           this.dataInfo.qnje =0
           this.dataInfo.qndmll=0
           }
         if(this.huanbiText=='Invalid date'){
           this.dataInfo.bncl=0
           this.dataInfo.bnje=0
           this.dataInfo.bndmll=0
           }

      this.percentage.vehicle=(((this.dataInfo.bncl - this.dataInfo.qncl) / this.dataInfo.qncl) *100||0).toFixed(0),
      this.percentage.money =(((this.dataInfo.bnje - this.dataInfo.qnje) /  this.dataInfo.qnje) *100 || 0).toFixed(0),
      this.percentage.flow=(((this.dataInfo.bndmll - this.dataInfo.qndmll) /  this.dataInfo.qndmll) * 100 || 0 ).toFixed(0)

      if( this.percentage.money =="Infinity"){ this.percentage.money=100}
      if( this.percentage.vehicle =="Infinity"){this.percentage.vehicle=100}
      if( this.percentage.flow =="Infinity"){ this.percentage.flow=100}
      this.categoryData = []; // 类目轴
      this.bnclData = []; // 本月车辆 (统计图)
      this.qnclData = []; // 上月车辆 (统计图)
      this.bnjeData = []; // 本月金额 (统计图)
      this.qnjeData = []; // 上月金额 (统计图)
      this.bndmllData = []; // 本月断面流量 (统计图)
      this.qndmllData = []; // 上月断面流量 (统计图)

      // 统计图 ===================
      //本月
      const data2 = await inOutSummaryChild(Object.assign({}, this.query));
       data2.msgContent.forEach((ele) => {
        this.bnclData.push(ele.flowCount); // 本月车辆
        this.bnjeData.push(ele.feeSum); // 本月金额
      });
      const datadm2 = await gateSummaryChild(this.query);
      datadm2.msgContent.forEach((ele) => {
        this.categoryData.push(ele.orgName); // 统计图横坐标 （这个几统计图的接口都可以取）
        this.bndmllData.push(ele.flowCount); // 本月断面流量
      });
      // 上月
      const data3 = await inOutSummaryChild(Object.assign({},  this.lastyQuery));
      data3.msgContent.forEach((ele) => {
        this.qnclData.push(ele.flowCount); // 上月车辆
        this.qnjeData.push(ele.feeSum); // 上月车辆
      });

      const datadm3 = await gateSummaryChild( this.lastyQuery);
      datadm3.msgContent.forEach((ele) => {
        this.qndmllData.push(ele.flowCount); // 上月车辆
      });
      this.getCharts();
    
    },

    getCharts() {
      const legendData = [
        this.nowText + '车辆',
        this.lastText + '车辆',
        this.nowText + '断面流量',
        this.lastText + '断面流量',
        this.nowText + '金额',
        this.lastText + '金额'
      ]
      this.ectOpts = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
        },
        grid: {
          left: 60,
          right: 60,
          bottom: 30,
        },
        color: [
          "#4999FF",
          "#40EFFF",
          "#22FF9A",
          "#FEC43C",
          "#0084FB",
          "#00E3FF",
        ],
        legend: {
          // data: ["收入", "车流量"],
          // data: [
          //   "本月车辆",
          //   "上月车辆",
          //   "本月断面流量",
          //   "上月断面流量",
          //   "本月金额",
          //   "上月金额",
          // ],
          data: legendData,
          textStyle: {
            fontSize: 11, //字体大小
            color: "#CBE7FF", //字体颜色
          },
          left: "center",
          borderRadius: 0,
          itemWidth: 17,
          itemHeight: 8,
        },
        xAxis: {
          type: "category",
          data: this.categoryData,
          splitLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            show: true,
            
            textStyle: {
              color: "#778FA4",
            },
          },
        },
        yAxis: [
          {
            type: "value",
            name: "车辆数(辆)",
            nameTextStyle: {
              color: "#909FAC",
              fontSize: 12,
              fontWeight: "bold",
            },
            min: 0,
            // max: 250,
            position: "left",
            axisLine: {
              show: true,
            },
            axisLabel: {
              formatter: function(v){
                        function tranNumber(num){
                       var numStr = num.toString();
                       // 十万以内直接返回
                       if (numStr.length <5 ) {
                           return numStr;
                       }
                       //大于6位数是十万 (以10W分割 10W以下全部显示)
                       else if (numStr.length > 5) {
                           var decimal = numStr.substring(numStr.length - 4, numStr.length - 4)
                           return parseFloat(parseInt(num / 10000) + '.' + decimal) + '万';
                       }else if(numStr.length == 5){
                           var decimal = numStr.substring(numStr.length - 3, numStr.length - 4)
                           return parseFloat(parseInt(num / 10000) + '.' + decimal) + '万';
                       } 
                   }
                     return  tranNumber(v);
                 },  
              textStyle: {
                color: "#778FA4",
              },
            },
            splitLine: {
              show: false,
            },
          },
          {
            type: "value",
            name: "金额(元)",
            nameTextStyle: {
              color: "#909FAC",
              fontSize: 12,
              fontWeight: "bold",
            },
            min: 0,
            // max: 250,
            position: "right",
            offset: 0,
            axisLine: {
              show: true,
            },
            axisLabel: {
              formatter: function(v){
                         function tranNumber(num){
                       var numStr = num.toString();
                       // 十万以内直接返回
                       if (numStr.length <4 ) {
                           return numStr;
                       }
                       //大于6位数是十万 (以10W分割 10W以下全部显示)
                       else if (numStr.length >5 ) {
                           var decimal = numStr.substring(numStr.length - 4, numStr.length - 4)
                           return parseFloat(parseInt(num / 10000) + '.' + decimal) + '亿';
                       }
                       else if (numStr.length >=5 ) {
                            var decimal = numStr.substring(numStr.length - 3, numStr.length - 4)
                           return parseFloat(parseInt(num / 10000) + '.' + decimal) + '亿';
                       }
                       else if (numStr.length ==4 ) {
                            var decimal = numStr.substring(numStr.length - 3, numStr.length - 3)
                           return parseFloat(parseInt(num / 1000) + '.' + decimal) + '千万';
                       }
                   }
                     return  tranNumber(v);
                 },  
              textStyle: {
                color: "#778FA4",
              },
            },
            splitLine: {
              show: false,
            },
          },
        ],
        series: [
          {
            name: legendData[0],
            data: this.bnclData,
            itemStyle: {
              normal: {
                color: function (params) {
                  return {
                    type: "linear",
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [
                      {
                        offset: 0,
                        color: "#4999FF",
                      },
                      {
                        offset: 0.5,
                        color: "#4999FF",
                      },
                      {
                        offset: 1,
                        color: "#4999FF",
                      },
                    ],
                  };
                },
                barBorderRadius: [10, 10, 0, 0],
              },
            },
            type: "bar",
            barMaxWidth: "5",
            showBackground: true,
            backgroundStyle: {
              color: "rgba(59, 152, 219, 0)",
              borderWidth: 35,
              borderColor: "rgba(59, 152, 219, 0.08)",
            },
          },
          {
            name: legendData[1],
            data: this.qnclData,
            itemStyle: {
              normal: {
                color: function (params) {
                  return {
                    type: "linear",
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [
                      {
                        offset: 0,
                        color: "#40EFFF",
                      },
                      {
                        offset: 0.5,
                        color: "#40EFFF",
                      },
                      {
                        offset: 1,
                        color: "#40EFFF",
                      },
                    ],
                  };
                },
                barBorderRadius: [10, 10, 0, 0],
              },
            },
            type: "bar",
            barMaxWidth: "5",
          },
          {
            name: legendData[2],
            data: this.bndmllData,
            itemStyle: {
              normal: {
                color: function (params) {
                  return {
                    type: "linear",
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [
                      {
                        offset: 0,
                        color: "#22FF9A",
                      },
                      {
                        offset: 0.5,
                        color: "#22FF9A",
                      },
                      {
                        offset: 1,
                        color: "#22FF9A",
                      },
                    ],
                  };
                },
                barBorderRadius: [10, 10, 0, 0],
              },
            },
            type: "bar",
            barMaxWidth: "5",
          },
          {
            name: legendData[3],
            data: this.qndmllData,
            itemStyle: {
              normal: {
                color: function (params) {
                  return {
                    type: "linear",
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [
                      {
                        offset: 0,
                        color: "#FEC43C",
                      },
                      {
                        offset: 0.5,
                        color: "#FEC43C",
                      },
                      {
                        offset: 1,
                        color: "#FEC43C",
                      },
                    ],
                  };
                },
                barBorderRadius: [10, 10, 0, 0],
              },
            },
            type: "bar",
            barMaxWidth: "5",
          },
          {
            name: legendData[4],
            yAxisIndex: 1,
            // data: this.flowCount,
            data: this.bnjeData,
            type: "line",
            smooth: true,
            showSymbol: false,
            itemStyle: {
              color: "#0084FB",
            },
            lineStyle: {
              width: 2,
              color: "#0084FB", //折线颜色
              shadowOffsetX: 0, // 折线的X偏移
              shadowOffsetY: 0, // 折线的Y偏移
              shadowBlur: 30, // 折线模糊
              shadowColor: "#0084FB", //折线颜色
            },
          },
          {
            name: legendData[5],
            yAxisIndex: 1,
            // data: this.flowCount,
            data: this.qnjeData,
            type: "line",
            smooth: true,
            showSymbol: false,
            itemStyle: {
              color: "#00E3FF",
            },
            lineStyle: {
              width: 2,
              color: "#00E3FF", //折线颜色
              shadowOffsetX: 0, // 折线的X偏移
              shadowOffsetY: 0, // 折线的Y偏移
              shadowBlur: 30, // 折线模糊
              shadowColor: "#00E3FF", //折线颜色
            },
          },
        ],
      };
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~/styles/viewport-convert.scss";
::v-deep(.el-carousel__container) {
  height: 100%;
}
// ::v-deep(.module-header) {
//   z-index: 999;
// }
.traff-tb-md {
  width: 100%;
  height: 100%;
  padding-top: 9px;
  display: flex;
  .tj-left {
    width: vw(177);
    margin-right: vw(15);
    height: 100%;
  }
  .txtb-carousel {
    height: 100%;
  }
  .chart-box {
    width: calc(100% - vw(192));
    height: 100%;
    position: relative;
    float:left
  }
  .tj-left-item {
    width: 100%;
    // height: calc(25% - 5px);
    height: vh(66);
    // margin-bottom: 5px;
    white-space: nowrap;
			overflow: hidden;
    margin-bottom: 5px;
    padding: vh(8) vw(12);
  }
  .tj-left-item-a {
    background: url("@/assets/images/ChargeInfo/trafftbbg1.png");
    background-size: 100% 100%;
  }
  .tj-left-item-b {
    background: url("@/assets/images/ChargeInfo/trafftbbg2.png");
    background-size: 100% 100%;
  }
  .tjli-text1 {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .tjli-text1-l {
    color: #b1dfff;
  }

  // .tjli-text1-zmd{
  //    width: 50px;
  //    position: relative;
  //    right: 5px;
	// 		width: fit-content;
	// 		animation:move 5s linear infinite;
  //      padding-left:100px;
  //     color: #b1dfff;
  // }
  @keyframes move {
			0% {
				transform: translateX(0);
			}
			100% {
				transform: translateX(-100%);
			}
		}
  .tjli-text1-r {
     position: absolute; 
     right: 10px;
     margin-top: 25px;
     color: #fec43c;
     font-size: vw(13);
     font-family: "DinNextRegular";
  }
  .tjli-text2-l {
    font-size: vw(17);
    font-family: "DinNextRegular";
  }
  .tjli-text2-r {
    font-size: vw(13);
    color: #96beda;
    margin-left: 5px;
  }
}

</style>
