<template>

  <div class="contents">
    <div class="contents_wrap">
      <div class="top_wrap">
        <h3 class="title">메시지 차단</h3>
        <p class="breadcrumb">리스크관리 &gt; 메시지 차단</p>
      </div>
<!--      <form autocomplete="off" class="search_form">-->
        <div class="search_wrap">
          <div class="input_box">
            <label for="regId" class="label">등록자</label>
            <input class="search-box" type="text" id="regId" 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 class="input_box">
            <label for="word" class="label">차단메시지</label>
            <input class="search-box" type="text" id="word" placeholder="검색어 입력" v-model.trim="grid.params.word"
                   v-on:keydown.enter.prevent="search" maxlength="50"/>
          </div>
          <button type="button" class="button grey" @click="search">조회</button>
        </div>
<!--      </form>-->
      <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-msg-pop ref="InsertMsgPop"></insert-msg-pop>
    <intrcp-msg-detail ref="IntrcpMsgDetail"></intrcp-msg-detail>
    <common-modal ref="commonModal"></common-modal>
  </div>
</template>

<script>
import customGrid from '@/components/CustomGrid';
import InsertMsgPop from '../components/InsertMsgPop';
import IntrcpMsgDetail from '../components/IntrcpMsgDetail';
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,
      options: [
        {text: '20', value: 20},
        {text: '50', value: 50},
        {text: '100', value: 100}
      ],
      blckRsnCd: '',
      grid: {
        url: '/api/v1/bo/riskMgt/msg/intrcpList',
        perPageRows: 20,
        pagination: true,
        isCheckbox: false,
        initialRequest: false,
        addCls: 'box_OFvis',


        header: [
          [
            {header: 'NO', childNames: []},
            {header: '일련번호', align: 'center', childNames: []},
            {header: '차단메시지', childNames: []},
            {header: '차단여부', childNames: []},
            {header: '마지막 수정일', childNames: []},
            {header: '차단사유', childNames: []},
            {header: '등록자', childNames: []}
          ]
        ],

        columns: [
          {name: 'no', header: 'NO', align: 'center', width: '5%'},
          {name: 'seqNo', header: '일련번호', align: 'center', width: '19%', hidden: true},
          {
            name: 'word', header: '차단메시지', align: 'center', width: '19%',
            renderer: {type: CustomATagRenderer, options: {callback: this.msgDetailPop}}
          },
          {name: 'blckYn', header: '차단여부', align: 'center', width: '19%'},
          {name: 'lastChgDt', header: '마지막 수정일', align: 'center', width: '19%'},
          {name: 'blckRsnCd', header: '차단사유', align: 'center', width: '19%'},
          {name: 'regId', header: '등록자', width: '19%', cls: 'td_line'}
        ],
        noDataStr: '검색 결과가 없습니다.',
        params: {
          regId: '',
          blckRsnCd: '',
          word: '',
          seqNo: '',
          blckYn: '',
        },
        excelHeader: []
      }
    };
  },
  components: {
    customGrid: customGrid,
    InsertMsgPop,
    IntrcpMsgDetail,
    commonModal,
  },
  destroyed() {

  },
  created() {
    const getCondition = this.$store.getters['searchcondition/getSearchCondition'];
  },
  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.pagePerRows = this.perPageCnt
      this.$refs.table.search(this.grid.params, isKeep);
      this.sendStoreData();
    },
    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
      });

      const getCondition = this.$store.getters['searchcondition/getSearchCondition'];
    },
    ModalOpen: function (target) {
      this.$refs.InsertMsgPop.ModalOpen(target);
    },
    msgDetailPop(props) {
      this.$refs.IntrcpMsgDetail.IntrcpMsgDetailModalOpen(props);
    },
    msgAlertModalOpen(props) {
      this.$refs.commonModal.alertModalOpen(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>