<template>
  <div>
    <div class="dimmed insertChrgModal" @click="insertChrgClose()"></div>
    <!-- 발신번호 등록 (타사업자)-->
    <div class="popup insertChrgModal popup_form register w700">
        <div class="pop-head">
            <h3 class="pop-tit">충전금 등록</h3>
        </div>
        <table style="width: 100%">
        <tbody>
          <tr>
            <th>유저 ID</th>
            <td>
              <div class="input_search">
                <!-- <input class="search-box" type="text" placeholder="아이디 입력" v-model="userId" /> -->
                <input class="search-box" type="text" placeholder="아이디 입력" disabled v-model="userId" />
                <button type="button" class="button grey" @click="searchIdPop">조회</button>
              </div>
            </td>
            
          </tr>
          <tr>
            <th>충전월</th>
            <td>
                <div class="input_box cal" style="width:60%">
                    <div class="term">
                        <span class="custom_input icon_date">
                        <vuejs-datepicker
                            :language="ko"
                            :format="customFormatter"
                            :disabled-dates="disabledSDate"
                            :minimumView="sDateDiv"
                            :maximumView="sDateDiv"
                            v-model="startDate"
                            @selected="selectedStartDate(0)"
                            @closed="closeDate('start')"
                            :picker-options="startDateOptions"
                        ></vuejs-datepicker>
                        </span>
                    ~
                    <span class="custom_input icon_date">
                        <vuejs-datepicker
                            :language="ko"
                            :format="customFormatter"
                            :disabled-dates="disabledEDate"
                            :minimumView="sDateDiv"
                            :maximumView="sDateDiv"
                            v-model="endDate"
                            @selected="selectedEndDate(0)"
                            @closed="closeDate('end')"
                            :picker-options="endDateOptions"
                        ></vuejs-datepicker>
                        </span>
                    </div>
                </div>
            </td>
          </tr>
          <tr v-show="bizrAuthYn !== 'Y'">
            <th>충전금액</th>
            <td>
              <div class="input_add">
              <input
                type="radio"
                name="nmineeDivCd"
                value="01"
                id="popup_radio5"
                v-model="nmineeDivCd"
                @change="changeNmineDiv($event)"
              />
              <label for="popup_radio5">소멸금액</label>
              <input  
                type="number" 
                placeholder="소멸 충전 금액 입력" 
                v-model="chrgAmtE" 
              />
              <input
                type="radio"
                name="nmineeDivCd"
                value="02"
                id="popup_radio6"
                v-model="nmineeDivCd"
                @change="changeNmineDiv($event)"
              />
              <label for="popup_radio6">이월금액</label>
              <input 
                class="search-box" 
                type="number"  
                placeholder="이월 충전 금액 입력" 
                disabled 
                v-model="chrgAmtC" 
              />
              </div>
            </td>
          </tr>
        </tbody>
        </table>
        <div class="popup-btn2">
        <button class="btn-pcolor" @click="saveChrg()">저장</button>
        <button class="btn-default" @click="insertChrgClose()">취소</button>
      </div>
      <user-list-pop ref="userListPop" :send-data="childData" @event-data="setChildData" />
    </div>
  </div>
</template>

<script>
import UserListPop from './UserListPop.vue';
import moment from 'moment';
import custMgtApi from "../service/custMgtApi";

