// pages/technical-management/index.js
import * as echarts from '../../ec-canvas/echarts'; 
const app = getApp()
let chart=null
let chart1=null
let canvas1 = {}
// function initChart(canvas,width,height,dpr) {
//     console.log(canvas)
//     canvas1 = canvas
//     console.log(canvas1)
//     console.log(width)
//     console.log(height)
//     console.log(dpr)
//     chart=echarts.init(canvas,null,{
//         width:width,
//         height:height,
//         devicePixelRatio:dpr
//     })
//     canvas.setChart(chart)

//     let option=getOption(0,0)
//     chart.setOption(option)
//     return chart 
// }

function getOption(rate,number) {
    let  that=this
 return {
     title: [
         {
             text: rate+'%',
             textAlign: "center",
             left: "48%",
             top: "30%",
             textStyle: {
                 color: "#67abf2",
                 fontSize: 23,
             },
         },
         {
             text: '未完成:'+number,
             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: 35,
                     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 initChart1(canvas,width,height,dpr) {
//     chart1=echarts.init(canvas,null,{
//         width:width,
//         height:height,
//         devicePixelRatio:dpr
//     })
//     canvas.setChart(chart1)

//     let option1=getOption1(0,0)
//     chart1.setOption(option1)
//     return chart1
// }

 function getOption1(rate,number) {
    let  that=this
 return {
     title: [
         {
             text: rate+'%',
             textAlign: "center",
             left: "48%",
             top: "30%",
             textStyle: {
                 color: "#67abf2",
                 fontSize: 23,
             },
         },
         {
             text: '未完成: '+number,
             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: 22,
                     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: {
        ec:{
            lazyLoad:true
        },
        ec1:{
            lazyLoad:true
        },
        index1:0,
        fourClarificaiton:{
            "gljd": "2022-08-25",
            "szjd": "2022-09-15",
            "zxfajd": 0,
            "jsjd": 0
        },
         //页面跳转参数
        loginName:'',
        userName:'',
        projectId:'',
        projectName:'' ,
        initData:{},
        loadShow:false,
        //科技创新
        sciencelnnovateDataList:[],
        //四新应用
        videoList:[],
        //方案管理
        timeAxisData: [],
        dangerName:[],
        proposalsNumber:0,
        planNodelNumber:0,
        //变更签认
        constructionNum:0,
        alterationNum:0,
        bgWTGNumber:0,
        gcWTGNumber:0,
        bgWTGNumberRate:0,
        gcWTGNumberRate:0,
       //人员
        show: false,
    },
    // 左键
    onLeftBtn(e){
        let  that=this
       this.setData({
           index1:this.data.index1-1
       })
       let maxLength = 0
       var query = wx.createSelectorQuery();
       query.select('.kejichuangxin-content-box').boundingClientRect();
       query.exec(function (res) {
         //res就是 所有标签为v1的元素的信息 的数组
        //  console.log(res);  
         //取宽度
        //  console.log(res[0].width);
         let n = 0
         if(that.data.index1 > maxLength){
            n = that.data.index1 * res[0].width
         
         }else{
             that.setData({
                 index1:3
             })
             n = that.data.index1 * res[0].width
         }
           console.log(n);
        })
    },
    onRightBtn(e){
       let  that=this
        this.setData({
            index1:this.data.index1+1
       })
       let maxLength = this.data.sciencelnnovateDataList.length ;
       console.log(this.data.sciencelnnovateDataList.length)
       var query = wx.createSelectorQuery();
       query.select('.kejichuangxin-content-box').boundingClientRect();
       query.exec(function (res) {
         //res就是 所有标签为v1的元素的信息 的数组
        //  console.log(res);  
         //取宽度
        //  console.log(res[0].width);
         let n = 0
         if(that.data.index1 < maxLength ){
            n = that.data.index1 * res[0].width
         
         }else{
             that.setData({
                 index1:0
             })
             n = that.data.index1 * res[0].width
         }
        //  console.log(n);
        //  const bigBox=wx.createSelectorQuery().select('#bigBox').boundingClientRect();
        //  bigBox.exec(function (res) {
        //      console.log(res);
        //  })
        //  console.log( bigBox);
        //  that.selectComponent('#bigBox').animate({scrollLeft:n+'px'})
         })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this
        this.oneComponent = this.selectComponent('#mychart-dom');
        this.twoComponent = this.selectComponent('#mychart-dom1');
        //获取项目缓存数据
        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)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

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

    },

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

    },

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

    },
    onClickHide() {
        this.setData({ loadShow: false });
      },
    onClickShow() {
        this.setData({ loadShow: true });
      },

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

    },

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

    },

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

    },
    /**
     * 四级交底情况
     */
    getfourData() {
        var that = this
        wx.request({
            url: 'https://api-tmp.makalu.cc/szhjzgkpt/four/clarificaiton', 
            data:{},
            method:"GET",
            success:(res)=>{
                that.fourClarificaiton = res.data
                wx.request({
                  url: app.globalData.szhUrl+'/api/project/selectTechnicalDisclosureInformationList',
                  data:{
                    projectId: that.data.projectId,
                  },
                  method:'POST',
                  success:(res)=>{
                        var zx = 0
                        var js = 0
                        that.setData({
                            fourDataList : res.data.data
                        })
                    
                        for (let tmp of res.data.data) {
                            switch (tmp.type) {
                                case 1:
                                    js++
                                    tmp.sort = js
                                    break
                                case 2:
                                    zx++
                                    tmp.sort = zx
                                    break
                            }
                        }
                    
                        that.setData({
                            'fourClarificaiton.zxfajd': zx,
                            'fourClarificaiton.jsjd':js
                        })
                  }
                })
            }
        
        })
    },
      //项目切换 返回值
    onProjectSelect(e) {
        var that = this;
        this.onClickShow();
        let projectId = e.detail.id;
        let projectName = e.detail.text;
        app.globalData.projectId = projectId;
        app.globalData.projectName = projectName;
        that.setData({
            projectId: projectId,
            projectName: projectName
        })
       that.onLoad()
    },
      /**
   * 项目对应关系
   */
  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,
          projectId:res.data.szh
        })
        that.getfourData()
        that.scienceInnovateData();
        that.fourNewApplicationData();
        that.getFangAnNumber();
        that.getSchemeTableData();
      }
    })
  },
    //科技创新
    scienceInnovateData(){
        var that = this
        wx.request({
            url: app.globalData.szhUrl+'/system/scienceInnovateConfig/list1', 
            data:{
                pageNum:1,pageSize:9999,projectId:that.data.projectId
            },
            method:"POST",
            success:(res)=>{
            let result = res.data
            let tempData = []
            var sciencelnnovateDataList = []
            let tmp = {}
            if(result.code == 0){
                result.rows.forEach(item=>{
                    var name=''
                    var status = ''
                    if(!tmp[item.name]){
                        tmp[item.name] = {
                            name:item.name,
                            context:[]
                        }
                    }

                    if(item.type == '0'){
                        name = '研究报告'
                    }else if(item.type == '1'){
                        name = '知识产权'
                    }else if(item.type == '2'){
                        name = '论文'
                    }else{
                        name = '工法'
                    }

                    if(item.status =='0'){
                       status = '进行中'
                    }else if(item.status =='1'){
                        status = '已完成'
                    }else if(item.status =='4'){
                       status = '未涉及'
                    }else{
                        status = '未开始'
                    }
                    tmp[item.name] .context.push({
                        name,
                       status
                    })

                })
                for(let k in tmp ){
                    sciencelnnovateDataList.push(tmp[k])
                }
                that.setData({
                    sciencelnnovateDataList : sciencelnnovateDataList
                })
                console.log(sciencelnnovateDataList)
            }
            }
        
        })
    },
     //四新引用公艺视频
     fourNewApplicationData(){
         var that = this
         wx.request({
           url: app.globalData.szhUrl+'/system/fourNewConfig/list1',
           data:{
                pageNum:1,
                pageSize:9999,
                projectId:that.data.projectId
           },
           method:"POST",
            success:(res)=>{
                let result = res.data
                var tempList = []
                if(result.code == 0){
                    tempList = result.rows.filter(item=>{
                        if(item.type == 1){
                            return item
                        }
                    }).map(item=>{
                        return {url:item.fileUrl}
                    });
                    that.setData({
                        videoList:tempList
                    })
                }
            } 
         })
    },
    //方案管理
    getFangAnNumber() {
        var that = this
        let project_id = that.data.projectId
        wx.request({
          url: app.globalData.szhUrl+'/api/danger/queryProjectDanger',
          data:{
            project_id,
            pageNum:1,
            pageSize:9999
          },
          method:"POST",
          success:(res)=>{
           
            if(res.data.code == 200){
                that.setData({
                    dangerName:res.data.data.danger_name,
                })
                let timeAxisData = res.data.data.dangerList
                var index = timeAxisData.length - 1
                for (let i = 0; i < timeAxisData.length; i++) {
                    if (timeAxisData[i].actualTima == '') {
                        index = i - 1
                        break
                    }
                }
                that.setData({
                    timeAxisData:timeAxisData, 
                })
                that.getPlanNodeNumber()
            } else {
                that.setData({
                    dangerName:[],
                    timeAxisData:[],
                    planNodelNumber:0
                
                })
            }
          }
        })
       
    },
    /**
     * 获取方案节点数
     */
    getPlanNodeNumber() {
        var that = this;
        let count = 0;
        var list = this.data.dangerName;
        for (let index = 0; index < list.length; index++) {
            wx.request({
              url: app.globalData.szhUrl+'/api/danger/queryProjectId',
              data:{
                "project_id": that.data.projectId,
                "danger_id": list[index].id
              },
              method:"POST",
              success:(res)=>{
                if(res.data.code == 200){
                    count += parseInt(res.data.allPoint);
                } 
                that.setData({
                    planNodelNumber:count
                })
              }
            })  

        }
    },
    //变更签认
    getSchemeTableData() {
        var that = this;
        wx.request({
          url: app.globalData.szhUrl+'/system/alterationEndorseConfig/list1',
          data:{pageNum:1,pageSize:9999,projectId:this.data.projectId},
          method:"POST",
          success:(res)=>{
            let result = res.data
            var bgWTGNumber= 0;
            var gcWTGNumber = 0
            if(result.code == 0){

                that.setData({
                    constructionNum: result.rows.filter(item=>item.alterationType == "0").length,
                    alterationNum: result.rows.filter(item=>item.alterationType == "1").length
                })
            } else {
                that.setData({
                    constructionNum:0,
                    alterationNum:0
                })
            }
                var tempList =  result.rows
                if(tempList != undefined){
                    for (let index = 0; index < tempList.length; index++) {
                        if(tempList[index].alterationType == "0") {
                             if(tempList[index].alterationStatus != '0') {
                                 bgWTGNumber += 1
                             }
                        } else {
                             if(tempList[index].alterationStatus != '0') {
                                 gcWTGNumber += 1
                             }
                        }
                        var bgWTGNumberRate = 0.0;
                        var gcWTGNumberRate = 0.0;
                        if(that.data.constructionNum != 0) {
                         bgWTGNumberRate =isNaN(((bgWTGNumber/that.data.constructionNum)*100).toFixed(2))?0.0:((bgWTGNumber/that.data.constructionNum)*100).toFixed(1);
                        }
                        if(that.data.alterationNum != 0 && gcWTGNumber != 0) {
                         gcWTGNumberRate = isNaN(((gcWTGNumber/that.data.alterationNum)*100).toFixed(2))?0.0:((gcWTGNumber/that.data.alterationNum)*100).toFixed(1);
                        }
                        that.setData({
                             bgWTGNumber:bgWTGNumber,
                             gcWTGNumber:gcWTGNumber,
                             bgWTGNumberRate:bgWTGNumberRate == 0 ? 0:bgWTGNumberRate,
                             gcWTGNumberRate:gcWTGNumberRate == 0 ? 0:gcWTGNumberRate
                         })
                         
                     }
                } else {
                    that.setData({
                        bgWTGNumber:0,
                        gcWTGNumber:0,
                        bgWTGNumberRate:0,
                        gcWTGNumberRate:0
                    })
                   
                }

                that.initChartA(this.data.bgWTGNumberRate, this.data.bgWTGNumber)
                that.initChartB(this.data.gcWTGNumberRate, this.data.gcWTGNumber)
            
                     
          }
        })
    },
    initChartA(rate,number) {
        this.oneComponent.init((canvas, width, height, dpr) => {
            chart=echarts.init(canvas,null,{
                width:width,
                height:height,
                devicePixelRatio:dpr
            })
            canvas.setChart(chart)
            let option=getOption(rate,number)
            chart.setOption(option)
            return chart
        })

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

    },
    /**
   * 返回到更多功能页面
   */
  goGCLB:function(){
    wx.redirectTo({
      url: '../../pages/gengduogongneng/gengduogongneng'
    })
  },
    
  showPopup() {
    this.setData({ show: true }); 
  },
  onClose() {
    this.setData({ show: false });
  },

  checkMajorNodes(){
      wx.navigateTo({
        url: '../dangerousProject/index',
      })
  },

  checkSignature(){
    wx.navigateTo({
      url: '../biangeng/index',
    })
  }
   
})