"use strict";
const common_vendor = require("../../common/vendor.js");
const utils_api = require("../../utils/api.js");
const utils_formatTime = require("../../utils/formatTime.js");
const utils_plate = require("../../utils/plate.js");
const utils_des = require("../../utils/des.js");
if (!Array) {
  const _easycom_uni_datetime_picker2 = common_vendor.resolveComponent("uni-datetime-picker");
  _easycom_uni_datetime_picker2();
}
const _easycom_uni_datetime_picker = () => "../../uni_modules/uni-datetime-picker/components/uni-datetime-picker/uni-datetime-picker.js";
if (!Math) {
  _easycom_uni_datetime_picker();
}
const _sfc_main = {
  __name: "register",
  setup(__props) {
    const type = common_vendor.ref("");
    const visName = common_vendor.ref("");
    const visPhone = common_vendor.ref("");
    const visStartTime = common_vendor.ref("");
    const visEndTime = common_vendor.ref("");
    const visIdNumber = common_vendor.ref("");
    const visReason = common_vendor.ref("");
    const visCarNumber = common_vendor.ref("");
    const visPeopleNum = common_vendor.ref("");
    const list = common_vendor.ref([]);
    const passiveName = common_vendor.ref("");
    const passivePhone = common_vendor.ref("");
    const authValue = common_vendor.ref(false);
    const btnFlag = common_vendor.computed(() => {
      const tem = list.value.some((item) => item.isChecked);
      if (visName.value && visPhone.value && visStartTime.value && visEndTime.value && visIdNumber.value && visReason.value && [type.value === "1" && !list.value.length] && [type.value === "1" && !tem] && [type.value === "2" && !passiveName.value] && [
        type.value === "2" && !passivePhone.value
      ] && authValue.value) {
        return true;
      } else {
        return false;
      }
    });
    common_vendor.onLoad((options) => {
      type.value = options.type;
      if (type.value === "1") {
        visName.value = common_vendor.index.getStorageSync("userInfo").name;
        visPhone.value = common_vendor.index.getStorageSync("userInfo").phone;
        visIdNumber.value = common_vendor.index.getStorageSync("userInfo").idCard;
        getList();
      } else {
        passiveName.value = common_vendor.index.getStorageSync("userInfo").name;
        passivePhone.value = common_vendor.index.getStorageSync("userInfo").phone;
      }
    });
    const getList = async () => {
      const res = await utils_api.myRequest({
        url: "/wanzai/api/smartUser/queryAffiliateUserById",
        data: {
          id: common_vendor.index.getStorageSync("userInfo").id
        }
      });
      const result = JSON.parse(utils_des.decryptDes(res.data));
      list.value = result;
    };
    const handleSubmit = () => {
      const flag = verifyValue();
      if (flag) {
        if (type.value === "1") {
          parentAppointment();
        } else {
          otherAppointment();
        }
      }
    };
    const parentAppointment = async () => {
      let temList = [];
      list.value.forEach((ele) => {
        if (ele.isChecked) {
          temList.push(ele);
        }
      });
      const res = await utils_api.myRequest({
        url: "/wanzai/api/smartVisitor/parentsAppointment",
        method: "post",
        data: {
          userId: common_vendor.index.getStorageSync("userInfo").id,
          visitorTime: visStartTime.value,
          visitReason: visReason.value,
          carNum: visCarNumber.value,
          peerNum: visPeopleNum.value ? visPeopleNum.value : 0,
          students: temList
        }
      });
      common_vendor.index.showToast({
        title: res.message,
        icon: "none",
        duration: 2e3
      });
      setTimeout(() => {
        common_vendor.index.setStorageSync("Tab-activeIndex", 1);
        common_vendor.index.reLaunch({
          url: "/pagesReservation/record/record"
        });
      }, 2e3);
    };
    const otherAppointment = async () => {
      const res = await utils_api.myRequest({
        url: "/wanzai/api/smartVisitor/otherAppointment",
        method: "post",
        data: {
          userId: common_vendor.index.getStorageSync("userInfo").id,
          userName: visName.value,
          userPhone: visPhone.value,
          userCard: visIdNumber.value,
          visitorTime: visStartTime.value,
          visitReason: visReason.value,
          carNum: visCarNumber.value,
          peerNum: visPeopleNum.value ? visPeopleNum.value : 0,
          respondentName: passiveName.value,
          respondentPhone: passivePhone.value
        }
      });
      common_vendor.index.showToast({
        title: res.message,
        icon: "none",
        duration: 2e3
      });
      setTimeout(() => {
        common_vendor.index.setStorageSync("Tab-activeIndex", 2);
        common_vendor.index.reLaunch({
          url: "/pagesReservation/record/record"
        });
      }, 2e3);
    };
    const changeStart = (e) => {
      const temTime = e.replace(/-/g, "/");
      const timestamp = Date.parse(new Date(temTime));
      const addTime = 1e3 * 60 * 60 * 4;
      visEndTime.value = utils_formatTime.time_format(timestamp + addTime);
    };
    const verifyValue = () => {
      const regName = /^[\u4e00-\u9fa5]{2,4}$/;
      const regPhone = /^1[3-9]\d{9}$/;
      const regIdentity = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
      if (!visName.value) {
        common_vendor.index.showToast({
          title: "请输入访客姓名",
          icon: "none"
        });
        return false;
      }
      if (!regName.test(visName.value)) {
        common_vendor.index.showToast({
          title: "姓名格式错误",
          icon: "none"
        });
        return false;
      }
      if (!visPhone.value) {
        common_vendor.index.showToast({
          title: "请输入访客手机号",
          icon: "none"
        });
        return false;
      }
      if (!regPhone.test(visPhone.value)) {
        common_vendor.index.showToast({
          title: "手机号码格式错误",
          icon: "none"
        });
        return false;
      }
      if (!visStartTime.value) {
        common_vendor.index.showToast({
          title: "请选择来访时间",
          icon: "none"
        });
        return false;
      }
      if (!visEndTime.value) {
        common_vendor.index.showToast({
          title: "请选择来访结束时间",
          icon: "none"
        });
        return false;
      }
      if (!visIdNumber.value) {
        common_vendor.index.showToast({
          title: "请输入访客证件号",
          icon: "none"
        });
        return false;
      }
      if (!regIdentity.test(visIdNumber.value)) {
        common_vendor.index.showToast({
          title: "证件号格式错误",
          icon: "none"
        });
        return false;
      }
      if (!visReason.value) {
        common_vendor.index.showToast({
          title: "请输入访问事由",
          icon: "none"
        });
        return false;
      }
      if (visCarNumber.value && !utils_plate.isVehicleNumber(visCarNumber.value)) {
        common_vendor.index.showToast({
          title: "请输入正确的车牌号",
          icon: "none"
        });
        return false;
      }
      if (type.value === "1" && !list.value.length) {
        common_vendor.index.showToast({
          title: "请绑定学生后再开始预约",
          icon: "none"
        });
        return false;
      }
      const tem = list.value.some((item) => item.isChecked);
      if (type.value === "1" && !tem) {
        common_vendor.index.showToast({
          title: "请选择受访学生",
          icon: "none"
        });
        return false;
      }
      if (type.value === "2" && !passiveName.value) {
        common_vendor.index.showToast({
          title: "请输入受访者姓名",
          icon: "none"
        });
        return false;
      }
      if (type.value === "2" && !regName.test(passiveName.value)) {
        common_vendor.index.showToast({
          title: "受访者姓名格式错误",
          icon: "none"
        });
        return false;
      }
      if (type.value === "2" && !passivePhone.value) {
        common_vendor.index.showToast({
          title: "请输入受访者电话",
          icon: "none"
        });
        return false;
      }
      if (type.value === "2" && !regPhone.test(passivePhone.value)) {
        common_vendor.index.showToast({
          title: "受访者手机号格式错误",
          icon: "none"
        });
        return false;
      }
      if (!authValue.value) {
        common_vendor.index.showToast({
          title: "请同意授权证件号",
          icon: "none"
        });
        return false;
      }
      return true;
    };
    const handleClick = (item) => {
      item.isChecked = !item.isChecked;
    };
    const handleAuth = () => {
      authValue.value = !authValue.value;
    };
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: type.value == 1,
        b: visName.value,
        c: common_vendor.o(($event) => visName.value = $event.detail.value),
        d: type.value == 1,
        e: visPhone.value,
        f: common_vendor.o(($event) => visPhone.value = $event.detail.value),
        g: common_vendor.t(visStartTime.value || "请选择来访时间 >"),
        h: visStartTime.value ? 1 : "",
        i: common_vendor.o(changeStart),
        j: common_vendor.o(($event) => visStartTime.value = $event),
        k: common_vendor.p({
          modelValue: visStartTime.value
        }),
        l: common_vendor.t(visEndTime.value || "来访时间为4小时"),
        m: visEndTime.value ? 1 : "",
        n: type.value == 1,
        o: visIdNumber.value,
        p: common_vendor.o(($event) => visIdNumber.value = $event.detail.value),
        q: visReason.value,
        r: common_vendor.o(($event) => visReason.value = $event.detail.value),
        s: visCarNumber.value,
        t: common_vendor.o(($event) => visCarNumber.value = $event.detail.value),
        v: visPeopleNum.value,
        w: common_vendor.o(common_vendor.m(($event) => visPeopleNum.value = $event.detail.value, {
          number: true
        })),
        x: type.value == 2
      }, type.value == 2 ? {
        y: type.value == 2,
        z: passiveName.value,
        A: common_vendor.o(($event) => passiveName.value = $event.detail.value)
      } : {}, {
        B: type.value == 2
      }, type.value == 2 ? {
        C: type.value == 2,
        D: passivePhone.value,
        E: common_vendor.o(($event) => passivePhone.value = $event.detail.value)
      } : {}, {
        F: type.value == 1
      }, type.value == 1 ? {} : {}, {
        G: type.value == 1
      }, type.value == 1 ? {
        H: common_vendor.f(list.value, (item, k0, i0) => {
          return {
            a: item.isChecked,
            b: common_vendor.t(item.name),
            c: common_vendor.t(item.cardNo),
            d: item.id,
            e: common_vendor.o(($event) => handleClick(item), item.id)
          };
        })
      } : {}, {
        I: authValue.value,
        J: common_vendor.o(handleAuth),
        K: btnFlag.value ? 1 : "",
        L: common_vendor.o(handleSubmit)
      });
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-0b2c2ae2"], ["__file", "E:/workSpace/WanZai_School_app/pagesReservation/register/register.vue"]]);
wx.createPage(MiniProgramPage);
