<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 >
            <th>충전금액</th>
            <td>
              <div class="input_add">
              <input
                type="radio"
                name="nmineeDivCd"
                value="01"
                id="popup_radio5"
                v-model="nmineeDivCd"
              />
              <label for="popup_radio5">소멸금액 <span style="font-size: 14px;">(다음달 소멸되는 충전금액입니다)</span></label>
              <input  
                type="number" 
                placeholder="소멸 충전 금액 입력" 
                v-model="chrgAmtE" 
                v-bind:disabled ="nmineeDivCd=='02'"
              />
              <input
                type="radio"
                name="nmineeDivCd"
                value="02"
                id="popup_radio6"
                v-model="nmineeDivCd"
              />
              <label for="popup_radio6">이월금액 <span style="font-size: 14px;">(다음달 정액 요금에 이월되는 충전금액입니다)</span></label>
              <input 
                class="search-box" 
                type="number"  
                placeholder="이월 충전 금액 입력" 
                v-model="chrgAmtC" 
                v-bind:disabled ="nmineeDivCd=='01'"
              />
              </div>
            </td>
          </tr>
        </tbody>
        </table>
        <div class="popup-btn2">
        <button class="btn-pcolor" @click="saveChrg()">저장</button>
        <button class="btn-default" @click="insertChrgClose()">취소</button>
      </div>
    </div>
      <user-list-pop ref="userListPop" :send-data="childData" @event-data="setChildData" />
      <common-modal ref="commonModal"/>
  </div>
</template>

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

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: '',

      nmineeDivCd: '01',
      chrgAmtE: '0',
      chrgAmtC: '0',
      charDiv: '',
      userId: '',
      custNm: '',
      userSttusCd: '',
      custSeq : '',
      chrgSeq : '',
      userTpCd: '',
      userSeq: '',

      row: {},

    }
  },
   components: {
    vuejsDatepicker,
    UserListPop,
      CommonModal,
  },
  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() {
      this.formReset();
      var dimmed = document.getElementsByClassName('insertChrgModal');
      for (var i = 0; i < dimmed.length; i++) {
        dimmed[i].style.display = 'none';
      }

    },
    formReset() {
      Object.assign(this.$data, this.$options.data());
    },
    searchIdPop(){
      this.$refs.userListPop.UserListPopOpen();
    },
    setChildData(data) {
      this.userId = data.userId;
      this.custNm = data.custNm;;
      this.userSttusCd = data.userSttusCd;
      this.custSeq = data.custSeq;
      this.userTpCd = data.userTpCd;
      this.userSeq = data.userSeq;
    },

    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')) -1)
        initEndDate.setMonth(Number(moment(initEndDate).format('MM')) + 100)


        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: this.startDate, from: initEndDate};
          if (this.endDate !== initEndDate) {
            this.disabledSDate = {from: this.endDate};
            this.disabledSDate = {to: initStartDate, from: this.endDate};
          }
        }
      }
    },
    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');
      }
    },
    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'))-1);
      this.startDate = initStartDate;
    },
    initSetEndDate() {
      let initEndDate = new Date();
      initEndDate.setMonth(Number(moment(initEndDate).format('MM')) +1);
      this.endDate = initEndDate;
    },
    saveChrg(){
      var chrgAmt =0;
      if (this.nmineeDivCd =='01') {
         this.chrgDiv = 'E';
         chrgAmt = this.chrgAmtE
      }else{
         this.chrgDiv='C';
         chrgAmt = this.chrgAmtC
      }


      if (chrgAmt<=0) {
        this.row.title = '경고';
				this.row.msg1 = '충전금을 입력해주세요.';
				this.$parent.alertInsert(this.row);
        return false;
      }

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

      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>
