// pages/voucherManagementAddto/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectNameList:[],
    voucherTypeList:[],
    loadShow:false,
    loginName:'',
    userName:'',
    deptId:'',
    projectName:'',
    projectId:'',
    fileList:[],
    //参数
    voucherType:'',
    happenTime:'',
    uploadTime:'',
    remarks:'',
  },

  //选择项目名称
  onSelectProjectName(e){
    console.log(e.detail)
    this.setData({
      projectId:e.detail.id,
      projectName:e.detail.text
    })
  },
  //选择凭证类型
  onSelectVoucherType(e){
    console.log(e.detail)
    this.setData({
      voucherType:e.detail.id
    })
  },
  //发生时间
  onOccurrenceTime(e){
    console.log(e.detail)
    this.setData({
      happenTime:e.detail
    })
  },
  //上传时间
  onUploadTime(e){
    console.log(e.detail)
    this.setData({
      uploadTime:e.detail
    })
  },
  //备注
  remarks(e){
    console.log(e.detail.value);
    this.setData({
      remarks:e.detail.value
    })
  },

  
  onClickShow() {
    this.setData({ loadShow: true });
  },

  onClickHide() {
    this.setData({ loadShow: false });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    var that = this;
    //加载蒙版
    that.onClickShow();
    //获取缓存数据
    wx.getStorage({
      key: 'userinfo',
      success:function(res){
        console.log(res.data);
        that.setData({
          loginName:res.data.loginName,
          userName:res.data.userName,
          deptId:res.data.deptId,
          projectNameList:[{text:options.projectName,id:options.projectId}]
        })  
        that.getDocumentType();
      }
    }) 
  },

  /**
   * 凭证类型
   */
  getDocumentType:function(){
    wx.request({
      url: app.globalData.reqUrl+'/weixin/security/getDocumentType',
      method: 'get',
      data: {
        deptId:this.data.deptId,
        projectId:this.data.projectId,
      },
      success: resData => {
          this.onClickHide();
          if(resData.data.code == 200){
            let DeviceGroupData = resData.data.data;
            let list = [];
            for(let i = 0;i<DeviceGroupData.length;i++){
              list.push({"id":DeviceGroupData[i].id,"text":DeviceGroupData[i].name});
            }
            this.setData({
              voucherTypeList:list
            })
          }
      }
    })
  },

  onImagesArr(e){
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      //图片上传地址
      url: app.globalData.reqUrl+'/weixin/security/fileUpload',
      filePath: e.detail[0],
      name: 'file',
      header: {
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      formData: { user: 'test' },
      success:res => {
        let data = JSON.parse(res.data);
        // 上传完成需要更新 fileList
        let fileList = this.data.fileList
        fileList.push(data);
        this.setData({ fileList:fileList});
      },
    });
  },

  add:function(){
    //判断值是否为空
    if(this.data.projectId == '' || this.data.projectId == undefined){
      app.toast("请选择项目");
      return;
    }else if(this.data.voucherType == '' || this.data.voucherType == undefined){
      app.toast("请选择凭证类型");
      return;
    }else if(this.data.happenTime == '' || this.data.happenTime == undefined){
      app.toast("请选择发生时间");
      return;
    }else if(this.data.uploadTime == '' || this.data.uploadTime == undefined){
      app.toast("请选择上传时间");
      return;
    }else if(this.data.fileList.length == 0){
      app.toast("请上传文件");
      return;
    }
    console.log(this.data.fileList);
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      url:app.globalData.reqUrl+'/weixin/security/addDocumentData',
      data:{
        deptId:this.data.deptId,
        projectId:this.data.projectId,
        projectName:this.data.projectName,
        type:this.data.voucherType,
        createTime:this.data.happenTime,
        time:this.data.uploadTime,
        createUser:this.data.loginName,
        userName:this.data.userName,
        remarks:this.data.remarks,
        djCertificatesonList:JSON.stringify(this.data.fileList)
      },
      method:"POST",
     success:function(res){
       console.log(res.data);
       if(res.data.code == 200){
        wx.redirectTo({
          url: '../voucherManagement/index',
        })
       }else{
         app.toast("添加失败");
         return;
       }
     }
    })
  },

  /**
   * 返回凭证管理
   */
  goGCLB:function(){
    wx.redirectTo({
      url: '../voucherManagement/index'
    })
  },
})