<template>
  <!-- <div class="wrap bg-wrap"> -->
  <div>
    <div class="dimmed modal29" @click="ModalClose();"></div>
    <div class="popup-wrap modal29">
      <!-- 테스트 ID 생성 -->
      <div class="popup modal29 popup_form">
        <div class="pop-head">
          <h3 class="pop-tit">테스트 ID 생성</h3>
        </div>
        <form autocomplete="off">
          <table>
            <tbody>
            <tr>
              <th>ID</th>
              <td><input type="text" placeholder="아이디 입력" v-model.trim="userId" ref="_userId" @keypress="onlyId"
                         @input="onlyId" minlength="6" maxlength="16"/></td>
            </tr>
            <tr>
              <th>비밀번호</th>
              <td><input type="password" @keypress="onlyPassword" @input="onlyPassword" required minlength="8"
                         maxlength="16" ref="_pwd1" v-model.trim="userPwd1"></td>
            </tr>
            <tr>
              <th>비밀번호 확인</th>
              <td><input type="password" @keypress="onlyPassword" @input="onlyPassword" required minlength="8"
                         maxlength="16" ref="_pwd2" v-model.trim="userPwd2"></td>
            </tr>
            <tr>
              <th>이름</th>
              <td><input type="text" @keypress="onlyText" @input="onlyText" v-model.trim="userNm" ref="_userNm" required
                         maxlength="40"></td>
            </tr>
            <tr>
              <th>휴대폰번호</th>
              <td><input type="text" placeholder="- 자 제외 숫자만 입력" v-model.trim="mdn" @keypress="onlyNum" @input="onlyNum"
                         minlength="10" maxlength="11" ref="_phone"></td>
            </tr>
            <tr>
              <th>이메일</th>
              <td><input type="email" v-model.trim="email" @keypress="onlyEmail" @input="onlyEmail" maxlength="100"
                         ref="_email"></td>
            </tr>
            <tr>
              <th class="center">상태</th>
              <td>
                <input type="radio" name="state" value="01" id="popup_radio1" v-model="userStat">
                <label for="popup_radio1">사용</label>
                <input type="radio" name="state" value="02" id="popup_radio2" v-model="userStat">
                <label for="popup_radio2">정지</label>
              </td>
            </tr>
            </tbody>
          </table>
        </form>
        <div class="popup-btn2">
          <button class="btn-pcolor" @click="doInsert">저장</button>
          <button class="btn-default" @click="ModalClose();">취소</button>
        </div>
      </div>
      <common-modal ref="commmonModal"></common-modal>
    </div>
  </div>
</template>

<script>
import api from '@/service/api';
import custMgtApi from "../service/custMgtApi.js";
import {utils_mixin, chkPattern2} from '../service/mixins';
import lodash from "lodash";
import commonModal from "../components/commonModal";
import ValidationConfirmPop from "@/modules/custMgt/components/ValidationConfirmPop";

