<template>

  <div class="contents">
    <div class="contents_wrap">
      <div class="top_wrap">
        <h3 class="title">발신번호 차단</h3>
        <p class="breadcrumb">리스크관리 &gt; 발신번호 차단</p>
      </div>
      <div class="search_wrap">
        <div class="group">
          <div class="input_box">
            <label for="regId" class="label">등록자</label>
            <input class="search-box" type="text" id="search" placeholder="검색어 입력" v-model.trim="grid.params.regId"
                   v-on:keydown.enter.prevent="search" v-on:keyup="onlyEng" @input="onlyEng" maxlength="20"/>
          </div>
          <div class="select_box">
            <label for="blckRsnCd" class="label">차단사유</label>
            <select name="" id="blckRsnCd" v-model="blckRsnCd" @keyup="search">
              <option value="" selected>전체</option>
              <option value="01">일반</option>
              <option value="02">대출</option>
              <option value="03">의약품</option>
              <option value="04">도박</option>
              <option value="05">스미싱</option>
              <option value="06">기타</option>
            </select>
          </div>
        </div>
        <div class="group">
          <div class="select_box">
            <label for="right" class="label">차단여부</label>
            <select name="" id="" v-model="blckYn">
              <option value="" selected>전체</option>
              <option value="Y">차단</option>
              <option value="N">해제</option>
            </select>
          </div>
          <div class="select_box">
            <label for="right" class="label">발송타입</label>
            <select name="" id="" v-model="sndblckTpCd">
              <option value="" selected>전체</option>
              <option value="01">공용</option>
              <option value="02">문자</option>
              <option value="03">RCS</option>
            </select>
          </div>
          <div class="input_box">
            <label for="right" class="label">발신번호</label>
            <input class="search-box" type="text" id="search" placeholder="검색어 입력"
                   v-model.trim="grid.params.blckSndrno">
          </div>
          <button type="button" class="button grey" @click="search">조회</button>
        </div>
      </div>
      <div class="info">
        <div class="count">총 <span>{{ totalItems.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') }}</span>건
          <div class="select_box NumberSe">
            <select name="" id="" v-model="perPageCnt" @change="changePerPage()">
              <option v-for="option in options" v-bind:value="option.value" v-bind:key="option.value">{{
                  option.text
                }}
              </option>
            </select>
          </div>
        </div>
        <div class="button_group">
          <button type="button" class="button blue add" @click="ModalOpen()">신규등록</button>
        </div>
      </div>

      <div class="table">
        <custom-grid
            ref="table"
            :totalItems="'totalItems'"
            :url="grid.url"
            :pagePerRows="grid.pagePerRows"
            :initialRequest="grid.initialRequest"
            :pagination="grid.pagination"
            :isCheckbox="grid.isCheckbox"
            :columns="grid.columns"
            :noDataStr="grid.noDataStr"
            :addCls="grid.addCls"
            :header="grid.headder"
        ></custom-grid>
      </div>
    </div>
    <insert-intrcp-pop ref="insertIntrcpPop"></insert-intrcp-pop>
    <intrcp-detail-popup ref="intrcpDetailPopup"></intrcp-detail-popup>
    <common-modal ref="commonModal"></common-modal>
  </div>
</template>

<script>
import customGrid from '@/components/CustomGrid';
import api from '@/service/api.js';
import intrcpDetailPopup from '../components/IntrcpDetailPopup';
import insertIntrcpPop from '../components/InsertIntrcpPop';
import commonModal from "@/components/modal/commonModal";
import {utils_mixin, chkPattern2} from '../service/mixins';

class CustomATagRenderer {
  constructor(props) {
    this.props = props;
    const el = document.createElement('a');
    el.href = 'javascript:void(0);';
    el.className = 'btn_text';
    el.innerText = String(props.colValue)
    this.el = el;
  }

  getElement() {
    return this.el;
  }

  addEvent(selEl) {
    selEl.addEventListener("click", () => {
      const {callback} = this.props["cgrido" + this.props.colName].options;
      callback(this.props);
    });
  }
}

export default {
  name: 'intrcpList',
  mixins: [utils_mixin, chkPattern2],
  data() {
    return {
      totalItems: 0,
      perPageCnt: 50,
      blckRsnCd: '',
      blckYn: '',
      sndblckTpCd: '',
      isFirst:true,
      options: [
        {text: '20', value: 20},
        {text: '50', value: 50},
        {text: '100', value: 100}
      ],
      grid: {
        url: '/api/v1/bo/riskMgt/sendNum/intrcpList',
        pagePerRows: 20,
        //perPage: 50,
        pagination: true,
        isCheckbox: false,
        initialRequest: false,
        addCls: 'box_OFvis',
        header: [
          [
            {header: 'No', childNames: []},
            {header: '발신번호', childNames: []},
            {header: '차단여부', childNames: []},
            {header: '발송타입', childNames: []},
            {header: '마지막 수정일', childNames: []},
            {header: '차단사유', childNames: []},
            {header: '등록자', childNames: []}
          ]
        ],

        columns: [
          {name: 'no', header: 'No', align: 'center', width: '5%'},
          {
            name: 'blcksndrno', header: '발신번호', align: 'center', width: '20%',
            renderer: {type: CustomATagRenderer, options: {callback: this.inDetailPop}}
          },
          {name: 'blckYn', header: '차단여부', align: 'center', width: '15%'},
          {name: 'sndblckTpCd', header: '발송타입', align: 'center', width: '15%', hidden: true},
          {name: 'sndblckTpNm', header: '발송타입', align: 'center', width: '15%'},
          {name: 'lastChgDt', header: '마지막 수정일', align: 'center', width: '15%'},
          {name: 'blckRsnCd', header: '차단사유', width: '15%', cls: 'td_line', hidden: true},
          {name: 'blckRsnNm', header: '차단사유', width: '15%', cls: 'td_line'},
          {name: 'regId', header: '등록자', width: '15%'}
        ],
        noDataStr: '검색 결과가 없습니다.',
        params: {
          regId: '',
          blckRsnCd: '',
          blckRsnNm: '',
          blckYn: '',
          sndblckTpCd: '',
          sndblckTpNm: '',
          blckSndrno: '',
          pagePerRows: '',
          page: ''
        },
        excelHeader: []
      }
    };
  },
  components: {
    customGrid: customGrid,
    intrcpDetailPopup,
    insertIntrcpPop,
    commonModal,
  },
  destroyed() {
    this.grid.params.blckSndrno = '';
    this.grid.params.regId = '';

  },
  created() {
    this.setCodeData();
    // this.formReset();
  },
  mounted() {
    let page = 1;
    const getCondition = this.$store.getters['searchcondition/getSearchCondition'];

    let isKeep = false;
    if (getCondition) {
      this.grid.pagePerRows = getCondition.perPage;
      this.grid.params = getCondition.params;
      page = getCondition.page;
      isKeep = true;
    }
    this.search(isKeep);
  },
  methods: {
    search: function (isKeep) {
      this.grid.params.blckRsnCd = this.blckRsnCd
      this.grid.params.blckYn = this.blckYn
      this.grid.params.sndblckTpCd = this.sndblckTpCd

      if(this.isFirst){
        this.grid.pagePerRows = 50
      }

      this.$refs.table.search(this.grid.params, isKeep);
      this.sendStoreData();
      this.isFirst=false;
    },
    changePerPage: function () { 	// 페이지당 조회할 개수
      this.grid.pagePerRows = this.perPageCnt;
      this.search(true);
    },
    sendStoreData: function () {
      const getP = this.$refs.table.getPagination();
      this.$store.commit('searchcondition/updateSearchCondition', {
        page: getP._currentPage,
        perPage: this.perPageCnt,
        params: this.grid.params
      });

    },
    setCodeData() {
      // 상태 옵션 셋팅.
      api.commCode({'grpCd': 'SVCUSER_STTUS_CD'}).then(response => {
        this.statType = response.data.data.list;
      });
      // 
      api.commCode({'grpCd': 'SVCUSER_TP_CD'}).then(response => {
        this.userType = response.data.data.list;
      });
      //발송타입
      api.commCode({'grpCd': 'SNDBLCK_TP_CD'}).then(response => {
        this.tpType = response.data.data.list;
      });
    },
    ModalOpen: function () {
      this.$refs.insertIntrcpPop.ModalOpen();
    },
    inDetailPop(props) {
      this.$refs.intrcpDetailPopup.IntrcpDetailModalOpen(props);
    },

    alertInsert(props) {
      this.$refs.commonModal.alertModalOpen(props);
    },
    confirmInsert(props) {
      this.$refs.commonModal.confirmModalOpen(props);
    },
    confirmCalbackFnc: function (props) {

    },

  },
  beforeRouteLeave(to, from, next) {
    const getP = this.$refs.table.getPagination();
    this.$store.commit('searchcondition/updateSearchCondition', {
      page: getP._currentPage,
      perPage: this.perPageCnt,
      params: this.grid.params
    });
    // 라우트 하기전 실행
    next();
  }
};
</script>
