// pages/Progress-management/index.js
import * as echarts from '../../ec-canvas/echarts'; 

const app = getApp()

let chart=null
let chart1=null

function getOption(a1) {
   let  that=this
return {
    title: [
        {
            text: a1+'%',
            textAlign: "center",
            left: "48%",
            top: "30%",
            textStyle: {
                color: "#67abf2",
                fontSize: 23,
            },
        },
        {
            text: '实际产值进度',
            left: "48%",
            top: "80%",
            textAlign: "center",
            textStyle: {
                color: "#cdd7fa",
                fontWeight: "normal",
                fontSize: "16",
                textAlign: "center",
            },
        },
    ],
    series: [
        {
            type: "pie",
            hoverAnimation: false,
            radius: ["60%", "59%"],
            center: ["50%", "40%"],
            labelLine: {
                normal: {
                    show: false,
                },
            },
            label: {
                normal: {
                    position: "center",
                },
            },
            data: [
                {
                    value: 100,
                    itemStyle: {
                        normal: {
                            color: "#255788",
                        },
                    },
                },

            ],
        },
        {
            type: "pie",
            hoverAnimation: false,
            radius: ["53%", "60%"],
            center: ["50%", "40%"],
            labelLine: {
                normal: {
                    show: false,
                },
            },
            label: {
                normal: {
                    position: "center",
                },
            },
            data: [
                {
                    value: a1,
                    itemStyle: {
                        normal: {
                            color: "#68a8f2",
                        },
                    },
                    normal: {
                        show: false,
                    },
                },
                {
                    value: 100-35,
                    itemStyle:  {
                        normal: {
                            label: {
                                show: false,
                            },
                            labelLine: {
                                show: false,
                            },
                            color: "rgba(0,0,0,0)",
                            borderWidth: 0,
                        },
                        emphasis: {
                            color: "rgba(0,0,0,0)",
                            borderWidth: 0,
                        },
                    },
                },
            ],
        },
    ],
};
}

function getOption1(a2) {
    let  that=this
 return {
     title: [
         {
             text: a2+'%',
             textAlign: "center",
             left: "48%",
             top: "30%",
             textStyle: {
                 color: "#67abf2",
                 fontSize: 23,
             },
         },
         {
             text: '时间管理',
             left: "48%",
             top: "80%",
             textAlign: "center",
             textStyle: {
                 color: "#cdd7fa",
                 fontWeight: "normal",
                 fontSize: "16",
                 textAlign: "center",
             },
         },
     ],
     series: [
         {
             type: "pie",
             hoverAnimation: false,
             radius: ["60%", "59%"],
             center: ["50%", "40%"],
             labelLine: {
                 normal: {
                     show: false,
                 },
             },
             label: {
                 normal: {
                     position: "center",
                 },
             },
             data: [
                 {
                     value: 100,
                     itemStyle: {
                         normal: {
                             color: "#255788",
                         },
                     },
                 },
 
             ],
         },
         {
             type: "pie",
             hoverAnimation: false,
             radius: ["53%", "60%"],
             center: ["50%", "40%"],
             labelLine: {
                 normal: {
                     show: false,
                 },
             },
             label: {
                 normal: {
                     position: "center",
                 },
             },
             data: [
                 {
                     value: a2,
                     itemStyle: {
                         normal: {
                             color: "#68a8f2",
                         },
                     },
                     normal: {
                         show: false,
                     },
                 },
                 {
                     value: 100-22,
                     itemStyle:  {
                         normal: {
                             label: {
                                 show: false,
                             },
                             labelLine: {
                                 show: false,
                             },
                             color: "rgba(0,0,0,0)",
                             borderWidth: 0,
                         },
                         emphasis: {
                             color: "rgba(0,0,0,0)",
                             borderWidth: 0,
                         },
                     },
                 },
             ],
         },
     ],
 };
 }

