// pages/search/search.js
const Dialog = require('../../bower_components/zanui-weapp/dist/dialog/dialog');
const Toast = require('../../bower_components/zanui-weapp/dist/toast/toast');
const app = getApp()
var sever = require("../../api.js").sever;
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    o_id:'',
    hiddenmodalput: true,
    order_id:'',
    role:0,
    pic_url: '',
    showpic: false,
    msg: '',
    order_num: 0,
    order_status: '',
    order_created_time: '',
    gType: '',
    gCategory: '',
    gColor: '',
    gDescription: '',
    uName: '',
    uMobile: '',
    uAddress: '',
    expected_time: '',
    theimgs: [],
    status: 0,
    steps: [
      {
        // 此步骤是否当前完成状态
        current: false,
        // 此步骤是否已经完成
        done: true,
        // 此步骤显示文案
        text: '10.01',
        // 此步骤描述语
        desc: '步骤一'
      },
      {
        done: true,
        current: false,
        desc: '步骤二',
        text: '10.02'

      }],
    peisong: [{
      text: '顺丰快递',
      color: 'red',
      type: '顺丰快递'
    }, {
      text: '韵达快递',
      color: '#3CC51F',
        type: '韵达快递'
    }, {
      text: '申通快递',
        type: '申通快递'
    }, {
      text: '中通快递',
      color: 'red',
        type: '中通快递'
    }, {
      text: '厂家配送',
        type: '厂家配送'
    }
    ],
    theimgs: ['../../img/add.png', '../../img/add.png'],
    parts:[],
    fahuo:'',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    backhome: function () {
      wx.reLaunch({
        url: '/page/homePage/homePage',
      })
    },
    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)
        }
      })
    },
    onLoad:function(option){
      var that = this
      var user = wx.getStorageSync('user')
      if(!user){
        console.log('请先登录 ')
      }else{
        if (user.role == 0) {
          that.setData({
            role: 0
          })
        } else if (user.role == 1) {
          that.setData({
            role:1
          })
        } else if (user.role == 2) {
          that.setData({
            role: 2
          })
        } else if (user.role == 3) {
          that.setData({
            role: 3
          })
        }
      }
      that.setData({
        order_id: option.order_id
      })
      console.log(option.order_id)
      console.log('detailpage')
      app.request({
        url: sever+'getorderdetail',
        method: 'get',
        data: {
          store_id: wx.getStorageSync('user').store_id,
          order_id: option.order_id
        },
        success: function (res) {
          console.log(res.data)
          var order = res.data.data.order
          var picArr = []
          picArr = [...order.picture]
          var pArr = [...res.data.data.parts]
          console.log(pArr)
          that.setData({
            parts: [...pArr]
          })
          //picArr.push(order.picture.split(','))
          var stateArr = ["待审核", "待生产", "生产中", "待发货", "运输中", "已签收", "已评价", "已取消"]
          that.setData({
            status: order.status,
            pic_url: order.qrcode,
            msg: order.remark,
            order_num: order.sno,
            order_status: stateArr[order.status],
            order_created_time: order.created_at,
            gCategory: order.category,
            gType: order.type,
            gColor: order.color,
            gDescription: order.question,
            uName: order.customer_name,
            uMobile: order.customer_phone,
            uAddress: order.customer_address,
            expected_time: order.expected_time,
            theimgs: [...picArr]
          })
          var progress = res.data.data.progress
          var pArr = []
          for (var i = 0; i < progress.length; i++) {
            var pObj = {}
            pObj.done = true
            pObj.current = false
            pObj.text = progress[i].created_at
            pObj.desc = progress[i].description
            pArr.push(pObj)
          }
          pArr[pArr.length - 1].current = true
          that.setData({
            steps: [...pArr]
          })
        }
      })
    },
    fanhui: function () {
      var that = this
      if (that.data.role == 0) {
        wx.reLaunch({
          url: '../manager/manage',
        })
      }
      if(that.data.role == 1){
        wx.reLaunch({
          url: '../make/make',
        })
      }
      if (that.data.role == 2) {
        wx.reLaunch({
          url: '../packing/packing',
        })
      }
      if (that.data.role == 3) {
        wx.reLaunch({
          url: '../manager/manage',
        })
      }
    },
    makestart: function () {
      var that = this
      app.request({
        url: sever+'updatestatus',
        method: 'post',
        data: {
          store_id: wx.getStorageSync('user').store_id,
          order_id: that.data.order_id,
          status: 2,
        },
        success: function (res) {
          console.log(res.data.data.ok)
          if (res.data.data.ok == true) {
            wx.showToast({
              title: '开始生产',
              icon: 'success',
              duration: 1000,
              success: function () {
                that.setData({
                  status:2
                })
              }
            })
          } else {
            wx.showToast({
              title: '开始失败',
              icon: 'none',
              duration: 1000
            })
          }
        }
      })
    },
    makeok: function () {
      var that = this
      app.request({
        url: sever+'updatestatus',
        method: 'post',
        data: {
          store_id: wx.getStorageSync('user').store_id,
          order_id: that.data.order_id,
          status: 3,
        },
        success: function (res) {
          console.log(res.data.data.ok)
          if (res.data.data.ok == true) {
            wx.showToast({
              title: '生产完成',
              icon: 'success',
              duration: 1000,
              success: function () {
                that.setData({
                  status:3
                })
                wx.reLaunch({
                  url: '../make/make',
                })
              }
            })
          } else {
            wx.showToast({
              title: '完成失败',
              icon: 'none',
              duration: 1000
            })
          }
        }
      })
    },
    dayin: function () {
      var that = this
      wx.showLoading({
        title: '打印中'
      })
      app.request({
        url: sever+'printorder',
        method: 'post',
        data: {
          store_id: wx.getStorageSync('user').store_id,
          order_id: that.data.order_id
        },
        success: function (res) {
          if (res.data.data.code == 200) {
            wx.hideLoading()
            wx.showToast({
              title: '打印成功',
              icon: 'success',
              duration: 1000,
              success: function () {

              }
            })
          }
        }
      })
    },
    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.order_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/packing',
                })
              }
            })
          } else {
            wx.showToast({
              title: '发货失败',
              icon: 'none',
              duration: 1000
            })
          }
        },
        fail: () => {
          wx.showToast({
            title: '发货失败',
            icon: 'none',
            duration: 1000
          })
        }
      })

    },
    showPic: function (event) {
      // var that = this
      // that.setData({
      //   showpic: true
      // })
      var src = event.currentTarget.dataset.src;//获取data-src
      var imgList = [src];//获取data-list
      //图片预览
      wx.previewImage({
        current: src, // 当前显示图片的http链接
        urls: imgList // 需要预览的图片http链接列表
      })
    },
  }
})
