// packageE/appointment/ProjectDetails/ProjectDetails.js
const app = getApp();
var location = require("../../../mybehaviors/location/location");

Page({
  behaviors: [location],

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    project: '',
    goods: '',
    store: '',

    // more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    titleInfo: '', // 标题
    commentList: [],
    project_id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      appointmentLang: wx.getStorageSync('yz_basic_info').lang.appointment,
      project_id: options.project_id,
    });
    wx.setNavigationBarTitle({
      title: this.data.appointmentLang.project + '详情'
    });

    if (options.point && options.point != 'undefined') {
      this.setData({
        point: JSON.parse(options.point),
      });
      this.getData();
    } else {
      this._getLocation((res, e) => {
        let point = app.qqMapTransBMap(parseFloat(e.lng),parseFloat(e.lat));
        this.setData({
          lat: point.lat,
          lng: point.lng,
          point: point
        });
        this.getData();
      });
    }

  },


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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var value = wx.getStorageSync('yz_uid');
    var mid = '';
    if (value) {
      mid = value;
    }
    let _path = '/packageE/appointment/ProjectDetails/ProjectDetails?project_id=' + this.data.project_id + '&' + this.data.point + '&mid=' + mid
    return {
      title: this.data.appointmentLang.project + '详情',
      path: _path,
    }
  },
  goGoodsBtn(e) {
    var url = '/packageA/detail_v2/detail_v2?id=' + e.currentTarget.dataset.id;
    wx.navigateTo({
      url: url
    });
  },
  goHome() {
    wx.reLaunch({
      url: '/packageG/index/index',
    });
  },
  goMember() {
    wx.reLaunch({
      url: '/packageG/member_v2/member_v2'
    });
  },
  goBack() {
    let pages = getCurrentPages();
    if (pages.length <= 1) {
      wx.reLaunch({
        url: '/packageG/index/index',
      });
    } else {
      wx.navigateBack({ //返回
        delta: 1
      });
    }
  },
  toRouter(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageC/o2o/HomeSeller/HomeSeller?store_id=' + id,
    });
  },
  swichTabTItem(e) {
    this.setData({
      active: e.detail.index,
    });

    if (this.data.active == 2 && this.data.commentList.length == 0) {
      this.getComment();
    }
  },
  getComment() {
    let urlStr = app.getNetAddresss("plugin.appointment.frontend.comment.get-comment-by-project-id");
    app._getNetWork({
      url: urlStr,
      data: {
        page: this.data.page,
        project_id: this.data.project_id
      },
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          this.setData({
            isLoadMore: true,
            total_page: response.data.last_page,
            commentList: response.data.data
          });
          if (!this.data.total_page) {
            this.setData({
              total_page: 0
            });
          }
        } else {
          wx.showToast({
            title: response.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 获取更多数据
  getMoreData() {
    this.setData({
      isLoadMore: false
    });
    // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let page = this.data.page + 1;
      this.setData({
        page
      });
      let urlStr = app.getNetAddresss('plugin.appointment.frontend.comment.get-comment-by-worker-id');
      app._getNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
          project_id: this.data.project_id
        },
        success: (resdata) => {
          var response = resdata.data;
          if (response.result == 1) {
            var myData = response.data.data.concat(this.data.commentList);
            this.setData({
              commentList: myData
            });
          } else {
            wx.showToast({
              title: response.msg,
              icon: 'none'
            });
            let pages = this.data.page - 1;
            this.setData({
              page: pages,
              isLoadMore: false
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.appointment.frontend.project.get-detail-by-id");
    app._getNetWork({
      url: urlStr,
      data: {
        lng: this.data.point.lng,
        lat: this.data.point.lat,
        project_id: this.data.project_id
      },
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          this.setData({
            project: response.data.project,
            store: response.data.store,
            goods: response.data.goods
          });

        } else {
          wx.showToast({
            title: response.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
});