const app = getApp();
import * as echarts from '../../../../ec-canvas/echarts' // 这个是自己实际的目录
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    adcs: [],
    allActive: '展开更多',
    isAll: true,
    Ec: {
      lazyLoad: true,
    },
    startTime: "", //开始时间
    endTime: "", //结束时间
    searchTage:0,
    modalName:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {id,orderid} = options;
    if(orderid){
      this.getOrderInfo(orderid)
    }
    this.setData({id,orderid})
    this.getPdfProductInfo(id);
    this.getPdfCompany(id);
    this.getPdfExpress(id);
    this.getPdfPackageInfo(id);
    this.getPdfTempLines(id);
    this.getwarningInfo(id,0,'','');
  },
  //产品信息
  getPdfProductInfo(chainId) {
    var that = this;
    app.requestP({
      url: '/pdf/pdfProduct',
      method: 'post',
      data: {
        chainId
      }
    }).then(res => {
      let content = [];
      var arr = [];
      var datacontent = JSON.parse(res.data.data.content);
      if(typeof datacontent[0] == 'string'){
        datacontent.map((item,index)=>{
          var temp = item.replace('{','').replace('}','').split(',');
          temp.map((temps,index)=>{
            var value = temps.split(':');
            var valueStr = value[0].replace(/\s/g,'')
            content.push({[valueStr]:value[1]})
          })
        })
        content.map((item,index)=>{
          if(index % 2 != 0){
            arr.push(Object.assign(content[index],content[index-1]))
          }
        })
      }else if(typeof datacontent[0] == 'object'){
        arr = datacontent;
      }
      that.setData({
        productInfo: res.data.data,
        productContent:arr
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg)
    })
  },
  //企业信息
  getPdfCompany(chainId) {
    var that = this;
    app.requestP({
      url: '/pdf/pdfCompany',
      method: 'post',
      data: {
        chainId
      }
    }).then(res => {
      that.setData({
        companyInfo: res.data.data
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg)
    })
  },
  //物流节点
  getPdfExpress(chainId) {
    var that = this;
    app.requestP({
      url: '/pdf/express',
      method: 'post',
      data: {
        chainId
      }
    }).then(res => {
      that.setData({
        adcs: res.data.data
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg)
    })
  },
  //包装结构
  getPdfPackageInfo(chainId) {
    var that = this;
    app.requestP({
      url: '/pdf/packageInfo',
      method: 'post',
      data: {
        chainId
      }
    }).then(res => {
      that.setData({
        packageInfo:res.data.data
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg)
    })
  },
  //温度曲线
  getPdfTempLines(chainId) {
    var that = this;
    app.requestP({
      url: '/pdf/tempLines',
      method: 'post',
      data: {
        chainId
      }
    }).then(res => {
      that.setData({
        tempLines: res.data.data.map((item,index)=>{
          return{...item,id:'llzsChartDom'+index,index,}
        }),
      })
      res.data.data.map((item,index)=>that.setTemp({...item,id:'llzsChartDom'+index,index,}))
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg);
    })
    
  },
  setTemp(temperature) {
    var that = this;
    that.selectComponent('#'+temperature.id).init((canvas, width, height, dpr) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
      });
      var option = that.getTempOption();//v.startLocal
      option.series[0].data = temperature.temps.map(v => v.temp);
      option.xAxis.data = temperature.temps.map(v => v.createTime.replace(" ", "\n"));
      chart.setOption(option);
      that.wenChart = chart;
      return chart;
    });
    
  },
  getTempOption() {
    return {
      title: {
        show:false,
        text:'' ,
        textStyle: {
          color: '#999',
          fontSize: 16,
        }
      },
      tooltip:{
        show:true,
        formatter: '{b}\n{c}℃',
        trigger:"axis",
        position:"top"
      },
      grid: {
        top: "15%",
        left: "4%",
        right: "5%",
        bottom: "3%",
        containLabel: true
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        nameTextStyle: {
          color: "#77869E",
          fontSize: 12
        },
        axisLine: {
          onZero: false,
          lineStyle: {
            color: "#cccccc"
          }
        },
        axisTick: {
          show: false
        },
        splitLine: {
          show: false,
        },
        axisLabel: {
          show: true,
          textStyle: {
            color: '#999', //更改坐标轴文字颜色
            fontSize: 12, //更改坐标轴文字大小
          }
        },
        data: ['16:00 \n 2021-06-30', '16:00 \n 2021-06-30', '16:00 \n 2021-06-30', '16:00 \n 2021-06-30', '16:00 \n 2021-06-30', '16:00 \n 2021-06-30', '16:00 \n 2021-06-30'],
        // show: false
      },
      yAxis: {
        type: 'value',
        splitNumber: 10,
        max: 40,
        axisLine: {
          lineStyle: {
            color: "#cccccc",
          },
          show: true
        },
        axisLabel: {
          show: true,
          textStyle: {
            color: '#999', //更改坐标轴文字颜色
            fontSize: 12, //更改坐标轴文字大小
          }
        },
        splitLine: {
          show: false
        }
      },
      series: [{
        type: 'line',
        showSymbol: false,
        smooth: true,
        lineStyle: {
          width: 1,
          color: "#328EFD", //#D4D4D4
        },
        markLine:{
          lineStyle:{color:"#F22424",},
          data:[
            { yAxis: 40}
          ]
        },
        markPoint :{
          symbolSize : 30,
          //symbol:'diamond',
          label:{
            fontSize:12,
          },
          data: [
            {type: 'max', name: '最大值'},
            {type: 'min', name: '最小值'}
          ]
        },
        areaStyle: {
          origin: "start",
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0,
              color: '#FFFFFF' //
            }, {
              offset: 1,
              color: '#4C96FE' //#D4D4D4
            }],
            global: false
          }
        },
        data: [18, 36, 25, 30, -20, 40, 33]
      }]
    }
  },
  //报警信息
  getwarningInfo(id,searchTage,startTime,endTime){
    var that = this;
    app.requestP({
      url:'/pdf/warningInfo',
      method:'post',
      data:{
        chainId:id,
        endTime:endTime || '',
        searchTage,
        startTime:startTime || '',
      }
    }).then(res=>{
      console.log(res.data.data);
      that.setData({
        warningList:res.data.data
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg)
    })
  },
  setNav(e){
    var nav = e.currentTarget.dataset.val;
    this.setData({searchTage:nav,warningList:[],});
    var {id,searchTage,startTime,endTime} = this.data;
    // this.getwarningInfo(id,searchTage,startTime,endTime);
    if(nav == 0 || nav == 3){
      this.getwarningInfo(id,searchTage,);
    }
  },
  endDateChange(e) {
    this.setData({
      endTime: e.detail.value
    })
  },
  startDateChange(e) {
    this.setData({
      startTime: e.detail.value
    })
  },
  submitDate(){
    var {id,searchTage,startTime,endTime} = this.data;
    this.getwarningInfo(id,searchTage,startTime,endTime);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onAll() {
    if (this.data.isAll) {
      this.setData({
        allActive: '收回',
        isAll: false,
      })
    } else {
      this.setData({
        allActive: '展开更多',
        isAll: true,
      })
    }
  },
  saveCode(e) {
    var that = this
    wx.downloadFile({
      url:'http://oss.zhongyihz.cn/group156/images/06619e9e4bfc54382290d931bf9814c2.png',
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath, // 此为图片路径
          success: (res) => {
            wx.showToast({
              title: '保存成功，请到相册中查看'
            });
          },
          fail: (err) => {
            wx.showToast({
              icon: 'none',
              title: '保存失败，请稍后重试'
            });
          }
        })
      },
      fail() {
        wx.showToast({
          icon: 'none',
          title: '保存失败，请稍后重试'
        });
      }
    })
  },
  downLabel(){
    this.setData({modalName: "goApp"})
  },
  getOrderInfo(uid){
    app.requestP({
      url: "/transport/get?id="+uid,
      method: "get",
    }).then(res => {
      var transportOrder = res.data.transportOrder;
      this.setData({transportOrder})
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
})