<template>
  <view>
    <my_form
      ref="form"
      :form="form"
      @isUncomplete="isUncomplete"
      @interrupt="interrupt"
      requiredFieldOnly
    />
    <view style="height: 70px" />
    <view class="footer">
      <view
        class="add"
        @click="uncomplete || loading ? '' : submit()"
        :class="uncomplete ? 'forbidden' : ''"
        hover-class=" navigator-hover"
      >
        <u-loading-icon v-if="loading" />
        <block v-else> 提交 </block>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      sys_enterprise_hrid: 0,
      form: [],
      uncomplete: true,
      loading: false,
    };
  },
  async onLoad(options) {
    uni.setNavigationBarTitle({
      title: options.title || "新增人员",
    });
    let form = [
      {
        key: "name",
        type: "text",
        label: "姓名",
        isMust: true, //是否必填
        value: "",
      },
      {
        key: "phonenumber",
        type: "text",
        label: "手机号",
        isMust: true, //是否必填
        value: "",
        placeholder: "请输入收货人手机号",
        inputmode: "number",
        verify: [this.getReg("phonenumber")],
      },
      {
        key: "sex",
        type: "sex",
        label: "性别",
        value: "男",
      },
      {
        key: "birthday",
        type: "date",
        label: "生日",
        value: "",
        placeholder: "请选择出生日期",
      },
      {
        key: "email",
        type: "text",
        label: "邮箱",
        isMust: false, //是否必填
        value: "",
        verify: [this.getReg("email")],
      },
      {
        key: "position",
        type: "text",
        label: "店内职位",
        isMust: false, //是否必填
        value: "",
      },
      {
        key: "sa_storeids",
        type: "route",
        path: "/select/store/store",
        isRadio: false,
        label: "所属门店",
        isMust: false, //是否必填
        value: [],
        showValue: [],
      },
    ];
    if (options.data) {
      let data = JSON.parse(options.data);
      this.sys_enterprise_hrid = data.sys_enterprise_hrid;
      form = form.map((v) => {
        switch (v.key) {
          case "sa_storeids":
            v.showValue = data.storenames ? data.storenames.split(",") : [];
            v.value = data.sa_storeids || [];
            break;
          default:
            v.value = data[v.key] || "";
            break;
        }
        if (data.status == "启用" && v.isMust) v.disabled = true;
        return v;
      });
      /*  if (data.iswechatbind) form.splice(6, 0, {
                 key: "roleids",
                 type: "route",
                 path: "/select/roleid/roleid?sys_enterprise_hrid=" + data.sys_enterprise_hrid,
                 isRadio: false,
                 label: "角色",
                 isMust: true,//是否必填
                 value: data.roleids || [],
                 showValue: data.rolenames ? data.rolenames.split(",") : [],
             }) */
    }
    this.form = form;
  },
  methods: {
    isUncomplete(uncomplete) {
      this.uncomplete = uncomplete;
    },
    interrupt(item, selected, index) {
      switch (item.key) {
        default:
          item.showValue = selected.showValue || [];
          item.value = selected.value || [];
          this.$refs.form.setItem(index, item, true);
          break;
      }
    },
    submit() {
      this.loading = true;
      let that = this;
      this.$refs.form.submit().then((data) => {
        data.sa_storeids = data.sa_storeids.value;
        if (data.roleids) data.roleids = data.roleids.value;
        this.$Http
          .basic({
            id: 20240410164102,
            content: {
              sys_enterprise_hrid: this.sys_enterprise_hrid,
              ...data,
            },
          })
          .then((res) => {
            this.loading = false;
            console.log("加入团队", res);
            if (this.cutoff(res.msg)) return;
            this.loading = true;

            this.$Http.uploadUserList && this.$Http.uploadUserList();
            if (this.sys_enterprise_hrid == 0) {
              uni.showModal({
                title: "新增成功",
                content: "人员新增成功!是否立即查看",
                cancelText: "返回",
                confirmText: "查看",
                success: ({ confirm }) => {
                  if (confirm) {
                    uni.redirectTo({
                      url:
                        "/team/userCenter/personal?id=" +
                        res.data.sys_enterprise_hrid,
                    });
                  } else {
                    uni.navigateBack();
                  }
                },
              });
            } else {
              this.$Http.uploadUserDetail && this.$Http.uploadUserDetail();
              uni.showToast({
                title: "编辑成功",
                icon: "none",
                duration: 1500,
                mask: true,
              });
              setTimeout(() => {
                uni.navigateBack();
              }, 800);
            }
          });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.footer {
  position: fixed;
  bottom: 0;
  width: 100vw;
  height: 65px;
  background: #ffffff;
  box-shadow: 0px -2px 6px 1px rgba(0, 0, 0, 0.16);
  box-sizing: border-box;
  padding: 5px 10px;
  z-index: 999;

  .add {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 45px;
    background: #c30d23;
    border-radius: 5px;
    font-family: PingFang SC, PingFang SC;
    font-size: 14px;
    color: #ffffff;
  }

  .forbidden {
    opacity: 0.6;
  }
}
</style>