// pages/infor/infor.js
const app = getApp()
var sever = require("../../api.js").sever;
console.log(sever)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nikename:'',
    avatar:'',
    pic_url:'',
    showpic:false,
    next_page_url:'',
    status:'',
    keyword:'',
    send:0,
    examine:0,
    sending:0,
    comment:0,
    selected:0,
    infor:[
      { vorderid: '12345213768779', state: '待审核', vordertime: '2018-07-27 23:23:23', cate: '床头柜', model: 'A1254', problem: '掉漆' },
      { vorderid: '12345213482166', state: '待审核', vordertime: '2018-07-27 23:23:23', cate: '床板', model: 'B1214', problem: '边角容易翘起' },
      { vorderid: '12345213742189', state: '待审核', vordertime: '2018-07-27 23:23:23', cate: '餐桌', model: 'C1264', problem: '外漆容易烫坏' },
      { vorderid: '12345213054529', state: '待审核', vordertime: '2018-07-27 23:23:23', cate: '茶几', model: 'D2154', problem: '不结实' },
      { vorderid: '12345213397579', state: '待审核', vordertime: '2018-07-27 23:23:23', cate: '书架', model: 'E9746', problem: '这种渣渣渣渣渣渣渣渣渣渣渣渣渣渣渣渣渣渣' }
    ],
    userInfo: {},
    hasUserInfo: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.setData({
      nikename:wx.getStorageSync('user').username,
      avatar: wx.getStorageSync('user').avatar
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    if(wx.getStorageSync('us')){
      that.setData({
        selected: wx.getStorageSync('us')
      })
    }
    var s = ''
    if(that.data.selected == 0){
      s = ''
    }
    if (that.data.selected == 1) {
      s = '0'
    }
    if (that.data.selected == 2) {
      s = '8'
    }
    if (that.data.selected == 3) {
      s = '4'
    }
    if (that.data.selected == 4) {
      s = '5'
    }
    console.log(s)
    app.request({
      url: sever+'getorder',
      method: 'get',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        user_id: wx.getStorageSync('user').id,
        status:s
      },
      success: function(res) {
        console.log(res.data)
        that.setData({
        next_page_url:res.data.data.order.next_page_url,
          status:''
        })
        var obj = res.data.data.order.data
        var gArr = []
        //console.log(obj)
        for (var i = 0; i < obj.length; i++) {
          var stateArr = ["待审核", "待生产", "生产中", "待发货", "未签收", "待评价", "已评价", "已取消"]
          var gObj = {}
          gObj.vorderid = obj[i].sno
          //console.log(obj[i].status)
          gObj.state = stateArr[obj[i].status]
          gObj.vordertime = obj[i].created_at
          gObj.cate = obj[i].category
          gObj.model = obj[i].type
          gObj.problem = obj[i].question
          gObj.qrcode = obj[i].qrcode
          gObj.expected_time = obj[i].expected_time
          gObj.detail_order_id = obj[i].id
          gArr.push(gObj)
        }
        that.setData({
          infor: [...gArr]
        })
        console.log(that.data.infor)
      }
    })
    app.request({
      url: sever+'getprogress',
      data:{
        store_id:wx.getStorageSync('user').store_id,
        user_id:wx.getStorageSync('user').id
      },
      method:'get',
      success:function(res){
        var c = res.data.data.count
        var k = 0
        for(var i = 0;i < c.length;i++){
          if(c[i].status == 0){
            that.setData({
              examine:c[i].total
            })
          }
          if (c[i].status == 4) {
            that.setData({
              sending: c[i].total
            })
          }
          if (c[i].status == 5) {
            that.setData({
              comment: c[i].total
            })
          }
          if (c[i].status == 1) {
            k = k + c[i].total
          }
          if (c[i].status == 2) {
            k = k + c[i].total
          }
          if (c[i].status == 3) {
            k = k + c[i].total
          }
        }
        that.setData({
          send:k
        })
      }
    })
  },
  backhome: function () {
    wx.reLaunch({
      url: '/page/homePage/homePage',
    })
  },
  allOrder:function(){
    wx.setStorageSync('us', 0)
    var that = this
    that.setData({
      selected:0,
      status:''
    })
    app.request({
      url: sever+'getorder',
      method: 'get',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        user_id: wx.getStorageSync('user').id
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          next_page_url: res.data.data.order.next_page_url
        })
        var obj = res.data.data.order.data
        var gArr = []
        console.log(obj)
        for (var i = 0; i < obj.length; i++) {
          var stateArr = ["待审核", "待生产", "生产中", "待发货", "未签收", "待评价", "已评价", "已取消"]
          var gObj = {}
          gObj.vorderid = obj[i].sno
          //console.log(obj[i].status)
          gObj.state = stateArr[obj[i].status]
          gObj.vordertime = obj[i].created_at
          gObj.cate = obj[i].category
          gObj.model = obj[i].type
          gObj.problem = obj[i].question
          gObj.qrcode = obj[i].qrcode
          gObj.expected_time = obj[i].expected_time
          gObj.detail_order_id = obj[i].id
          gArr.push(gObj)
        }
        that.setData({
          infor: [...gArr]
        })
        console.log(that.data.infor)
      }
    })
  },
  TobeAudited:function(){
    wx.setStorageSync('us', 1)
    var that = this
    that.setData({
      selected: 1,
      status:'0'
    })
    app.request({
      url: sever+'getorder',
      method: 'get',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        user_id: wx.getStorageSync('user').id,
        status:'0'
      },
      success: function (res) {
        console.log(res.data.data.order.data)
        that.setData({
          next_page_url: res.data.data.order.next_page_url
        })
        var obj = res.data.data.order.data
        var gArr = []
        //console.log(obj)
        for (var i = 0; i < obj.length; i++) {
          var stateArr = ["待审核", "待生产", "生产中", "待发货", "未签收", "待评价", "已评价", "已取消"]
          var gObj = {}
          gObj.vorderid = obj[i].sno
          //console.log(obj[i].status)
          gObj.state = stateArr[obj[i].status]
          gObj.vordertime = obj[i].created_at
          gObj.cate = obj[i].category
          gObj.model = obj[i].type
          gObj.problem = obj[i].question
          gObj.qrcode = obj[i].qrcode
          gObj.expected_time = obj[i].expected_time
          gObj.detail_order_id = obj[i].id
          if(gObj.state == '待审核'){
            gArr.push(gObj)
          }
        }
        that.setData({
          infor: [...gArr]
        })
        console.log(that.data.infor)
      }
    })
  },
  TobeSend:function(){
    wx.setStorageSync('us', 2)
    var that = this
    that.setData({
      selected: 2,
      status: '8',
    })
    app.request({
      url: sever+'getorder',
      method: 'get',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        user_id: wx.getStorageSync('user').id,
        status: '8'
      },
      success: function (res) {
        console.log(res.data.data.order.data)
        that.setData({
          next_page_url: res.data.data.order.next_page_url
        })
        var obj = res.data.data.order.data
        var gArr = []
        //console.log(obj)
        for (var i = 0; i < obj.length; i++) {
          var stateArr = ["待审核", "待生产", "生产中", "待发货", "未签收", "待评价", "已评价", "已取消"]
          var gObj = {}
          gObj.vorderid = obj[i].sno
          //console.log(obj[i].status)
          gObj.state = stateArr[obj[i].status]
          gObj.vordertime = obj[i].created_at
          gObj.cate = obj[i].category
          gObj.model = obj[i].type
          gObj.problem = obj[i].question
          gObj.qrcode = obj[i].qrcode
          gObj.expected_time = obj[i].expected_time
          gObj.detail_order_id = obj[i].id
          console.log(gObj)
          gArr.push(gObj)
        }
        that.setData({
          infor: [...gArr]
        })
        console.log(that.data.infor)
      }
    })
  },
  TobeReceived:function(){
    wx.setStorageSync('us', 3)
    var that = this
    that.setData({
      selected: 3,
      status:'4',
    })
    app.request({
      url: sever+'getorder',
      method: 'get',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        user_id: wx.getStorageSync('user').id,
        status:'4'
      },
      success: function (res) {
        console.log(res.data.data.order.data)
        that.setData({
          next_page_url: res.data.data.order.next_page_url
        })
        var obj = res.data.data.order.data
        var gArr = []
        //console.log(obj)
        for (var i = 0; i < obj.length; i++) {
          var stateArr = ["待审核", "待生产", "生产中", "待发货", "未签收", "待评价", "已评价", "已取消"]
          var gObj = {}
          gObj.vorderid = obj[i].sno
          //console.log(obj[i].status)
          gObj.state = stateArr[obj[i].status]
          gObj.vordertime = obj[i].created_at
          gObj.cate = obj[i].category
          gObj.model = obj[i].type
          gObj.problem = obj[i].question
          gObj.qrcode = obj[i].qrcode
          gObj.expected_time = obj[i].expected_time
          gObj.detail_order_id = obj[i].id
          console.log(gObj)
          gArr.push(gObj)
        }
        that.setData({
          infor: [...gArr]
        })
        console.log(that.data.infor)
      }
    })
  },
  TobeEvaluated:function(){
    wx.setStorageSync('us', 4)
    var that = this
    that.setData({
      selected: 4,
      status:'5',
    })
    app.request({
      url: sever+'getorder',
      method: 'get',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        user_id: wx.getStorageSync('user').id,
        status:'5'
      },
      success: function (res) {
        console.log(res.data.data.order.data)
        that.setData({
          next_page_url: res.data.data.order.next_page_url
        })
        var obj = res.data.data.order.data
        var gArr = []
        //console.log(obj)
        for (var i = 0; i < obj.length; i++) {
          var stateArr = ["待审核", "待生产", "生产中", "待发货", "未签收", "待评价", "已评价", "已取消"]
          var gObj = {}
          gObj.vorderid = obj[i].sno
          //console.log(obj[i].status)
          gObj.state = stateArr[obj[i].status]
          gObj.vordertime = obj[i].created_at
          gObj.cate = obj[i].category
          gObj.model = obj[i].type
          gObj.problem = obj[i].question
          gObj.qrcode = obj[i].qrcode
          gObj.expected_time = obj[i].expected_time
          gObj.detail_order_id = obj[i].id
          gArr.push(gObj)
        }
        that.setData({
          infor: [...gArr]
        })
        console.log(that.data.infor)
      }
    })
  },
  detail: function(e) {
    console.log(e.target.id)
    wx.setStorageSync("detail_order_id", e.target.id);
    wx.navigateTo({
      url: 'details',
    })
  },
  showPic:function(event){
    // var that = this
    // that.setData({
    //   pic_url: e.target.id,
    //   showpic:true
    // })
    var src = event.currentTarget.dataset.src;//获取data-src
    var imgList = [src];//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  hidepic:function(){
    var that = this
    that.setData({
      showpic:false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showToast({
      title: '正在刷新',
      icon:'loading',
      duration:500
    })
    wx.reLaunch({
      url: 'infor',
    })
  },

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

  },
  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function () {
    var that = this
    console.log(that.data.next_page_url)
    if (that.data.next_page_url) {
      wx.showLoading({
        title: 'loading',
      })

      if(that.data.status != '10'){
        app.request({
          url: that.data.next_page_url,
          data: {
            store_id: wx.getStorageSync('user').store_id,
            user_id: wx.getStorageSync('user').id,
            status: that.data.status
          },
          method: 'get',
          success: function (res) {
            console.log(res.data)
            that.setData({
              next_page_url: res.data.data.order.next_page_url,
            })
            var obj = res.data.data.order.data
            var gArr = []
            //console.log(obj)
            for (var i = 0; i < obj.length; i++) {
              var stateArr = ["待审核", "待生产", "生产中", "待发货", "未签收", "待评价", "已评价", "已取消"]
              var gObj = {}
              gObj.vorderid = obj[i].sno
              //console.log(obj[i].status)
              gObj.state = stateArr[obj[i].status]
              gObj.vordertime = obj[i].created_at
              gObj.cate = obj[i].category
              gObj.model = obj[i].type
              gObj.problem = obj[i].question
              gObj.expected_time = obj[i].expected_time
              gObj.detail_order_id = obj[i].id
              if (that.data.selected == 2) {
                if (gObj.state == '未签收' || gObj.state == '待生产' || gObj.state == '生产中' || gObj.state == '待发货') {
                  gArr.push(gObj)
                }
              } else {
                gArr.push(gObj)
              }

            }
            that.setData({
              infor: [...that.data.infor.concat(gArr)]
            })
            console.log(that.data.infor)
            wx.hideLoading()
          }
        })
      }else{
        app.request({
          url: that.data.next_page_url,
          data: {
            store_id: wx.getStorageSync('user').store_id,
            keyword: that.data.keyword
          },
          method: 'get',
          success: function (res) {
            that.setData({
              next_page_url: res.data.data.next_page_url
            })
            var obj = res.data.data.data
            var gArr = []
            //console.log(obj)
            for (var i = 0; i < obj.length; i++) {
              var stateArr = ["待审核", "待生产", "生产中", "待发货", "未签收", "待评价", "已评价", "已取消"]
              var gObj = {}
              gObj.vorderid = obj[i].sno
              //console.log(obj[i].status)
              gObj.state = stateArr[obj[i].status]
              gObj.vordertime = obj[i].created_at
              gObj.cate = obj[i].category
              gObj.model = obj[i].type
              gObj.problem = obj[i].question
              gObj.qrcode = obj[i].qrcode
              gObj.expected_time = obj[i].expected_time
              gObj.detail_order_id = obj[i].id
              gArr.push(gObj)
            }
            that.setData({
              infor: [...that.data.infor.concat(gArr)]
            })
            console.log(that.data.infor)
            wx.hideLoading()
          }
        })
      }
    } else {
      // wx.showToast({
      //   title: '已经没有更多了',
      //   icon: 'none',
      //   duration: 1000
      // })
    }
  },
  searchin:function(e){
    var that = this
    that.setData({
      keyword : e.detail.value,
    })
  },
  search:function(){
    var that = this
    that.setData({
      status:'10',
      selected: 0,
    })
    app.request({
      url:sever+'searchlist',
      data:{
        store_id:wx.getStorageSync('user').store_id,
        keyword:that.data.keyword
      },
      method:'get',
      success:function(res){
        wx.showToast({
          title: '共搜索到'+res.data.data.total+'条结果',
          icon:"none",
          duration:1000
        })
        that.setData({
          next_page_url: res.data.data.next_page_url
        })
        var obj = res.data.data.data
        var gArr = []
        //console.log(obj)
        for (var i = 0; i < obj.length; i++) {
          var stateArr = ["待审核", "待生产", "生产中", "待发货", "未签收", "待评价", "已评价", "已取消"]
          var gObj = {}
          gObj.vorderid = obj[i].sno
          //console.log(obj[i].status)
          gObj.state = stateArr[obj[i].status]
          gObj.vordertime = obj[i].created_at
          gObj.cate = obj[i].category
          gObj.model = obj[i].type
          gObj.problem = obj[i].question
          gObj.qrcode = obj[i].qrcode
          gObj.expected_time = obj[i].expected_time
          gObj.detail_order_id = obj[i].id
          gArr.push(gObj)
        }
        that.setData({
          infor: [...gArr]
        })
        console.log(that.data.infor)
      }
    })
  },
  quxiao: function (e) {
    app.request({
      url: sever+'updatestatus',
      method: 'post',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        order_id: e.target.id,
        status: 7,
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.data.ok == true) {
          wx.showToast({
            title: '取消成功',
            icon: 'success',
            duration: 1000,
            success: function () {
              wx.reLaunch({
                url: 'infor',
              })
            }
          })
        } else {
          wx.showToast({
            title: '取消失败',
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '取消失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },
  shouhuo: function (e) {
    app.request({
      url: sever+'updatestatus',
      method: 'post',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        order_id: e.target.id,
        status: 5,
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.data.ok == true) {
          wx.showToast({
            title: '收货成功',
            icon: 'success',
            duration: 1000,
            success: function () {
              wx.reLaunch({
                url: 'infor',
              })
            }
          })
        } else {
          wx.showToast({
            title: '收货失败',
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '收货失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },
  pingjia:function(e){
    console.log(e.target.id)
    wx.setStorageSync("detail_order_id", e.target.id);
    wx.navigateTo({
      url: 'assess',
    })
  },
  upFormID: function (e) {
    console.log('....')
    app.request({
      url: sever+'saveformid',
      method: 'post',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        openid: wx.getStorageSync('user').wechat_open_id,
        formid: e.detail.formId
      },
      success: (res) => {
        console.log(res.data.data.msg)
      }
    })
  },
  goIndex:function(){
    wx.redirectTo({
      url: '../index/index',
    })
  }
})