const _my = require("../../__antmove/api/index.js")(my);
const wx = _my;
// pages/company_register/company_register.js
import international from "../../international/appointment_scence/index";
const {
  makeRooms,
  floor,
  storiedBuilding
} = require("../../utils/enterpriseInformation/floor");
const {
  showToast
} = require("../../utils/func/request");
const {
  getBuildingList,
  companyCommit
} = require("../../utils/api/api");
const {
  pro_baseUrl_wxrlxf
} = require("../../utils/func/request");
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    international: international,
    //国际化语言包
    language: app.data.language,
    // 基础路径
    baseUrl: pro_baseUrl_wxrlxf,
    // swiper的index
    currentIndex: 0,
    // swiper滚动长度
    swiperDx: 0,
    // 步骤列表
    stepsList: [{
      name: "企业信息",
      name1: "Enterprise",
      complete: true
    }, {
      name: "营业执照信息",
      name1: "business license",
      complete: false
    }, {
      name: "登录信息",
      name1: "login",
      complete: false
    }],
    //员工人数
    employeesNum: [{
      name: "1-50人"
    }, {
      name: "50-100人"
    }, {
      name: "100-500人"
    }, {
      name: "500-1000人"
    }, {
      name: "1000人以上"
    }],
    showOCR: true,
    // 提交表单
    form: {},
    companyList: [],
    storiedBuildingList: storiedBuilding(),
    // 楼幢数据
    chooseBuilding: false,
    //选择租赁楼幢
    floorList: floor(),
    //楼层
    chooseFloor: false,
    roomNumList: [],
    chooseRoomNum: false
  },
  submit() {
    const that = this;
    wx.requestSubscribeMessage({
      entityIds: ["09f48c25223a42c09c5d1f1747e76392"],
      success(res) {
        if(res.behavior === 'cancel') {
          return
        }
        companyCommit(that.data.form).then(res => {
          if (res.code == 200) {
            wx.showModal({
              title: "提示",
              content: "提交企业申请成功",
              showCancel: false,
              //是否显示取消按钮
              duration: 1500,
              success(res) {
                if (res.confirm) {
                  wx.reLaunch({
                    url: "/pages/resitor_scence/resitor_scence"
                  });
                }
              }
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: "none"
            });
          }
        });
      }
    });
  },
  getEnterpriseOCR(e) {
    if (e.detail) {
      this.setData({
        "form.enterpriseFullName": e.detail.enterpriseFullName,
        "form.companyCode": e.detail.companyCode,
        "form.legalPerson": e.detail.legalPerson,
        "form.companyType": e.detail.companyType,
        "form.registrationAddress": e.detail.registeredAddress,
        "form.mainBusinessScope": e.detail.mainBusinessScope,
        "form.registeredCapital": e.detail.registeredCapital,
        "form.registrationTime": e.detail.registrationTime,
        "form.userId": app.data.userInfo.userId
      });
      this.setData({
        showOCR: false
      });
      this.matchStepComplete();
    }
  },
  // 下一步
  next_step() {
    let currentIndex = null;
    this.data.currentIndex == 1 ? currentIndex = 2 : currentIndex = 1;
    this.setData({
      currentIndex
    });
  },
  // 上一步
  previousStep() {
    let currentIndex = null;
    this.data.currentIndex == 1 ? currentIndex = 0 : currentIndex = 1;
    this.setData({
      currentIndex
    });
  },
  // 获得swiper-item位置
  getSwiperPercent(e) {
    this.setData({
      swiperDx: e.detail.dx
    });
  },
  // 改变swiper
  changeSwiper(e) {
    if (e.detail.current < this.data.currentIndex || this.data.stepsList[e.detail.current].complete) {
      this.setData({
        currentIndex: e.detail.current
      });
    } else {
      this.setData({
        currentIndex: this.data.currentIndex
      });
    }
  },
  //获取输入or选择的内容
  getValue(e) {
    var {
      form
    } = this.data;
    switch (e.currentTarget.dataset.type) {
      case "enterpriseSimpleName":
        this.setData({
          "form.enterpriseSimpleName": e.detail
        });
        // form.enterpriseSimpleName = e.detail
        break;
      case "employeesNumber":
        this.setData({
          "form.employeesNumber": e.detail.name
        });
        // form.employeesNumber = e.detail.name
        break;
      case "enterpriseFullName":
        this.setData({
          "form.enterpriseFullName": e.detail
        });
        // form.enterpriseFullName = e.detail
        break;
      case "actualPhone":
        this.setData({
          "form.actualPhone": e.detail
        });
        // form.actualPhone = e.detail
        break;
      case "storiedBuilding":
        this.setData({
          "form.storiedBuilding": e.detail.name
        });
        form.storiedBuilding = e.detail.name;
        break;
      case "company":
        this.setData({
          "form.company": e.detail.name
        });
        // form.company = e.detail.name
        break;
      case "reason":
        this.setData({
          "form.reason": e.detail.name
        });
        // form.reason = e.detail.name
        break;
      case "tenantId":
        this.setData({
          "form.tenantId": e.detail.adminId
        });
        // form.tenantId = e.detail.adminId
        break;
      case "leaseBuilding":
        // form.leaseBuilding = e.detail
        this.setData({
          "form.leaseBuilding": e.detail,
          chooseBuilding: false
        });
        break;
      case "floor":
        // form.leaseFloor = e.detail
        this.setData({
          "form.roomNumber": null,
          chooseFloor: false
        });
        var num = parseInt(e.detail.substr(0, e.detail.length - 1));
        this.setData({
          "form.leaseFloor": e.detail,
          roomNumList: makeRooms(num, 20, 20)
        });
        break;
      case "roomNumber":
        // form.roomNumber = e.detail
        this.setData({
          "form.roomNumber": e.detail,
          chooseRoomNum: false
        });
        break;
      case "actualName":
        this.setData({
          "form.actualName": e.detail
        });
        break;
      case "username":
        this.setData({
          "form.username": e.detail
        });
        break;
      case "password":
        this.setData({
          "form.password": e.detail
        });
        break;
      default:
        form[e.currentTarget.dataset.type] = e.detail;
        break;
    }
    this.matchStepComplete();
  },
  //步骤条判断是否完成
  matchStepComplete() {
    let form = this.data.form;
    this.data.stepsList[0].complete = form.actualName && form.actualPhone && form.employeesNumber && form.tenantId && form.leaseBuilding && form.leaseFloor && form.roomNumber ? true : false;
    this.data.stepsList[1].complete = form.enterpriseFullName && form.companyCode && form.legalPerson && form.companyType && form.registrationAddress && form.mainBusinessScope && form.registeredCapital && form.registrationTime ? true : false;
    this.data.stepsList[2].complete = form.username && form.password ? true : false;
    this.setData({
      stepsList: this.data.stepsList
    });
  },
  // 打开租赁楼幢弹出框
  showBuilding() {
    this.setData({
      chooseBuilding: true
    });
  },
  // 打开租赁楼层弹出框
  showFloor() {
    this.setData({
      chooseFloor: true
    });
  },
  // 打开租赁房间号弹出框
  showRoomNum() {
    this.setData({
      chooseRoomNum: true
    });
  },
  // 获取租赁楼宇
  getBuildingList() {
    getBuildingList().then(res => {
      this.setData({
        companyList: res.data
      });
    });
  },
  //查看照片
  //   watch() {
  // wx.previewImage({
  //   urls: [this.data.baseUrl + '/file/buildingOperationInstructions.pdf'],
  // })
  //下载文件
  async download() {
    wx.downloadFile({
      url: "https://tx.hz-hanghui.com:8088/buildingOperationInstructions.pdf",
      //仅为示例，并非真实的资源
      success(res) {
        if (res.statusCode === 200) {
          const file_path = res.tempFilePath;
          wx.openDocument({
            filePath: file_path,
            fileType: "pdf",
            success: function (res) {
              wx.showToast({
                title: "下载成功！",
                icon: "none"
              });
            },
            fail(err) {
              wx.showToast({
                title: err.errMsg,
                icon: "none"
              });
            }
          });
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBuildingList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      language: app.data.language
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  antmoveAction: function () {
    //执行时动态赋值，请勿删除
  }
});