export default {
  name: "subsRegPop",
  mixins: [utils_mixin, chkPattern2],
  watch: {
    stat() {
    }
  },
  data() {
    return {
      row: {},
      userId: '',
      name: '',
      mdn: '',
      email: '',
      userStat: '01',
      userNm: "",
      userPwd1: "",
      userPwd2: "",
      code: "",
    }
  },
  model: {
    prop: 'sendData',
    event: 'event-data'
  },
  props: ['sendData'],
  components: {
    commonModal,
  },
  created() {
    this.formReset();
  },
  methods: {
    doPwdValidate() {
      if (this.isNull(this.userPwd1)) {
        this.row.title = '청약고객관리';
        this.row.msg1 = '비밀번호를 입력해 주세요.';
        this.$refs.commmonModal.alertModalOpen(this.row);
        this.$refs._pwd1.focus();
        return false;
      }
      if (this.isNull(this.userPwd2)) {
        this.row.title = '청약고객관리';
        this.row.msg1 = '비밀번호 확인을 입력해 주세요.';
        this.$refs.commmonModal.alertModalOpen(this.row);
        this.$refs._pwd2.focus();
        return false;
      }
      if (!lodash.isEqual(this.userPwd1, this.userPwd2)) {
        this.row.title = '청약고객관리';
        this.row.msg1 = '비밀번호가 일치하지 않습니다.';
        this.$refs.commmonModal.alertModalOpen(this.row);
        this.$refs._pwd2.focus();
        return false;
      }
      const pwdLen = this.bytes(this.userPwd1);
      if (!(pwdLen >= 8 && pwdLen <= 16)) {
        this.row.title = '청약고객관리';
        this.row.msg1 = '비밀번호는 8~16자의 영문, 숫자, 특수문자(!,@, $, %, ^, &, *) 조합이 필요합니다.';
        this.$refs.commmonModal.alertModalOpen(this.row);
        this.$refs._pwd1.focus();
        return false;
      }
      const pEng = /[A-Za-z]/g;	// 영문자
      const pNum = /[0-9]/g;	// 숫자
      const pSpc = /[!@$%^&*]/g; // 특수문자
      if (!(pEng.test(this.userPwd1) && pNum.test(this.userPwd1) && pSpc.test(this.userPwd1))) {
        this.row.title = '청약고객관리';
        this.row.msg1 = '비밀번호는 8~16자의 영문, 숫자, 특수문자(!,@, $, %, ^, &, *) 조합이 필요합니다.';
        this.$refs.commmonModal.alertModalOpen(this.row);
        this.$refs._pwd1.focus();
        return;
      }
      this.row.userPw = this.userPwd1;
      return true;
    },
    doValidate() {
      if (this.isNull(this.userId)) {
        this.row.title = '청약고객관리';
        this.row.msg1 = '아이디를 입력해 주세요.';
        this.$refs.commmonModal.alertModalOpen(this.row);
        this.$refs._userId.focus();
        return false;
      }
      // 청약 ID  길이 체크 6~16
      var userIdlength = this.userId.length;
      if (userIdlength < 6 || userIdlength > 16) {
        this.row.title = '청약고객관리';
        this.row.msg1 = 'ID 형식에 맞지 않습니다.';
        this.row.msg2 = '확인하여 다시 등록 부탁 드립니다.';
        this.row.focusTaget = '1';
        this.$refs.commmonModal.alertModalOpen(this.row);
        return false;
      }

      if (!this.doPwdValidate()) {
        return false;
      }
      if (this.isNull(this.userNm)) {
        this.row.title = '청약고객관리';
        this.row.msg1 = '이름을 입력해 주세요.';
        this.$refs.commmonModal.alertModalOpen(this.row);
        this.$refs._userNm.focus();
        return false;
      }
      if (this.isNull(this.mdn)) {
        this.row.title = '청약고객관리';
        this.row.msg1 = '휴대폰번호를 입력해주세요.';
        this.$refs.commmonModal.alertModalOpen(this.row);
        this.$refs._phone.focus();
        return false;
      }
      const hp = this.mdn;
      if (!this.isNull(hp) && !this.isMobile(hp)) {
        this.row.title = '청약고객관리';
        this.row.msg1 = '휴대폰 번호 형식이 잘못되었습니다. 확인해 주세요.';
        this.$refs.commmonModal.alertModalOpen(this.row);
        this.$refs._phone.focus();
        return false;
      }
      if (this.isNull(this.email)) {
        this.row.title = '청약고객관리';
        this.row.msg1 = '이메일을 입력해주세요.';
        this.$refs.commmonModal.alertModalOpen(this.row);
        this.$refs._email.focus();
        return false;
      }
      const email = this.email;
      if (!this.isNull(email) && !lodash.isEqual(email, '@') && !this.emailCheck(email)) {
        this.row.title = '청약고객관리';
        this.row.msg1 = '이메일 형식이 잘못되었습니다. 확인해 주세요.';
        this.$refs.commmonModal.alertModalOpen(this.row);
        this.$refs._email.focus();
        return false;
      }
      if (this.isNull(this.userStat)) {
        this.row.title = '청약고객관리';
        this.row.msg1 = '상태를 선택 해주세요.';
        this.$refs.commmonModal.alertModalOpen(this.row);
        // this.$refs._auth.focus();
        return false;
      }
      this.row.userId = this.userId;
      this.row.userNm = this.userNm;
      this.row.mdn = hp;
      this.row.userEmail = email;
      this.row.userStat = this.userStat;
      return true;
    },
    // 모달 띄우기
    ModalOpen() {
      // 모달 오픈
      var dimmed = document.getElementsByClassName('modal29');
      for (var i = 0; i < dimmed.length; i++) {
        dimmed[i].style.display = 'block';
      }
      this.formReset();
    },
    // 모달 끄기
    ModalClose() {
      var dimmed = document.getElementsByClassName('modal29');
      for (var i = 0; i < dimmed.length; i++) {
        dimmed[i].style.display = 'none';
      }
    },
    // 저장 후 부모창 호출.
    toComplete() {
      this.getParent('memberList').$refs.table.reloadData();
      this.ModalClose();
    },
    async doInsert() {
      if (this.doValidate()) {

        try {
          const response = await custMgtApi.insertTestId(this.row);
          const result = response.data;
          if (result != null && result.retCode == "0000") {
            this.toComplete();
          } else if (result != null && result.retCode == "1009") {
            this.row.title = '청약고객관리';
            this.row.msg1 = '실패 하였습니다.';
            this.$refs.commmonModal.alertModalOpen(this.row);
          } else if (result != null && result.retCode == "4018") {
            this.row.title = '청약고객관리';
            this.row.msg1 = '중복된 아이디 입니다.';
            this.row.msg2 = '아이디를 다시 확인하여 등록 부탁드립니다.';
            this.$refs.commmonModal.alertModalOpen(this.row);
          }
        } catch (err) {
          this.row.title = '청약고객관리';
          this.row.msg1 = '실패 하였습니다.';
          this.$refs.commmonModal.alertModalOpen(this.row);
        }
      }
    },
    formReset() {
      // this.$refs.adminRegForm.reset();
      Object.assign(this.$data, this.$options.data());
    },

  }
}
</script>