Page({

    /**
     * 页面的初始数据
     */
    data: {
           //查看图片
        picShow:false,
        progressList:[{text:'曲江御井路公租房项目EPC总承包工程',id:132}],
        progressName:'曲江御井路公租房项目EPC总承包工程',
        num:'0',
        startEnd:'',
        ec:{
            lazyLoad:true
        },
        ec1:{
            lazyLoad:true
        },
        tableList:[{name:'整体进度',value:'4#楼 12F层'},{name:'实时进度',value:'4#-12F-YBS7dM'},{name:'预制构件总数',value:'16811'},{name:'施工完成',value:'3130'},{name:'施工验收',value:'0'},{name:'构件吊装',value:'0'},{name:'扫码进场',value:'25'},{name:'未开始',value:'13656'}],
        contentShow:false,
        shigongList:[],
        timeStr:'',
        jinduStr:'',
        video:'',

        show: false,
        projectId:'',
        projectName:'' ,
        loginName:'',
        userName:'',
        initData:{},
        projectData:{}
    },
    showPopup() {
        this.setData({ show: true });
    },
    onClose() {
        this.setData({ show: false });
    },

    // 打开遮罩层
    showPic(e) {
        this.setData({
            picShow:true,
            imageUrl:e.currentTarget.dataset.id
        })
      },
    // 关闭遮罩层
    closePic() {
        this.setData({
            picShow:false,
            imageUrl:''
        
        })
    },
    // 点击单选
    onSelectBtn(e){
        console.log(e.currentTarget.dataset.set);
        const m= this.data.shigongList.map(function (item,i) {
            console.log(item);
          if(e.currentTarget.dataset.set==i){
           item.select= item.select==false?true:false
          }
          return item
       })
   // console.log(e.currentTarget.dataset.set);
   this.setData({
      shigongList:m
   })
    },
    // 点击让模块隐藏
    onClick(e){
        console.log(e.currentTarget.dataset.set);
         const m= this.data.shigongList.map(function (item,i) {
             console.log(item);
           if(e.currentTarget.dataset.set==i){
            item.type= item.type==false?true:false
           }
           return item
        })
    // console.log(e.currentTarget.dataset.set);
    this.setData({
       shigongList:m
    })
    },

    //项目切换 返回值
    onProjectSelect(e) {
        let projectId = e.detail.id;
        let projectName = e.detail.text;
        app.globalData.projectId = projectId;
        app.globalData.projectName = projectName;
        this.setData({
            projectId: projectId,
            projectName: projectName
        })
        this.onLoad()
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this
        //获取项目缓存数据
        wx.getStorage({
            key: 'userinfo',
            success:function(res){
            that.setData({
                loginName:res.data.loginName,
                userName:res.data.userName,
                projectId:app.globalData.projectId,
                projectName:app.globalData.projectName,
                initData:{text:app.globalData.projectName,id:app.globalData.projectId}
            })  
            }
        })
        that.getProjectCorrespondence(app.globalData.projectId);

        this.oneComponent = this.selectComponent('#mychart-dom');
        this.twoComponent = this.selectComponent('#mychart-dom1');

     
    },

      /**
   * 项目对应关系
   */
  getProjectCorrespondence(projectId) {
    var that = this;
    wx.request({
      url: app.globalData.reqUrl + '/weixin/training/getProjectCorrespondence',
      data: {
        "projectId": projectId,
      },
      method: "get",
      success: function (res) {
        that.setData({
          projectData: res.data
        })

        that.gainProgressLog()
        that.gainVideoUrl()
        that.gainBaseInfo()
        
      }
    })
  },
 
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

    gainProgressLog() {
        var that = this;
        //日志
        if(this.data.projectData.szh == '132') {
            wx.request({
            url: app.globalData.szhUrl+'/api/wei/gainWeiListFloorInfo',
            data:{
                "projectId":this.data.projectData.szh
            },
            method:"GET",
            success:function(res){
                that.setData({
                    shigongList:res.data.data
                })
            }
            })
        } else {
            that.setData({
                shigongList:[]
            })
        }
    },

    gainVideoUrl() {
        var that = this;
        if(this.data.projectData.szh == '132') {
             //视频
            wx.request({
            url: app.globalData.szhUrl+'/api/check/gainUrl',
            data:{
                "projectId":this.data.projectData.szh
            },
            method:"GET",
            success:function(res){
                that.setData({
                    video:res.data.data.httpHlss
                })
            }
            
            })
        } else {
            that.setData({
                video:''
            })
        }
        
    },

    getDateDiff(dateTime) {   // 将年月日转换为几小时前，几天前等等
        let dateTimeStamp = new Date(dateTime).getTime();
        let result = '';
        let minute = 1000 * 60;
        let hour = minute * 60;
        let day = hour * 24;
        let halfamonth = day * 15;
        let month = day * 30;
        let year = day * 365;
        let now = new Date().getTime();
        let diffValue = now - dateTimeStamp;
        if (diffValue < 0) {
          return;
        }
        let monthEnd = diffValue / month;
        let weekEnd = diffValue / (7 * day);
        let dayEnd = diffValue / day;
        let hourEnd = diffValue / hour;
        let minEnd = diffValue / minute;
        let yearEnd = diffValue / year;
        if (yearEnd >= 1) {
          result = dateTime;
        } else if (monthEnd >= 1) {
          result = "" + parseInt(monthEnd) + "月前";
        } else if (weekEnd >= 1) {
          result = "" + parseInt(weekEnd) + "周前";
        } else if (dayEnd >= 1) {
          result = "" + parseInt(dayEnd) + "天前";
        } else if (hourEnd >= 1) {
          result = "" + parseInt(hourEnd) + "小时前";
        } else if (minEnd >= 1) {
          result = "" + parseInt(minEnd) + "分钟前";
        } else {
          result = "刚刚";
        }
        return result;
      },

    initChartA(A1) {
        this.oneComponent.init((canvas, width, height, dpr) => {
            chart=echarts.init(canvas,null,{
                width:width,
                height:height,
                devicePixelRatio:dpr
            })
            canvas.setChart(chart)
            let option=getOption(A1)
            chart.setOption(option)
            return chart
        })

    },

    initChartB(A2) {
        this.twoComponent.init((canvas, width, height, dpr) => {
            chart1=echarts.init(canvas,null,{
                width:width,
                height:height,
                devicePixelRatio:dpr
            })
            canvas.setChart(chart1)
            let option1=getOption1(A2)
            chart1.setOption(option1)
            return chart1
        })

    },

    gainBaseInfo() {
        //基本进度百分比
        var that = this;
        wx.request({
        url: app.globalData.szhUrl+'/api/building/proBaseInfo',
        data:{
            "projectId":this.data.projectData.szh
        },
        method:"GET",
        success:function(res){
            that.setData({
                num:res.data.data.contract_monery,
                startEnd:(res.data.data.plan_start_date.split(" "))[0] + " 至 " + (res.data.data.plan_end_date.split(" "))[0],
                jinduStr:(res.data.data.kailei_money / 67640  * 100).toFixed(0),
                timeStr:(that.getDateDiff(res.data.data.actual_start_date) / 720 * 100).toFixed(0) 
            })

            that.initChartA((res.data.data.kailei_money / 67640 * 100).toFixed(0) )
            that.initChartB(((parseInt(new Date() - new Date(res.data.data.actual_start_date)) / (1000 * 60 * 60 * 24)).toFixed(0) / 720 * 100).toFixed(0)  )
        }
        })
        
    },

    goGCLB:function(){
        wx.redirectTo({
            url: '../../pages/gengduogongneng/gengduogongneng'
        })
    },
    
})