<template>
  <div class="contents">
    <div class="contents_wrap">
      <div class="top_wrap">
        <h3 class="title">권한 관리</h3>
        <p class="breadcrumb">시스템관리 &gt; 권한 관리</p>
      </div>
      <div class="info">
        <div class="title">권한 수정</div>
      </div>
      <div class="table table_form">
        <table>
          <tbody>
          <tr class="tr_input w30">
            <th>권한명</th>
            <td colspan="5"><input type="text" v-model.trim="authNm" ref="_authNm" maxlength="100"></td>
          </tr>
          <tr class="tr_input w75">
            <th>권한 코드</th>
            <td colspan="2"><input type="text" v-model.trim="authCd" ref="_authCd" disabled></td>
            <th>상태</th>
            <td class="td_radio" colspan="2">
              <input type="radio" name="state" value="01" id="right_radio1" v-model="stat" checked>
              <label for="right_radio1">사용</label>
              <input type="radio" name="state" value="02" id="right_radio2" v-model="stat">
              <label for="right_radio2">정지</label>
            </td>
          </tr>
          <tr class="tr_input w100">
            <th>권한 설명</th>
            <td colspan="5"><input type="text" v-model="authDesc" maxlength="100"></td>
          </tr>
          <tr class="tr_checkbox">
            <td class="check" rowspan="2">
              <p>메뉴 권한 체크</p>
              <input type="checkbox" id="right_check0" v-model="checkedAuthMenuAll">
              <!--<div class="label_group">
                <label for="right_check0"></label>
                <label for="right_check0">전체 체크</label>
              </div>-->
            </td>
            <td class="check">
              <p>고객관리</p>
              <input type="checkbox" id="right_check1" value="2001" v-model="checkedAuthMenu">
              <div class="label_group">
                <label for="right_check1"></label>
                <label for="right_check1">청약고객관리</label>
              </div>
            </td>
            <td class="check">
              <p>유치현황관리</p>
              <div class="label_group">
                <tr>
                  <td class="td_radio">
                    <input type="radio" name="userStat" value="2002" id="channel_radio1" v-model="channelAuth">
                    <label for="channel_radio1">유치채널 현황</label>
                  </td>
                </tr>
              </div>
              <div class="label_group">
                <tr>
                  <td class="td_radio">
                    <input type="radio" name="userStat" value="2003" id="channel_radio2" v-model="channelAuth">
                    <label for="channel_radio2">유치현황 관리(유치채널)</label>
                  </td>
                </tr>
              </div>
      </td>
      <td class="check">
        <p>서비스관리</p>
        <input type="checkbox" id="right_check4" value="2004" v-model="checkedAuthMenu">
        <div class="label_group">
          <label for="right_check4"></label>
          <label for="right_check4">080수신거부 인증코드 조회</label>
        </div>
      </td>
      <td class="check">
        <p>정산</p>
        <input type="checkbox" id="right_check5" value="2005" v-model="checkedAuthMenu">
        <div class="label_group">
          <label for="right_check5"></label>
          <label for="right_check5">정산이력</label>
        </div>
      </td>
      <td class="check">
        <p>채널관리</p>
        <input type="checkbox" id="right_check6" value="2006" v-model="checkedAuthMenu">
        <div class="label_group">
          <label for="right_check6"></label>
          <label for="right_check6">알림톡 템플릿 관리</label>
        </div>
      </td>
      </tr>
      <tr>
        <td class="check">
          <p>발신번호관리</p>
          <input type="checkbox" id="right_check7" value="2007" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check7"></label>
            <label for="right_check7">발신프로필 관리</label>
          </div>
          <input type="checkbox" id="right_check8" value="2008" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check8"></label>
            <label for="right_check8">문자 발신번호 관리</label>
          </div>
          <input type="checkbox" id="right_check9" value="2009" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check9"></label>
            <label for="right_check9">발신번호 승인</label>
          </div>
        </td>
        <td class="check">
          <p>모니터링</p>
          <input type="checkbox" id="right_check10" value="2010" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check10"></label>
            <label for="right_check10">발송내역</label>
          </div>
          <input type="checkbox" id="right_check11" value="2011" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check11"></label>
            <label for="right_check11">실시간발송현황</label>
          </div>
        </td>
        <td class="check">
          <p>리스크관리</p>
          <input type="checkbox" id="right_check12" value="2012" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check12"></label>
            <label for="right_check12">발신번호 차단</label>
          </div>
          <input type="checkbox" id="right_check13" value="2013" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check13"></label>
            <label for="right_check13">080수신번호 차단</label>
          </div>
          <!-- <input type="checkbox" id="right_check14" value="2014" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check14"></label>
            <label for="right_check14">메시지 차단</label>
          </div> -->
          <!-- <input type="checkbox" id="right_check15" value="2015" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check15"></label>
            <label for="right_check15">차단 내역</label>
          </div> -->
        </td>
        <td class="check">
          <p>발송통계</p>
          <input type="checkbox" id="right_check16" value="2016" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check16"></label>
            <label for="right_check16">날짜별 통계</label>
          </div>
          <input type="checkbox" id="right_check17" value="2017" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check17"></label>
            <label for="right_check17">사업자별 통계</label>
          </div>
        </td>
        <td class="check">
          <p>시스템 관리</p>
          <input type="checkbox" id="right_check18" value="2018" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check18"></label>
            <label for="right_check18">관리자/유치채널 관리</label>
          </div>
          <input type="checkbox" id="right_check19" value="2019" v-model="checkedAuthMenu">
          <div class="label_group">
            <label for="right_check19"></label>
            <label for="right_check19">권한 관리</label>
          </div>
        </td>
      </tr>
      </tbody>
      </table>
    </div>
    <div class="pop-btn2">
      <button class="btn-default" type="button" @click="authModifyCancel()">취소</button>
      <button class="btn-pcolor" type="button" @click="authModifySave()">저장</button>
    </div>
  </div>
  <common-modal ref="commmonModal"></common-modal>
  </div>
