const app = getApp();
var dateTimePicker = require('../../../../utils/dateTimePicker');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    seleteType:[
      {name:'支',type:'10'},
      {name:'包',type:'20'},
      {name:'箱',type:'30'},
      {name:'托盘',type:'40'},
      {name:'其他',type:'50'},
    ], 
    //运输类型id  10 汽车 20船 30火车 40飞机 50其他
    transportType:[
      {name:'汽车',type:'10'},
      {name:'船',type:'20'},
      {name:'火车',type:'30'},
      {name:'飞机',type:'40'},
      {name:'其他',type:'50'},
    ],
    transportOrder:{}, // 订单信息
    warningList:[],
    productList:[],
    userInfo:{},
    name:'',
    deptType:'',
    transport:'',
    warning:'',
    product:'',
    num:'',
    remark:'',
    pid:0,
    productId:'',
    chainsNum:1,

    multiArray: [],
    listByType:[],
    multiIndex: [],
    //时间
    dateTimeArray1: null,
    dateTime1: null,
    startYear: null,
    endYear: null,
    estiTime:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.item){
      var item = JSON.parse(options.item);
      this.setData({
        item,
        name:item.name,
        deptType:this.setOptonsType(item.packageType,this.data.seleteType,'type'),
        transport:this.setOptonsType(item.transportType,this.data.transportType,'type'),
        num:item.num,
        remark:item.remark,
        pid:item.pid,
        productName:item.productName,
        productId:item.productId || '',
        estiTime:item.estiTime,
      })
      if(item.id){
        this.getInfo(item.id)
      }
    }else if(options.pid){
      this.setData({
        pid:options.pid
      })
    }
    var userInfo = wx.getStorageSync('userInfo');
    this.setData({userInfo})
    this.getWarningList();
    // this.getProductInfo();
    this.getProductList();
    this.getTime()
  },
  getTime(){
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    //var lastArray = obj1.dateTimeArray.pop();
    //var lastTime = obj1.dateTime.pop();
    this.setData({
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });
  },
  // 选择日期时间
  changeDateTime1(e) {
    var that = this;
    // {{dateTimeArray1[0][dateTime1[0]]}}-{{dateTimeArray1[1][dateTime1[1]]}}-{{dateTimeArray1[2][dateTime1[2]]}} {{dateTimeArray1[3][dateTime1[3]]}}:{{dateTimeArray1[4][dateTime1[4]]}}:{{dateTimeArray1[5][dateTime1[5]]}}
    this.setData({
      dateTime1: e.detail.value,
      // estiTime:
    },()=>{
      var dateTimeArray1 = that.data.dateTimeArray1;
      var dateTime1 = that.data.dateTime1;
      var years = dateTimeArray1[0][dateTime1[0]] //年
      var month = dateTimeArray1[1][dateTime1[1]] // 月
      var day = dateTimeArray1[2][dateTime1[2]] // 日
      var hours = dateTimeArray1[3][dateTime1[3]] // 时
      var min = dateTimeArray1[4][dateTime1[4]] // 分
      var seconds = dateTimeArray1[5][dateTime1[5]] // 秒
      that.setData({
        estiTime:years+'-'+month+'-'+day+' '+hours+':'+min+':'+seconds,
      })
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1,
      dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },
  onShow(){
    // var orderSelect = wx.getStorageSync('orderSelect') || {}
    // this.setData({
    //   transportOrder:orderSelect,
    //   transportUid:orderSelect.uid,
    //   transCode:orderSelect.code,
    // })
  },
  //扫描订单二维码
  goOrderScan(){
    var that=this
    wx.scanCode({
      success (res) {
        app.requestP({
          url: "/transport/pageByCode?code="+res.result,
          method: "get",
        }).then(res=>{
          var data = res.data.rows
          if(data.length){
            var order = data[0]
            that.setData({
              transportOrder:{
                transCode:order.transportCode,
                transportUid:order.transportUid,
              },
            })
          }else{
            app.showToptip(this,'error','未查询到此订单')
          }
        })
      }
    })
  },
  //去选择订单绑定页面
  goOrderSelcet(){
    wx.navigateTo({
      url: '/pages/workbench/orderList/orderSelect',
    })
  },
  getInfo(pid){
    app.requestP({
      url: "/chain/info?chainId="+pid,
      method: "post",
      contentType: 'application/json',
    }).then(res=>{
      const {transportCode,transportUid,estiTime} = res.data.data;
      this.setData({
        transportOrder:{
          transCode:transportCode,
          transportUid:transportUid,
          estiTime,
        },
      })
    })
  },
  setOptonsType(item,params,type){
    var index;
    for(var i=0;i<params.length;i++){
      if(item == params[i][type]){
        index = i;
        break;
      }
    }
    return index;
  },
  setType(e){
    //数据
    var value = e.detail.value;
    this.setData({
      deptType:value+''
    })
  },
  setTransportType(e){
    var value = e.detail.value;
    this.setData({
      transport:value+''
    })
  },
  setWarningType(e){
    var value = e.detail.value;
    this.setData({
      warning:value+''
    })
  },
  setProduct(e){
    var value = e.detail.value;
    var productList = this.data.productList;
    console.log(value)
    var multiIndex = this.data.multiIndex;
    if(productList[1][value[1]]){
      this.setData({
        multiIndex: e.detail.value,
        productName:'请选择'
      })
    }else{
      app.showToptip(this,'error','当前分组暂无产品')
    }

  },
  bindMultiPickerColumnChange(e){
    var that = this;
    // console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.productList,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    app.requestP({
      url: '/product/pageProduct',
      method: 'post',
      contentType: ' application/json',
      data: {
        productTypeId: data.multiArray[0][data.multiIndex[0]].id,
        keyword: "",
        pageNum: 1,
        pageSize: 99
      }
    }).then(response => {
      var productList = 'productList[1]';
      that.setData({
        [productList]: response.data.data
      });
    }).catch(err => {
      app.showToptip(that, 'error', err.data.msg);
    })
    this.setData({
      productList:data.multiArray,
      multiIndex:data.multiIndex
    })
  },
  getWarningList() {
    var that = this;
    app.requestP({
      url: '/tempRuleGroup/list',
      method: 'post',
    }).then(res => {
      that.setData({
        warningList: res.data.data,
      })
      if(that.data.item){
        that.setData({warning:that.setOptonsType(that.data.item.ruleGroupId,res.data.data,'id')})
      }
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg);
    })
  },
  getProductList() { //获取产品类别列表
    var that = this;
    var arr = [];
    var productList = that.data.productList;
    var listByType = that.data.listByType;
    app.requestP({
      url: '/productType/productTypeState',
      method: 'post',
      data: {
        keyword : '',
        pageNum:1,
        pageSize:99,
        productTypeId:''
      }
    }).then(res => {
      var data = res.data.data;
      var productList = 'productList[0]';
      that.setData({
        [productList]:data
      })
      app.requestP({
        url:'/product/pageProduct',
        method:'post',
        contentType:' application/json',
        data:{
          productTypeId:data[0].id,
          keyword: '',
          pageNum: 1,
          pageSize: 99

        }
      }).then(response=>{
        var productList = 'productList[1]';
        that.setData({
          [productList] : response.data.data 
        });
      }).catch(err=>{
        app.showToptip(that,'error',err.data.msg);
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg);
    })
  },
  bindinputWeight(e){
    let value =e.detail.value;
    value = value.replace(/\D/g, '');
    this.setData({
      chainsNum: value
    });
  },
  formSubmit(e){
    var that = this;
    var value = e.detail.value;
    const {transportOrder:{transCode,transportUid},seleteType,warningList,productList,transportType,userInfo,item,pid,productId,chainsNum,estiTime} = that.data;
    const {name,deptType,transport,warning,nickName,product,num,remarks} = value;
    var productInfoId = productId || productList[1][product[1]].id || '';
    if (name && estiTime && deptType.length != 0 && transport.length!=0 && warning.length!=0 && nickName && num && productInfoId && String(chainsNum).length > 0) {
      app.requestP({
        url:item ?'/chain/update':'/chain/createChains',
        method:'post',
        contentType:' application/json',
        data:{
          chainsNum,
          name,
          num,
          packageType:seleteType[deptType].type,
          ruleGroupId:warningList[warning].id,
          productId:productInfoId,
          remark:remarks || '',
          transportType:transportType[transport].type,
          pid,
          chainId:item ?item.id:'',
          transportUid,
          estiTime
        }
      }).then(res=>{
        app.showToptip(this,'success',item ?'修改成功' :'创建成功');
        wx.navigateBack({
          delta: 1
        });
      }).catch(err=>{
        app.showToptip(this,'error',err.data.msg);
      })
    }else{
      app.showToptip(this,'error','冷链内容不能为空');
    }
  },
})