// pages/packing/packing.js
const Dialog = require('../../bower_components/zanui-weapp/dist/dialog/dialog');
const app = getApp()
var sever = require("../../api.js").sever;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    o_id:'',
    hiddenmodalput: true,
    keyword: '',
    pic_url: '',
    showpic: false,
    send: 0,
    sending: 0,
    comment: 0,
    expected: 0,
    selected: 0,
    next_page_url: '',
    status: '9',
    expecte: '',
    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: '这种渣渣渣渣渣渣渣渣渣渣渣渣渣渣渣渣渣渣'
      }
    ],
    parts: [{
        name: '2号螺丝',
        num: 2
      },
      {
        name: '4号螺丝',
        num: 4
      }
    ],
    userInfo: {},
    hasUserInfo: false,
    peisong: [{
      text: '顺丰快递',
      color: 'red',
      type: '顺丰快递'
    }, {
      text: '韵达快递',
      color: '#3CC51F',
      type: '韵达快递'
    }, {
      text: '申通快递',
      type: '申通快递'
    }, {
      text: '中通快递',
      color: 'red',
      type: '中通快递'
    }, {
      text: '厂家配送',
      type: '厂家配送'
    }],
    fahuo: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    if (wx.getStorageSync('pa')) {
      that.setData({
        selected: wx.getStorageSync('pa')
      })
    }
    var s = '9'
    var e = ''
    if (that.data.selected == 0) {
      s = '9'
      e = ''
      that.setData({
        selected: 0,
        status: '9',
        expecte: ''
      })
    }
    if (that.data.selected == 1) {
      s = '3'
      e = ''
      that.setData({
        selected: 1,
        status: '3',
        expecte: ''
      })
    }
    if (that.data.selected == 2) {
      s = '4'
      e = ''
      that.setData({
        selected: 2,
        status: '4',
        expecte: ''
      })
    }
    if (that.data.selected == 3) {
      s = '5'
      e = ''
      that.setData({
        selected: 3,
        status: '5',
        expecte: ''
      })
    }
    if (that.data.selected == 4) {
      s = ''
      e = '1'
      that.setData({
        selected: 4,
        status: '',
        expecte: '1'
      })
    }
    app.request({
      url: sever+'getorder',
      method: 'get',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        status: s,
        expected: e
      },
      success: function(res) {
        that.setData({
          next_page_url: res.data.data.next_page_url
        })
        console.log(res.data.data.data)
        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
          if (gObj.state == '待发货' || gObj.state == '运输中' || gObj.state == '已签收' || gObj.state == '逾期') {
            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
      },
      method: 'get',
      success: function(res) {
        var c = res.data.data.count
        for (var i = 0; i < c.length; i++) {
          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 == 3) {
            that.setData({
              send: c[i].total
            })
          }
          if (c[i].expected == 1) {
            that.setData({
              expected: c[i].total
            })
          }
        }

      }
    })
    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
          })
        }
      })
    }

  },
  tap1: function() {
    wx.setStorageSync('pa', 0)
    this.setData({
      selected: 0,
      status: '9',
      expecte: ''
    })
    var that = this
    app.request({
      url: sever+'getorder',
      method: 'get',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        status: '9'
      },
      success: function(res) {
        that.setData({
          next_page_url: res.data.data.next_page_url
        })
        console.log(res.data.data.data)
        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
          if (gObj.state == '待发货' || gObj.state == '运输中' || gObj.state == '已签收' || gObj.state == '逾期') {
            gArr.push(gObj)
          }
        }
        that.setData({
          infor: [...gArr]
        })
        console.log(that.data.infor)
      }
    })
  },
  tap2: function() {
    wx.setStorageSync('pa', 1)
    this.setData({
      selected: 1,
      status: '3',
      expecte: ''
    })
    var that = this
    app.request({
      url: sever+'getorder',
      method: 'get',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        status: '3'
      },
      success: function(res) {
        that.setData({
          next_page_url: res.data.data.next_page_url
        })
        console.log(res.data.data.data)
        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)
      }
    })
  },
  tap3: function() {
    wx.setStorageSync('pa', 2)
    this.setData({
      selected: 2,
      status: '4',
      expecte: ''
    })
    var that = this
    app.request({
      url: sever+'getorder',
      method: 'get',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        status: '4'
      },
      success: function(res) {
        that.setData({
          next_page_url: res.data.data.next_page_url
        })
        console.log(res.data.data.data)
        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)
      }
    })
  },
  tap4: function() {
    wx.setStorageSync('pa', 3)
    this.setData({
      selected: 3,
      status: '5',
      expecte: ''
    })
    var that = this
    app.request({
      url: sever+'getorder',
      method: 'get',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        status: '5'
      },
      success: function(res) {
        that.setData({
          next_page_url: res.data.data.next_page_url
        })
        console.log(res.data.data.data)
        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)
      }
    })
  },
  tap5: function() {
    wx.setStorageSync('pa', 4)
    this.setData({
      selected: 4,
      expecte: '1',
      status: ''
    })
    var that = this
    app.request({
      url: sever+'getorder',
      method: 'get',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        expected: '1',
      },
      success: function(res) {
        that.setData({
          next_page_url: res.data.data.next_page_url
        })
        console.log(res.data.data.data)
        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)
      }
    })
  },

  detail: function(e) {
    console.log(e.target.id)
    wx.setStorageSync("detail_order_id", e.target.id);
    wx.navigateTo({
      url: '../packing/packing-detail',
    })
  },
  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)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showToast({
      title: '',
      icon: 'loading',
      duration: 500
    })
    wx.reLaunch({
      url: 'packing',
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    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,
            status: that.data.status,
            expected: that.data.expecte
          },
          method: 'get',
          success: function(res) {
            console.log(res.data.data.data)
            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 {
        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
      // })
    }
  },

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

  },
  backhome: function () {
    wx.reLaunch({
      url: '/page/homePage/homePage',
    })
  },
  handleClick() {
    Dialog({
      title: '',
      message: '',
      selector: '#zan-dialog-test'
    }).then((res) => {
      console.log(res);
    })
  },
  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
    })
  },
  searchin: function(e) {
    var that = this
    that.setData({
      keyword: e.detail.value,
    })
    that.search()
  },
  search: function() {
    wx.setStorageSync('pa', 0)
    var that = this
    that.setData({
      status: '10',
      selected: 0,
      expecte: ''
    })
    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)
      }
    })
  },
  scanqrCode: () => {
    wx.scanCode({
      scanType: ['qrCode'],
      success: (res) => {
        console.log(res.result)
        // wx.navigateTo({
        //   url: '../search/search?order_id=35',
        // })
      }
    })
  },
  dayin: function(e) {
    wx.showLoading({
      title: '打印中'
    })
    app.request({
      url: sever+'printorder',
      method: 'post',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        order_id: e.target.id
      },
      success: function(res) {
        if (res.data.data.code == 200) {
          wx.hideLoading()
          wx.showToast({
            title: '打印成功',
            icon: 'success',
            duration: 1000,
            success: function() {

            }
          })
        } else {
          wx.showToast({
            title: '打印失败',
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '打印失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },
  fahuo: function(e) {
    var that = this
    that.setData({
      hiddenmodalput: false,
      o_id:e.target.id
    })
  },
  getRemark: function(e) {
    var that = this
    that.setData({
      fahuo: e.detail.value
    })
  },
  // 取消按钮
  cancel: function() {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认
  confirm: function() {
    var that = this
    this.setData({
      hiddenmodalput: true
    })
    app.request({
      url: sever+'updatestatus',
      method: 'post',
      data: {
        store_id: wx.getStorageSync('user').store_id,
        order_id: that.data.o_id,
        status: 4,
        remark: that.data.fahuo
      },
      success: function (res) {
        console.log(res.data.data.ok)
        if (res.data.data.ok == true) {
          wx.showToast({
            title: '发货完成',
            icon: 'success',
            duration: 1000,
            success: function () {
              wx.reLaunch({
                url: 'packing',
              })
            }
          })
        } else {
          wx.showToast({
            title: '发货失败',
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '发货失败',
          icon: 'none',
          duration: 1000
        })
      }
    })

  }
})