<template>
  <details open class="form-table-style mb30">
    <summary
      :class="{
        'point-font2': pageRole == 'adm',
        'point-font': pageRole == 'government',
      }"
    >
      <p class="summary-style pl10">기본정보</p>
    </summary>
    <div class="pt10 pb10">
      <table class="form-table2 mb10">
        <colgroup>
          <col width="15%" />
          <col width="85%" />
        </colgroup>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            기업명
          </th>
          <td colspan="3">
            <span class="mr10"> {{ rvwMttrVO["entNm"] }}</span>
            <button
              v-show="pageNm != 'entInfo'"
              class="small-btn darkg-border-btn"
              @click="fnMove(rvwMttrVO.entId)"
            >
              기업정보 보기
            </button>
          </td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            투자상담명
          </th>
          <td>{{ rvwMttrVO["ttl"] }}</td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            유형
          </th>
          <td>{{ rvwMttrVO.type }}</td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            접수자
          </th>
          <td>
            {{ rvwMttrVO["clrNm"] }}
          </td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            접수일자
          </th>
          <td>{{ rvwMttrVO["rcptDt"] }}</td>
        </tr>
        <tr>
          <th
            :class="{
              'point-font2': pageRole == 'adm',
              'point-font': pageRole == 'government',
            }"
          >
            첨부파일
          </th>
          <td>
            <template v-if="this.pageAuth.fileDwnldAuthrt == 'Y'">
              <template
                v-if="rvwMttrVO.fileList != null && rvwMttrVO.fileList != ''"
              >
                <ul v-for="(item, idx) in rvwMttrVO.fileList" :key="idx">
                  <li @click="downloadFile(item)">
                    {{ item.fileNm + "." + item.extnNm }}
                  </li>
                </ul>
              </template>
              <p v-else class="data-none">등록된 첨부파일이 없습니다.</p>
            </template>
          </td>
        </tr>
      </table>
      <div class="gd-12 pd0">
        <label for="" class="point-font mb10" style="font-size: 1.6rem">
          접수내용
        </label>
        <textarea
          name=""
          id=""
          class="content"
          :value="rvwMttrVO['rcptCn']"
          disabled
        ></textarea>
      </div>
    </div>
  </details>
  <details open class="form-table-style mb30">
    <summary
      :class="{
        'point-font2': pageRole == 'adm',
        'point-font': pageRole == 'government',
      }"
    >
      <p class="summary-style pl10">진행단계 및 지원방안</p>
    </summary>
    <div class="pt10 pb10">
      <template
        v-if="
          rvwMttrVO.prgrsList != null &&
          rvwMttrVO.prgrsList.length > 0 &&
          rvwMttrVO.prgrsList[0].picId != null
        "
      >
        <div v-for="(prgrs, index) of rvwMttrVO.prgrsList" :key="index">
          <table class="form-table2 mb10">
            <colgroup>
              <col width="15%" />
              <col width="85%" />
            </colgroup>
            <tr>
              <th
                :class="{
                  'point-font2': pageRole == 'adm',
                  'point-font': pageRole == 'government',
                }"
              >
                담당자
              </th>
              <td>{{ prgrs["picNm"] }}</td>
            </tr>
            <tr>
              <th
                :class="{
                  'point-font2': pageRole == 'adm',
                  'point-font': pageRole == 'government',
                }"
              >
                진행단계
              </th>
              <td>
                <span v-show="prgrs['prgrsCrs'] == 0">접수전</span>
                <span v-show="prgrs['prgrsCrs'] == 1">처리중</span>
                <span v-show="prgrs['prgrsCrs'] == 2">완료</span>
              </td>
            </tr>
            <tr>
              <th
                :class="{
                  'point-font2': pageRole == 'adm',
                  'point-font': pageRole == 'government',
                }"
              >
                진행날짜
              </th>
              <td>{{ prgrs["prgrsDt"] }}</td>
            </tr>
          </table>
          <div class="gd-12 pd0">
            <label for="" class="point-font mb10" style="font-size: 1.6rem">
              지원방안
            </label>
            <textarea
              class="content"
              v-model="prgrs['sprtMthd']"
              disabled
            ></textarea>
          </div>
        </div>
      </template>
      <div v-else>
        <table class="form-table2 mb10">
          <tbody>
            <tr>
              <td class="text-ct data-none">
                등록된 정보가 존재하지 않습니다.
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </details>
</template>

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

// api
import { rvwMttrDetailProc } from "../../../resources/api/rvwMttr";

export default {
  props: {
    pageNm: {
      type: String,
      default: null,
    },
    pageId: {
      type: String,
    },
  },
  data() {
    return {
      pageRole: this.$store.state.userType,
      pageAuth: JSON.parse(localStorage.getItem("vuex")).pageAuth, // 페이지 권한

      rvwMttrVO: {},
      rvwMttrTypeList: [],
    };
  },
  created() {
    this.axiosViewDetail();
  },
  methods: {
    // axios: 조회(상세)
    async axiosViewDetail() {
      // 데이터 세팅
      const data = { rvwMttrId: this.pageId };
      // 실행
      try {
        const response = await rvwMttrDetailProc(data);
        this.rvwMttrVO = response.data.data.rvwMttrVO;
        this.rvwMttrTypeList = response.data.data.rvwMttrTypeList;
        this.$emit("fnSaveVO", this.rvwMttrVO, this.rvwMttrTypeList);
      } catch (error) {
        const errorData = error.response.data;
        if (errorData.message != null && errorData.message != "") {
          alert(error.response.data.message);
        } else {
          alert("에러가 발생했습니다.\n관리자에게 문의해주세요.");
        }
      }
    },

    // 기업 정보로 이동
    fnMove(entId) {
      this.$router.push({
        path: this.$store.state.userType + "/companyInfo/view.page",
        query: { pageId: entId },
      });
    },
    // 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("파일 다운로드 중 오류가 발생했습니다.");
      }
    },
  },
};
</script>
