const {
  IMG_BASE_URL,
  getAddressLonLat,
  getQualityRectificationList
} = require("../../../api/api");
const app = getApp()
const { routeLink } = require("../../../utils/route.js");
const {
  checkPermission
} = require("../../../utils/permission");
Page({
  data: {
    IMG_BASE_URL: IMG_BASE_URL,
    statusBarHeight: app.globalData.statusBarHeight, //状态栏高度
    navHeight: app.globalData.navHeight, //导航栏高度
    keyWords: '', //关键字
    searchData: [],
    pageNum: 1, //分页页面
    pageSize: 10, //分页条数
    loadEnd: false, //加载到最底部
  },
  /** 搜索 **/
  keyWordsSearch(res) {
    this.setData({
      searchData: [],
    })
    const search = res.detail; //搜索内容 
    this.setData({
      searchData: this.data.searchData.concat(search),
      pageNum:1
    })
    wx.pageScrollTo({
      scrollTop: 0,
    })
    this.getList()
  }, 
  /** 获取电站列表 **/
  getList(e) {
    wx.showLoading({
      title: '加载中',
    })
    getQualityRectificationList({
      userId: wx.getStorageSync('userId'),
      smallCondition: this.data.searchData.length > 0 ? this.data.searchData + "" : "",
    }).then((res) => {
      wx.hideLoading()
      wx.stopPullDownRefresh()
      if (res.code == 200) {
        this.data.loadEnd = res.data.length < this.data.pageSize
        if (this.data.pageNum == 1) {
          this.data.rows = res.data
        } else {
          this.data.rows = this.data.rows.concat(res.data)
        }
        if (!this.data.loadEnd) {
          this.data.pageNum++;
        }
        this.setData({
          list: this.data.rows,
          loadEnd: this.data.loadEnd,
        })
      } else {
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      }
    })
  },
  /** 页面触底上拉刷新 **/
  onReachBottom(e) {
    if (!this.data.loadEnd) {
      this.getList()      
    } else {
      wx.showToast({
        title: '暂无更多数据',
        icon: 'none',
        duration: 1500
      })
    }
  },
  /** 下拉刷新 **/
  onPullDownRefresh(e) {
    this.setData({
      pageNum: 1,
      pageSize: 10,
      loadEnd: false,
    })   
    this.getList() 
  },
  onLoad(options) {
    console.log("options",options);
    this.setData({
      type: options.type,
      userId: wx.getStorageSync('userId'),
      userType: wx.getStorageSync('userType'),
      traderId:wx.getStorageSync('traderId') ? wx.getStorageSync('traderId') : '-1',//代理商登录的traderId
      PTpermission: wx.getStorageSync('PTpermission'),   
    })
  },
  onShow() {
    this.setData({
      pageNum:1,
    });   
    this.getList() 
  },
  /** 地址导航 **/
  addrFn(e){
    let obj = e.currentTarget.dataset.obj;
    wx.showLoading({
      title: '加载中',
    });
    getAddressLonLat({
      address:obj.address
    }).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.address,
          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'
        })
      }
    })
  },
  /** 派工-按钮 **/
  async bindDispatch(e){
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    routeLink("/other/pages/dispatch/dispatch?projectId=" + e.currentTarget.dataset.projectid + "&type=31");
  },
  /** 整改-按钮 **/
  async bindRectify(e){   
    routeLink("/other/pages/rectifyForm/rectifyForm?projectId=" + e.currentTarget.dataset.projectid + "&entryId=" + e.currentTarget.dataset.entryid);
  },
  /** 验收-按钮 **/
  async bindAccept(e){
    routeLink("/other/pages/rectifyForm/rectifyForm?projectId=" + e.currentTarget.dataset.projectid + "&entryId=" + e.currentTarget.dataset.entryid);
  },
  /** 详情 **/
  detailFn(e){
    routeLink("/other/pages/rectifyForm/rectifyForm?projectId=" + e.currentTarget.dataset.projectid + "&entryId=" + e.currentTarget.dataset.entryid + "&title=详情");
  },  
})