// pages/stock/stock.js
const $api = require('../../utils/api.js').API;
const $push = require('../../utils/api.js').PUSH;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    id:'',
    stock_date:'',
    type:1,
    page:1,
    total:0,
    cur:1,
    text:'我要解读',
    comment:'',
    list:[],
    page1:1,
    total1:0,
    myList:[],
    searchList:[],
    nbComments:[],
    todaynb:0,
    role:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('112',options)
    this.setData({
      type:options.type,
      stock_date:options.stock_date
    })

    wx.getStorage({
      key: 'user',
      complete: res => {
        this.setData({
          user_id: res.data.id,
          role: res.data.role,
        })
      }
    })

    if (options.id){
      //详情
      this.setData({
        id:options.id,
        list:[],
        page1:1,
        total1:0
      })
      this.getComment()
      this.getMylist()
      $api.getStock({ id: options.id, stock_date: this.data.stock_date }).then(res => {
        this.setData({
          info: res.data.data
        })
        wx.setNavigationBarTitle({
          title: res.data.data.name,
        })
      })
      $api.getStockNbcomments({stock_id:options.id,date:this.data.stock_date}).then(res=>{
        this.setData({
          nbComments:res.data.data.list
        })
      })
      this.getPlarer()
    }else{
      this.getData()
      $api.getNbcomments({date:this.data.stock_date}).then(res=>{
        this.setData({
          todaynb:res.data.data.total
        })
      })
    } 
  },
  //获取今日持仓
  getPlarer(){
    var list = this.data.list
    $api.getStockPlayer({ id: this.data.id, stock_date: this.data.stock_date,page:this.data.page1 }).then(res=>{
      wx.hideNavigationBarLoading()
      res.data.data.list.forEach(item => {
        list.push(item)
      })
      this.setData({
        list: list,
        total1: res.data.data.total
      })
    })
  },
  next1: function () {
    if (this.data.cur==1 && this.data.list.length < this.data.total1) {
      const page = this.data.page1 + 1
      this.setData({
        page1: page
      })
      this.getPlarer()
    }
  },
  bindDateChange(e){
    this.setData({
      stock_date:e.detail.value,
      list:[]
    })
    wx.showNavigationBarLoading()
    this.getData()
  },
  toSearch(){

    wx.navigateTo({
      url: '../stock/stock?type=2&stock_date=' + this.data.stock_date,
    })
    
  },
  getMylist(){
    $api.getChoice({ stock_id: this.data.id }).then(res => {
      this.setData({
        myList: res.data.data.list
      })
    })
  },
  delmsg(e){
    var data={
      id:e.target.dataset.id
    }
    $api.delcomment(data).then(res=>{
      this.getComment()
    })
  },
  inputchange1(e) {
    this.setData({
      comment: e.detail.value
    })
  },
  blur(){
    this.setData({
      comment:'',
      pid:null,
      text:'我要解读'
    })
  },
  sendmsg(){
    var data={
      stock_id:this.data.id,
      cotent:this.data.comment,
      pid:this.data.pid
    }
    if(!data.cotent){
        return
    }
    
    $api.postcomment(data).then(res=>{
      this.getComment()
      this.setData({
        comment:'',
        pid:null,
        text:'我要解读'
      })
    })
  },
  choice(){
    $api.addchoice({stock_id:this.data.id}).then(res=>{
      wx.showToast({
        title: '加入成功',
      })
      var info=this.data.info;
      info.choiced=1
      this.setData({
        info: info
      })
      this.getMylist()
    })
  },
  delchoice(){
    $api.deletechoice({stock_id:this.data.id}).then(res=>{
      wx.showToast({
        title: '取消成功',
      })
      var info=this.data.info;
      info.choiced=0
      this.setData({
        info: info
      })
      this.getMylist()
    })
  },
  tab(e){
      this.setData({
          cur:e.target.dataset.id
      })
  },
  getComment(){
    $api.getComment({stock_id:this.data.id}).then(res=>{
      if(res.data.code == 0){
          this.setData({
            comment_list:res.data.data.list,
            comment_count:res.data.data.total
          })
      }
    })
  },
  answer(e){
      this.setData({
          focus:true,
          text:'回复'+e.target.dataset.name,
          pid:e.target.dataset.id
      })
  },
  inputChange(e) {
    this.setData({
      name: e.detail.value
    })
    this.getData('search')
  },
  search() {
    this.setData({
      total: 0,
      list:[],
      page:1
    })
    this.getData()
  },
  next: function () {
    if (this.data.list.length < this.data.total) {
      const page = this.data.page + 1
      this.setData({
        page: page
      })
      this.getData()
    }
  },
  getData(type){
    wx.showNavigationBarLoading()
    var list = this.data.list;
    var data={}
    if(type == 'search'){
      data={ name: this.data.name, stock_date: this.data.stock_date}
      this.setData({
        searchList:[]
      })
    }else{
      data={ name: this.data.name, stock_date: this.data.stock_date, page: this.data.page, page_size: 20}
    }
      $api.getHotbuyList(data).then(res => {
        res.data.data.list.forEach(item => {
          list.push(item)
        })
        wx.hideNavigationBarLoading()
        if(type == 'search'){
          this.setData({
            searchList: res.data.data.list
          })
        }else{
          this.setData({
            list: list,
            total: res.data.data.total
          })
        }
        
      })
      
    
  },
  pushToday(e){
    $push.pushToday(e.currentTarget.dataset)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})