// pages/bankDraft/add/add.js
import * as UTIL from '../../../utils/util.js';
import * as API from '../../../utils/API.js';
let EVN_CONFIG = require('../../../env/env');

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    form:{
      eventTime: UTIL.formatDate(new Date()), //开票日期 必填
      remark: "",
    },
    showBtn:true,
    showEventTime:false,
    status:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    //所属银行
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'major_type', {method:'GET'}, {
      success: (res) => {
        this.setData({
          majorTypeOptions:res.data
        });
      }
    })
    let templateQueryParams = {
      // 分页
      type:'5'
    };
    UTIL.httpRequest(API.URL_GET_TEMPLATELIST , templateQueryParams, {
      success: (res) => {
        that.setData({
          approvalTemplateList:res.rows,
          ["form.approvalTemplateId"]:res.rows[0].id,
          ["form.approvalTemplateName"]:res.rows[0].name,
        })
      }
    })

    if(options.id!=null&&options.id!=""){
      this.setData({id:options.id})
      UTIL.httpRequest(API.URL_GET_MAJOREVENTGET + this.data.id, {method:'GET'}, {
        success: (res) => {
          console.log(res);
          if(res.data.auditStatus!='0'&&res.data.auditStatus!='2'){
              this.setData({'showBtn':false});
          }
          res.data.majorTypeText = UTIL.getTransform(res.data.majorType,that.data.majorTypeOptions);
          let op = that.data.approvalTemplateList.filter(function (e) { return e.id == res.data.approvalTemplateId; });
          res.data.approvalTemplateName = op[0].name;
          this.setData({'form':res.data})
          this.selectComponent('#hf_editor').setHtml(res.data.eventContent);
          this.selectComponent('#hf_editor2').setHtml(res.data.dzbty);
          this.selectComponent('#hf_editor3').setHtml(res.data.lwhsy);
          this.selectComponent('#hf_editor4').setHtml(res.data.dqyshsyhjy);
          this.selectComponent('#hf_editor5').setHtml(res.data.cydbhjy);
          this.selectComponent('#hf_editor6').setHtml(res.data.jcjggk);
          this.selectComponent('#hf_editor7').setHtml(res.data.ssqkgk);
        }
      })
    }else{
     this.onShow()
    }
  },
  onConfirmPickApproval(event) {
    console.log(event);
    var that = this;
    this.setData({
      ["form.showApprovalTemplateName"]: false,
      ["form.approvalTemplateName"]: event.detail.value.name,
      ["form.approvalTemplateId"]: event.detail.value.id,
    });
  },
  onConfirmEventTime(e){
    let data = this.getNewDate(new Date(e.detail.value));
    this.setData({'form.eventTime':data});
  },
  getHtml(e) {//从组件获取值
    this.setData({
      [e.currentTarget.dataset.name]:e.detail.content.html
    });
  },
  insertImage(e){ //图片上传插入示例
    wx.chooseMedia({
      count: 1,
      success: r => {
        // 本地测试图片插入
        // this.selectComponent('#hf_editor').insertSrc(res.tempFilePaths[0]);
        console.log(r.tempFiles[0]);
        wx.uploadFile({ //调用图片上传接口
          url: wx.getStorageSync('dressCode')+'/common/upload',
          filePath: r.tempFiles[0].tempFilePath,
          header: {
            'Authorization':'Bearer '+getApp().globalData.userInfo.token
          },
          name: 'file',
          success: res => {
            console.log(111111);
            let imgUrl = JSON.parse(res.data).fileName
            this.selectComponent('#'+e.currentTarget.dataset.id).insertSrc(wx.getStorageSync('dressCode')+imgUrl);//调用组件insertSrc方法
          }
        })
      }
    })
  },
  getNewDate(date){
    //date是传过来的时间戳，注意需为13位，10位需*1000
    //也可以不传,获取的就是当前时间
    var time
    if(date){
       time = new Date(date);
    }else{
       time = new Date();
    }
    var year= time.getFullYear()  //年
    var month = ("0" + (time.getMonth() + 1)).slice(-2); //月
    var day = ("0" + time.getDate()).slice(-2); //日
    var mydate = year + "-" + month + "-" + day;
    return mydate
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },
  openBox(even){
    console.log(even.currentTarget.dataset.name);
    this.setData({
      [even.currentTarget.dataset.name]:true
    })
  },
  onChange(event){
    console.log(event);
    this.setData({
      [event.currentTarget.dataset.value]: event.detail,
    })
  },
  closeBox(even){
    console.log(even.currentTarget.dataset.name);
    this.setData({
      [even.currentTarget.dataset.name]:false
    })
  },
  goSubmit(){
    if(this.data.form.eventName===''||this.data.form.eventName==null){ //事项名称
      UTIL.showToastNoneIcon('请填写事项名称！');
      return false;
    }else if(this.data.form.eventTime===''||this.data.form.eventTime==null){  //事项时间
      UTIL.showToastNoneIcon('事项时间不能为空！');
      return false;
    }else if(this.data.form.eventContent===''||this.data.form.eventContent==null){  //事项内容
      UTIL.showToastNoneIcon('事项内容不能为空！');
      return false;
    }else if(this.data.form.dzbty===''||this.data.form.dzbty==null){  //事项内容
      UTIL.showToastNoneIcon('村党支部提议不能为空！');
      return false;
    }else if(this.data.form.lwhsy===''||this.data.form.lwhsy==null){  //事项内容
      UTIL.showToastNoneIcon('两委会商议不能为空！');
      return false;
    }else if(this.data.form.dqyshsyhjy===''||this.data.form.dqyshsyhjy==null){  //事项内容
      UTIL.showToastNoneIcon('党群议事会审议和决议不能为空！');
      return false;
    }else if(this.data.form.jcjggk===''||this.data.form.jcjggk==null){  //事项内容
      UTIL.showToastNoneIcon('决策结果公开不能为空！');
      return false;
    }else if(this.data.form.ssqkgk===''||this.data.form.ssqkgk==null){  //事项内容
      UTIL.showToastNoneIcon('实施情况公开不能为空！');
      return false;
    }else if(this.data.status=='0'){
      this.setData({'status':'1'})
      var that = this;
      that.data.form.method = 'POST';
      that.data.form.eventContent = that.data.form.eventContent.replace(/\\/g,"/")
      that.data.form.eventContent = that.data.form.eventContent.replace(/"/g,"'")
      that.data.form.dzbty = that.data.form.dzbty.replace(/"/g,"'")
      that.data.form.lwhsy = that.data.form.lwhsy.replace(/"/g,"'")
      that.data.form.dqyshsyhjy = that.data.form.dqyshsyhjy.replace(/"/g,"'")
      that.data.form.cydbhjy = that.data.form.cydbhjy.replace(/"/g,"'")
      that.data.form.jcjggk = that.data.form.jcjggk.replace(/"/g,"'")
      that.data.form.ssqkgk = that.data.form.ssqkgk.replace(/"/g,"'")
      console.log(that.data.form);
      if(that.data.form.id==""||that.data.form.id==null){
        UTIL.httpRequest(API.URL_POST_MAJOREVENTADD, that.data.form , {
          success: (res) => {
            this.setData({'status':0})
            if(res.code == 200){
              UTIL.showToastNoneIcon('新增成功');
              setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })
              },2000)
            }else{
              UTIL.showToastNoneIcon('新增失败：'+res.msg);
            }
          }
        })
      }else{
        UTIL.httpRequest(API.URL_POST_MAJOREVENTUPDATE, that.data.form , {
          success: (res) => {
            this.setData({'status':0})
            if(res.code == 200){
              UTIL.showToastNoneIcon('修改成功');
              setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })
              },2000)
            }else{
              UTIL.showToastNoneIcon('修改失败：'+res.msg);
            }
          }
        })
      }
    }   
  },
  onConfirmTime(event){
    this.setData({
      [event.currentTarget.dataset.name]: false,
      [event.currentTarget.dataset.value]: UTIL.formatDate(event.detail),
    });
  },
  openBox(even){
    console.log(even.currentTarget.dataset.name);
    this.setData({
      [even.currentTarget.dataset.name]:true
    })
  },
  closeBox(even){
    console.log(even.currentTarget.dataset.name);
    this.setData({
      [even.currentTarget.dataset.name]:false
    })
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  onConfirm(event) {
    this.setData({
      [event.currentTarget.dataset.name]: false,
      [event.currentTarget.dataset.value]: event.detail.value.dictValue,
      [event.currentTarget.dataset.value+'Text']: event.detail.value.dictLabel,
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

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

  }
})