<template>
  <div class="contents">
    <div class="contents_wrap">
      <div class="top_wrap">
        <h3 class="title">배치 모니터링</h3>
        <p class="breadcrumb">운영 관리 &gt; 배치 모니터링</p>
      </div>
      <div class="table">
        <custom-grid ref="table" :url="grid.url" :columns="grid.columns"></custom-grid>
      </div>
    </div>
  </div>
</template>

<script>
import customGrid from '@/components/CustomGrid';

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: 'batchList',
  data() {
    return {
      row: {},
      perPageCnt: 50,
      grid: {
        url: '/api/v1/bo/sysMgt/batchList',
        header: [
          [
            { header: '배치ID', childNames: {} },
            { header: '배치명', childNames: {} },
          ],
        ],
        columns: [
          { name: 'batchType', header: '배치유형', align: 'center', width: '4%' },
          { name: 'batchId', header: '배치ID', align: 'center', width: '10%' },
          {
            name: 'batchNm',
            header: '배치명',
            align: 'left',
            width: '15%',
            renderer: {
              type: CustomATagRenderer,
              options: {
                callback: this.batchDetail,
              },
            },
          },
          { name: 'batchCycle', header: '실행일자', align: 'center', width: '7%' },
          { name: 'batchTime', header: '실행시간', align: 'center', width: '6%' },
          { name: 'regDt', header: '배치 수행시간', align: 'center', width: '13%' },
          { name: 'sttusCd', header: '상태', align: 'center', width: '5%' },
          { name: 'errMsg', header: '메시지', align: 'left', width: '40%' },
        ],
      },
    };
  },
  components: {
    customGrid: customGrid,
  },
  created() {
    const getCondition = this.$store.getters['searchcondition/getSearchCondition'];
  },
  destroyed() {
    // this.$store.commit('dataStore/updateDataStore', '');
    this.$store.commit('searchcondition/updateSearchCondition', {
      page: 1,
      perPage: 50,
      params: {
        startDt: '',
        endDt: '',
      },
    });
  },
  mounted() {
    let page = 1;
    const getCondition = this.$store.getters['searchcondition/getSearchCondition'];
    let isKeep = false;
    if (getCondition) {
      this.grid.params = getCondition.params;
      page = getCondition.page;
      isKeep = true;
    }
    this.search(isKeep);
  },

  methods: {
    search: function (isKeep) {
      this.$refs.table.search(this.grid.params, isKeep);
      this.sendStoreData();
    },
    batchDetail(props) {
      this.row.batchId = props.batchId;
      this.$router.push({ path: `/sysMgt/batchDetail/${props.batchId}`, params: this.row });
    },
    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'];
    },
  },
};
</script>
