<template>
  <details open class="form-table-style mb30">
    <summary
      :class="{
        'point-font2': pageRole == 'adm',
        'point-font': pageRole == 'government',
      }"
    >
      <template v-if="pageNm == 'entInfo'">
        <p class="summary-style pl10">투자상담</p>
      </template>
      <template v-else>
        <p class="summary-style pl10">기본정보</p>
      </template>
    </summary>
    <div class="pt10 pb10">
      <table class="form-table2 mb10">
        <colgroup>
          <col width="15%" />
          <col width="35%" />
          <col width="15%" />
          <col width="35%" />
        </colgroup>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            상담요지
          </th>
          <td colspan="3">
            {{ ivstDscsnVO["dscsnOtln"] }}
          </td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            제목
          </th>
          <td colspan="3">
            {{ ivstDscsnVO["ttl"] }}
          </td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            일시
          </th>
          <td colspan="3">
            {{ ivstDscsnVO["dt"] }}
          </td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            장소
          </th>
          <td colspan="3">
            {{ ivstDscsnVO["plc"] }}
          </td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            기업명
          </th>
          <td colspan="3">
            <span class="mr10">{{ ivstDscsnVO["entNm"] }}</span>

            <button
              v-show="pageNm != 'entInfo'"
              class="small-btn darkg-border-btn"
              @click="fnMove(ivstDscsnVO.entId)"
            >
              기업정보 보기
            </button>
          </td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            참석자
          </th>
          <td colspan="3">
            {{ ivstDscsnVO["prtpnt"] }}
          </td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            상담공무원
          </th>
          <td colspan="3">
            {{ ivstDscsnVO["dscsnPbofc"] }}
          </td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            주요내용
          </th>
          <td colspan="3">
            {{ ivstDscsnVO["mainCn"] }}
          </td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            첨부파일
          </th>
          <td colspan="3">
            <div v-if="ivstDscsnVO['fileMngId'] != null">
              <div v-for="(file, index) of ivstDscsnVO.fileList" :key="index">
                <div @click="downloadFile(file)">
                  {{ file["fileNm"] }}.{{ file["extnNm"] }}
                </div>
              </div>
            </div>
            <p v-else class="data-none">등록된 첨부파일이 없습니다.</p>
          </td>
        </tr>
        <template v-if="pageNm == 'entInfo'">
          <tr>
            <th
              :class="{
                'point-font2': pageRole == 'adm',
                'point-font': pageRole == 'government',
              }"
            >
              입주의향
            </th>
            <td>
              <span v-show="ivstDscsnVO['mvnInten'] == 0">의향없음</span>
              <span v-show="ivstDscsnVO['mvnInten'] == 1">의향있음</span>
              <span v-show="ivstDscsnVO['mvnInten'] == 2">검토중</span>
            </td>
          </tr>
          <tr>
            <th
              :class="{
                'point-font2': pageRole == 'adm',
                'point-font': pageRole == 'government',
              }"
            >
              MOU의향
            </th>
            <td>
              <span v-show="ivstDscsnVO['mouInten'] == 0">의향없음</span>
              <span v-show="ivstDscsnVO['mouInten'] == 1">의향있음</span>
              <span v-show="ivstDscsnVO['mouInten'] == 2">검토중</span>
            </td>
          </tr>
        </template>
        <template v-else>
          <tr>
            <th
              :class="{
                'point-font2': pageRole == 'adm',
                'point-font': pageRole == 'government',
              }"
            >
              입주의향
            </th>
            <td>
              <span v-show="ivstDscsnVO['mvnInten'] == 0">의향없음</span>
              <span v-show="ivstDscsnVO['mvnInten'] == 1">의향있음</span>
              <span v-show="ivstDscsnVO['mvnInten'] == 2">검토중</span>
            </td>
          </tr>
          <tr>
            <th
              :class="{
                'point-font2': pageRole == 'adm',
                'point-font': pageRole == 'government',
              }"
            >
              MOU의향
            </th>
            <td>
              <span v-show="ivstDscsnVO['mouInten'] == 0">의향없음</span>
              <span v-show="ivstDscsnVO['mouInten'] == 1">의향있음</span>
              <span v-show="ivstDscsnVO['mouInten'] == 2">검토중</span>
            </td>
          </tr>
        </template>
      </table>
    </div>
  </details>

  <!-- 검토사항 -->
  <RvwMttrList
    :data="ivstDscsnVO"
    :pageNm="pageNm"
    :pageId="pageId"
    :pageAuth="pageAuth"
    :pageRole="pageRole"
    :entId="this.ivstDscsnVO.entId"
    @fnIvstDscsnView="fnViewDetail"
  />
</template>

<script>
import defaultAxios from "../../../resources/js/defaultAxios";
import RvwMttrList from "./RvwMttrList.vue";

// api
import { ivstDscsnDetailProc } from "../../../resources/api/ivstDscsn";

export default {
  components: {
    RvwMttrList: RvwMttrList,
  },
  props: {
    pageNm: {
      type: String,
      default: null,
    },
    pageId: {
      type: String,
    },
    pageAuth: {
      type: String,
    },
    pageRole: {
      type: String,
    },
  },
  data() {
    return {
      ivstDscsnVO: {},
    };
  },
  created() {
    this.fnViewDetail();
  },
  methods: {
    // axios: 조회(상세)
    async fnViewDetail() {
      // 데이터 세팅
      const data = { ivstDscsnId: this.pageId };
      // 실행
      try {
        const response = await ivstDscsnDetailProc(data);
        this.ivstDscsnVO = response.data.data;
        this.$emit("fnSaveVO", this.ivstDscsnVO);
      } catch (error) {
        const errorData = error.response.data;
        if (errorData.message != null && errorData.message != "") {
          alert(error.response.data.message);
        } else {
          alert("에러가 발생했습니다.\n관리자에게 문의해주세요.");
        }
      }
    },
    // axios: 첨부파일 다운로드
    async downloadFile(file) {
      try {
        const response = await defaultAxios({
          url: "/sys/file/fileDownload.json", // URL 경로 확인
          method: "post",
          headers: {
            "Content-Type": "application/json; charset=UTF-8",
            Authorization: this.$store.state.authorization,
          },
          data: file.fileId,
          responseType: "blob",
        });
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", file.fileNm + "." + file.extnNm);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        window.URL.revokeObjectURL(url);
      } catch (error) {
        alert("파일 다운로드 중 오류가 발생했습니다.");
      }
    },

    // 기업 정보로 이동
    fnMove(entId) {
      this.$router.push({
        path: "/invest/" + this.$store.state.userType + "/companyInfo/view.page",
        query: { pageId: entId },
      });
    },
  },
};
</script>