</template>

<script>

import sysMgtApi from "../service/sysMgtApi.js";
import {utils_mixin, chkPattern2} from '../service/mixins';
import commonModal from "../components/commonModal";

export default {
  name: 'authModify',
  mixins: [utils_mixin, chkPattern2],
  data() {
    return {
      row: {},
      authType: [],
      authNm: "",
      authCd: "",
      authDesc: "",
      stat: "",
      defaultAuthMenu: ["2001", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2015", "2016", "2017", "2018", "2019"],
      checkedAuthMenu: [],
      channelAuth: "",
    };
  },
  props: {
    targetAuthCd: {
      type: String,
      default: "",
    }
  },
  components: {
    commonModal,
  },
  created() {
    //this.setCodeData();
    this.authDetail();
  },
  destroyed() {

  },
  mounted() {

  },
  methods: {
    doValidate() {

      // 필수 등록정보 체크
      if (this.isNull(this.authNm)) {
        this.row.title = '시스템관리';
        this.row.msg1 = '권한명을 입력해 주세요.';
        this.row.focusTaget = '1';
        this.$refs.commmonModal.alertSysModalOpen(this.row);
        return false;
      }

      if (this.isNull(this.authCd)) {
        this.row.title = '시스템관리';
        this.row.msg1 = '권한 코드를 입력해 주세요.';
        this.row.focusTaget = '2';
        this.$refs.commmonModal.alertSysModalOpen(this.row);
        return false;
      }

      if (this.authCd.length > 5) {
        this.row.title = '시스템관리';
        this.row.msg1 = '권한코드는 영문과 숫자포함 최대4자리까지 입력해주세요.';
        this.row.focusTaget = '2';
        this.$refs.commmonModal.alertSysModalOpen(this.row);
        return false;
      }

      if (this.isNull(this.stat)) {
        this.row.title = '시스템관리';
        this.row.msg1 = '상태를 체크해 주세요.';
        this.$refs.commmonModal.alertSysModalOpen(this.row);
        return false;
      }

      if (this.checkedAuthMenu.length == 0) {
        this.row.title = '시스템관리';
        this.row.msg1 = '메뉴 권한 체크를 해주세요.';
        this.$refs.commmonModal.alertSysModalOpen(this.row);
        return false;
      }

      return true;


    },
    authModifyCancel() {
      // 권한리스트 페이지로 이동
      this.$router.push({name: 'authList'});

    },
    async authDetail() {
      this.row.authCd = this.$route.params.targetAuthCd;
      try {
        const response = await sysMgtApi.authDetail(this.row);
        const result = response.data;

        if (result != null && result.retCode == "0000") {
          this.authCd = result.data.authCd;
          this.authNm = result.data.authNm;
          this.authDesc = result.data.authDesc;
          this.stat = result.data.authStat;

          // 메뉴리스트 처리
          var dataList = result.data.list;
          var rsArr = [];
          for (var i = 0; i < dataList.length; i++) {
            if (dataList[i].upperMenuNo != null || dataList[i].upperMenuNo != "") {
              if (dataList[i].menuNo === '2002' || dataList[i].menuNo === '2003') {
                this.channelAuth = dataList[i].menuNo;
              } else {
                this.checkedAuthMenu.push(dataList[i].menuNo);
              }

            }
          }

        } else {
          this.row.title = '시스템관리';
          this.row.msg1 = '실패 하였습니다.';
          this.row.focusTaget = '0';
          this.$refs.commmonModal.alertSysModalOpen(this.row);
          return false;
        }
      } catch (err) {
        this.row.title = '시스템관리';
        this.row.msg1 = '실패 하였습니다.';
        this.row.focusTaget = '0';
        this.$refs.commmonModal.alertSysModalOpen(this.row);
        return false;
      }

    },
    authModifySave() {
      if (this.doValidate()) {
        this.row.title = '시스템관리';
        this.row.msg1 = '권한 수정 저장하시겠습니까?';
        this.row.focusTaget = '0';
        this.$refs.commmonModal.confirmModalOpen(this.row);
        return false;
      }
    },
    async authUpdate() {

      var reqAuthMenuArr = this.checkedAuthMenu;
      var listArr = [];
      var dataMap = {};
      if (this.channelAuth !== '') {
        dataMap.menuNo = this.channelAuth;
        listArr.push(dataMap);
      }
      for (var i = 0; i < reqAuthMenuArr.length; i++) {
        dataMap = {};
        dataMap.menuNo = reqAuthMenuArr[i];
        listArr.push(dataMap);
      }

      this.row.authCd = this.authCd;
      this.row.authNm = this.authNm;
      this.row.authDesc = this.authDesc;
      this.row.stat = this.stat;
      this.row.list = listArr;

      try {
        let response = await sysMgtApi.updateAuth(this.row);
        const result = response.data;
        if (result != null && result.retCode == "0000") {
          // 권한리스트 페이지 이동
          this.$router.push({name: 'authList'});
        } else {
          this.row.title = '시스템관리';
          this.row.msg1 = '실패 하였습니다.';
          this.row.focusTaget = '0';
          this.$refs.commmonModal.alertSysModalOpen(this.row);
          return false;
        }

      } catch (err) {
        //alert("실패 하였습니다.");
        this.row.title = '시스템관리';
        this.row.msg1 = '실패 하였습니다.';
        this.row.focusTaget = '0';
        this.$refs.commmonModal.alertSysModalOpen(this.row);
        return false;
      }

    },
    checkFocus() {
      if (this.row.focusTaget === '1') {
        this.$refs._authNm.focus();
      } else if (this.row.focusTaget === '2') {
        this.$refs._authCd.focus();
      }else if(this.row.focusTaget === '0'){
        this.$router.push({name: 'authList'});
      }
    },
    confirmCalbackFnc(props) {
      if (props.result) {
        this.authUpdate();
      }
    }
  },
  computed: {
    // 체크박스 전체선택 기능
    checkedAuthMenuAll: {
      get: function () {
        return this.defaultAuthMenu.length === this.checkedAuthMenu.length;
      },
      set: function (e) {
        if (e) {
          this.checkedAuthMenu = this.defaultAuthMenu;
          if (this.channelAuth === '') {
            this.channelAuth = '2002';
          }
        } else {
          this.checkedAuthMenu = [];
          this.channelAuth = '';
        }
      }
    }
  }

};
</script>