// other/pages/qualityAcceptList/qualityAcceptList.js
const {
  getAddressLonLat, 
  qualityDetailData,
  addSample,
  cancelAccept,listSorting,getAddressByLonLat
} = require("../../../api/api");
const app = getApp()
const { routeLink } = require("../../../utils/route.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 1,
    detail: {},
    cjEntry: [],//检验电站
    wcjEntry: [], //电站列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id:options.projectId
    });     
  },  

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getDetail()
  },
  //详情
  getDetail(){
    wx.showLoading({
      title: "加载中"
    });
    qualityDetailData({     
      id: this.data.id     
    }).then(res=>{     
      if(res.code == 200){
        this.setData({
          detail: res.data.detailRecord.columns,
          cjEntry: res.data.cjEntry.map(itm=>itm.columns),
          wcjEntry: res.data.wcjEntry.map(itm=>itm.columns),
        })  
      }
      wx.hideLoading();
    });
  },
  
  //切换TAB
  changeTab(e){    
    let type = e.currentTarget.dataset.index
    this.setData({
      tabIndex: type
    })
  },
  /** 打开导航 **/
  addrFn(e){
    let obj = e.currentTarget.dataset.obj;
    wx.showLoading({
      title: '加载中',
    });
    getAddressLonLat({
      address:obj
    }).then(res=>{
      wx.hideLoading()
      if (res.code == 200 && res.data) {
        var data = res.data.split(",")
        wx.openLocation({
          latitude:Number(data[1]),
          longitude:Number(data[0]),
          name:obj,
          scale: 18,
        });
      } else {
        wx.showToast({
          title: '获取经纬度失败',
          icon: 'none'
        })
      }
    });
  },
  /** 复制 **/
  copy(e) {
    const code = e.currentTarget.dataset.code;
    wx.setClipboardData({
      data: code,
      success(res) {
        wx.showToast({
          title: '已复制',
          icon: 'none'
        })
      }
    })
  },
  //验收
  bindAccept(e){
    routeLink("/other/pages/qualityAccept/qualityAccept?entryId=" + e.currentTarget.dataset.eid+ '&id='+ e.currentTarget.dataset.id);
  },
  //无法验收
  bindNoAccept(e){
    routeLink("/other/pages/unableAccept/unableAccept?id=" + e.currentTarget.dataset.id + '&type='+ e.currentTarget.dataset.type);
  },
  //添加抽检
  addSampleFn(e){
    wx.showLoading({
      title: "加载中",
      mask: true
    });
    addSample({
      id: this.data.id, //质检单id   
      detailId: e.currentTarget.dataset.id, //质检单明细id
    }).then(res=>{      
      if(res.code == 200){
       wx.showToast({
         title: '添加成功',
         duration: 1000
       })
       setTimeout(()=>{
        this.getDetail()
       },1000)
      }
      wx.hideLoading();
    });
  },
  //撤回抽检
  bindCancelAccept(e){
    wx.showModal({
      title: '提示',
      content: '是否撤回这条质检单？',
      complete: (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          cancelAccept({
            detailId: e.currentTarget.dataset.id, //质检单明细id
          }).then((res) => {
            if (res.code == 200) {
              wx.showToast({
                title: '操作成功',
                icon: 'none'
              })
              this.getDetail()
            } else {
              wx.showToast({
                title: res.errMsg,
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },

  //定位排序
  bindPosition(e) {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      isHighAccuracy: true,
      success(res) {
        console.log("获取经纬", res.longitude.toFixed(6), res.latitude.toFixed(6))
        wx.showLoading({
          title: '获取定位中',
        })        
        if(res.longitude && res.latitude){
          wx.hideLoading()
          that.getAddress(res.longitude.toFixed(6), res.latitude.toFixed(6))
          wx.showLoading({
            title: "加载中"
          });
        }
      },
      fail(res) {
        wx.showModal({
          title: '提示',
          content: '请授权当前小程序定位权限',
          cancelText: "取消",
          confirmText: "确定",
          success(res) {
            if (res.confirm) {
              wx.openSetting({
                withSubscriptions: true,
              })
            }
          }
        })
      }
    })
  },
  //根据经纬度获取地址
  getAddress: function(longitude, latitude) {
    let that = this
    getAddressByLonLat({
      lonLat: longitude+","+latitude  
    }).then((res) => {
      wx.hideLoading()
      if (res.code == 200) {
        wx.showModal({
          title: '当前位置',
          content: res.data,
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              that.positionSort(longitude, latitude)
            }
          }
        })
        that.setData({
          currentAddress: res.data,         
        })        
      } else {
        wx.showToast({
          title: '地址获取失败',
          icon: 'none'
        })
      }
    })
  },
  //重新排序
  positionSort(longitude, latitude) {
    var that = this
    wx.showLoading({
      title: "排序中"
    });
    listSorting({     
      id: that.data.id,
      longitude: longitude,
      latitude: latitude,
    }).then(ress=>{     
      if(ress.code == 200){
        that.setData({
          detail: ress.data.detailRecord.columns,
          cjEntry: ress.data.cjEntry.map(itm=>itm.columns),
          wcjEntry: ress.data.wcjEntry.map(itm=>itm.columns),
        })
      }else{
        wx.showToast({
          title: res.errMsg,
          icon: 'none',
          duration: 1500
        })
      }
      wx.hideLoading();
    });   
  },
})