const _my = require("../../../__antmove/api/index.js")(my);
const wx = _my;
// pages/wx_airport/rlxf_userMsg/index.js
const util = require("../../../utils/util/util");
const {
  selectContextByCardId,
  userInfoInsert
} = require("../../../utils/api/api");
const upload_image = require("../../../utils/upload/upload_image");
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    orgId: null,
    org: null
  },
  // 获取自定义列表
  getList() {
    wx.showLoading({
      title: "加载中..."
    });
    selectContextByCardId(this.data.orgId, wx.getStorageSync("userInfo").userId).then(suc => {
      let data = suc.data.some(i => {
        return i.customNameVal === null && i.isShow == 1 && i.isEditable === 1;
      });
      if (data) {
        this.setData({
          list: suc.data,
          list: suc.data
        });
      } else {
        this.submit();
      }
    }).finally(() => {
      wx.hideLoading();
    });
  },
  // 下拉
  getSelect(e) {
    let index = e.target.dataset.index;
    let customNameVal = `list[${index}].customNameVal`;
    this.setData({
      [customNameVal]: this.data.list[index].configurationOption[e.detail.value].name
    });
  },
  // 文本&时间&日期
  getValue(e) {
    let customNameVal = `list[${e.target.dataset.index}].customNameVal`;
    this.setData({
      [customNameVal]: e.detail.value
    });
  },
  // 获取省市区（街道）列表
  getAddress(e) {
    let index = e.currentTarget.dataset.index;
    let list = this.data.list;
    list[index].customNameVal = e.detail;
    this.setData({
      list
    });
  },
  //上传图片
  doUploadAvatar(event) {
    let that = this;
    let list = this.data.list;
    let index = event.target.dataset.index;
    wx.chooseImage({
      count: 1,
      sizeType: ["original"],
      sourceType: ["album", "camera"],
      success(res) {
        const image = {
          url: res.tempFilePaths[0]
        }
        wx.showLoading({
          title: "上传中~"
        });
        upload_image(image).then(img => {
          wx.hideLoading();
          list[index].customNameVal = img.url;
          that.setData({
            list
          });
        }).catch(() => {
          wx.hideLoading();
        });
      }
    });
  },
  //删除照片
  DelImg(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let list = this.data.list;
    wx.showModal({
      title: "提示",
      content: "确定删除？",
      cancelText: "取消",
      confirmText: "确定",
      success: res => {
        if (res.confirm) {
          list[index].customNameVal = null;
          that.setData({
            list
          });
        }
      }
    });
  },
  // 预览图片
  previewImg(event) {
    // 拿到图片的地址url
    let currentUrl = event.currentTarget.dataset.src;
    // 微信预览图片的方法
    wx.previewImage({
      current: currentUrl,
      // 图片的地址url
      urls: [currentUrl] // 预览的地址url
    });
  },

  // 提交
  submit() {
    if (!this.setList()) {
      wx.showToast({
        title: "请将内容填写完整~",
        icon: "none"
      });
      return;
    }
    let that = this;
    let userInfo = wx.getStorageSync("userInfo");
    let data = {
      contents: this.data.list,
      userId: userInfo.userId,
      adminId: parseInt(this.data.orgId),
      type: app.data.type,
      action: 1 //1-自定义字段更新 2-下发
    };

    userInfoInsert(data).then(suc => {
      if (suc.code === 200 && suc.data.status) {
        userInfo.org = that.data.org;
        userInfo.orgId = that.data.orgId;
        wx.setStorageSync("userInfo", userInfo);
        wx.reLaunch({
          url: "/pages/wx_airport/rlxf_pass/rlxf_pass?orgId=" + that.data.orgId + "&org=" + that.data.org + "&showModal=" + this.data.showModal
        });
      } else {
        setTimeout(() => {
          wx.showToast({
            title: suc.msg,
            icon: "none"
          });
        }, 0);
        wx.navigateBack(1);
      }
    }).catch(err => {
      console.log(err);
    });
  },
  // 判断是否填写
  setList() {
    let canSubmit = true;
    let list = this.data.list;
    list.forEach((item, index) => {
      if (item.customNameVal == null && item.isShow === 1) {
        return canSubmit = false;
      }
    });
    return canSubmit;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({
    org = "",
    orgId = null,
    showModal = false
  }) {
    this.setData({
      org,
      orgId,
      showModal
    });
    this.getList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  antmoveAction: function () {
    //执行时动态赋值，请勿删除
  }
});