const _my = require("../../__antmove/api/index.js")(my);
const wx = _my;
// components/customField.js
const {
  selectContextByCardId,
  InsertContentByCardId
} = require("../../utils/api/customField");
const upload_image = require("../../utils/upload/upload_image");
Component({
  options: {
    observers: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    orgId: {
      type: Number
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    orgId: null,
    org: null
  },
  observers: {
    orgId(e) {
      if (e) {
        this.getList();
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 获取自定义列表
    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
          });
          this.triggerEvent("isFillFields", false);
        } else {
          this.triggerEvent("isFillFields", true);
        }
      }).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
      });
      this.triggerEvent("isFillFields", this.setList());
    },
    // 文本&时间&日期
    getValue(e) {
      let customNameVal = `list[${e.target.dataset.index}].customNameVal`;
      this.setData({
        [customNameVal]: e.detail.value
      });
      this.triggerEvent("isFillFields", this.setList());
    },
    // 获取省市区（街道）列表
    getAddress(e) {
      let index = e.currentTarget.dataset.index;
      let list = this.data.list;
      list[index].customNameVal = e.detail;
      this.setData({
        list
      });
      this.triggerEvent("isFillFields", this.setList());
    },
    //上传图片
    doUploadAvatar(event) {
      let that = this;
      let list = this.data.list;
      let index = event.target.dataset.index;
      wx.chooseMedia({
        mediaType: ["image"],
        sizeType: ["original"],
        sourceType: ["album", "camera"],
        success(res) {
          var image = {
            url: res.tempFiles[0].tempFilePath
          };
          wx.showLoading({
            title: "上传中~"
          });
          upload_image(image).then(img => {
            wx.hideLoading();
            list[index].customNameVal = img.url;
            that.setData({
              list
            });
            that.triggerEvent("isFillFields", that.setList());
          }).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
            });
            that.triggerEvent("isFillFields", this.setList());
          }
        }
      });
    },
    // 预览图片
    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 data = {
    //         list: this.data.list,
    //         id: wx.getStorageSync('userInfo').userId,
    //         adminId: parseInt(this.data.orgId)
    //     }
    //     InsertContentByCardId(data).then((suc) => {
    //         if (suc.code === 200) {
    //             wx.showModal({
    //                 content: '提交成功！',
    //                 showCancel: false,
    //                 success() {
    //                     let url = `/pages/wx_airport/${(that.data.org !== null&&that.data.org !== '')?'rlxf_logs/rlxf_logs?org=' + that.data.org:'rlxf_pass/rlxf_pass?orgId=' + that.data.orgId}`
    //                     wx.reLaunch({
    //                         url
    //                     })
    //                 }
    //             })
    //         } else {
    //             wx.showToast({
    //                 title: suc.msg,
    //             })
    //         }
    //     })
    // },
    // 判断是否填写
    setList() {
      let canSubmit = true;
      let list = this.data.list;
      list.forEach((item, index) => {
        if ((item.customNameVal == null || item.customNameVal == "") && item.isShow === 1) {
          return canSubmit = false;
        }
      });
      if (canSubmit) return list;else return canSubmit;
    },
    antmoveAction: function () {
      //执行时动态赋值，请勿删除
    }
  }
});