export default {
  name: 'insertChrgModal',

  watch: {
    nmineeDivCd() {},
  },

  data() {
    return {
      childData: 20,
      ko: vdp_translation_ko.js,

      // 달력 데이터
      periodDay: 7, 
      sDateDiv: 'month',
      startDate: new Date(),
      endDate: new Date(),
      startDateOptions: {disabledDate: this.disabledStDate},
      endDateOptions: {disabledDate: this.disabledEdDate},
      startDt: '',
      endDt: '',
      startYear: '',
      startMonth: '',
      endYear: '',
      endMonth: '',

      bizrAuthYn: '',
      nmineeDivCd: '01',
      chrgAmtE: '',
      chrgAmtC: '',

      userId: '',
      custNm: '',
      bRegNo: '',
      bizrAuthYn : '',
      custSeq : '',
      chrgSeq : '',

      row: {},

    }
  },
   components: {
    vuejsDatepicker,
    UserListPop,
  },
  mounted(){
    // 달력 세팅
    let page = 1;
  },
  created() {
    this.setPeriodDay(0);
  },
  methods: {
     // 모달 띄우기
    insertChrgOpen() {
      var dimmed = document.getElementsByClassName('insertChrgModal');
      for (var i = 0; i < dimmed.length; i++) {
        dimmed[i].style.display = 'block';
      }
    },
    // 모달 끄기
    insertChrgClose() {
      var dimmed = document.getElementsByClassName('insertChrgModal');
      for (var i = 0; i < dimmed.length; i++) {
        dimmed[i].style.display = 'none';
      }
    },
    searchIdPop(){
      this.$refs.userListPop.UserListPopOpen();
    },
    setChildData(data) {
      this.userId = data.userId;
      this.custNm = this.fromHtmlEntities(data.custNm);
      this.bRegNo = data.bregNo;
      this.bizrAuthYn = data.bizrAuthYn;
      this.custSeq = data.custSeq;
    },

    search: function (isKeep) {
      this.grid.params.startDt = moment(this.startDate).format('YYYYMM');
      this.grid.params.endDt = moment(this.endDate).format('YYYYMM');
      this.grid.params.searchType1 = this.searchType1;
      this.$refs.table.search(this.grid.params, isKeep);
      this.sendStoreData();
    },

    //달력 셋팅
    setPeriodDay(day) {
      this.periodDay = day;
      this.endDate = new Date();     
      this.initSetStartDate();
      this.initSetEndDate();
      this.closeDate('start');
      this.closeDate('end');
    },

    selectedStartDate(day) {
      if (day != undefined && day != null) {
        this.periodDay = day;
      }
      if (this.startDate > this.endDate) {
        this.startDate = this.endDate;
      }      
    },
    selectedEndDate(day) {
      if (day != undefined && day != null) {
        this.periodDay = day;
      }
    },
    closeDate(type) {
      if (type != undefined && type != null) {
        let initStartDate = new Date();
        let initEndDate = new Date();
        initStartDate.setMonth(Number(moment(initStartDate).format('MM')) )
        initEndDate.setMonth(Number(moment(initEndDate).format('MM')) + 12)


        if (type == 'start') {
          this.disabledSDate = {to: initStartDate, from: new Date()};
          if (this.startDate !== initStartDate) {
            this.disabledEDate = {to: this.startDate, from: initEndDate};

          }
        } else if (type == 'end') {
          this.disabledEDate = {to: initStartDate, from: initEndDate};
          if (this.endDate !== initEndDate) {
            this.disabledSDate = {from: this.endDate};
            this.disabledSDate = {to: initStartDate, from: initEndDate};
          }
        }
      }
    },
    customFormatter: function (date) {
      if (this.sDateDiv == 'month') {
        return moment(date).format('YYYY-MM');
      } else if (this.sDateDiv == 'year') {
        return moment(date).format('YYYY');
      } else {
        return moment(date).format('YYYY-MM-DD');
      }
    },
    gridParamSet() {
      this.grid.params = {
        startMonth: moment(this.startDate).format('YYYYMM'),
        endMonth: moment(this.endDate).format('YYYYMM'),
      }
  },
  initSetStartDate() {
      let initStartDate = new Date();
      initStartDate.setMonth(Number(moment(initStartDate).format('MM')));
      this.startDate = initStartDate;
    },
    initSetEndDate() {
      let initEndDate = new Date();
      initEndDate.setMonth(Number(moment(initEndDate).format('MM')) +12);
      this.endDate = initEndDate;
    },
    saveChrg(){

      let param = {
        userId : this.userId,
        strtYm : this.startDate, 
        endYm : this.endDate, 
        chrgAmt : this.chrgAmtE, 
      }

      custMgtApi.insertChrg(param).then((respose)=> {
          var result = respose.data;
          if (result != null && result.retCode == "0000"){
					this.row.title = '충전금';
					this.row.msg1 = '등록 하였습니다.';
					this.$parent.alertInsert(this.row);
					this.toComplete();
				}
        })
    },

    toComplete(){
			this.$parent.$refs.table.reloadData();
			this.insertChrgClose();
		},
  },
};
</script>
