<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Qna_view
 */


class Qna_view extends CI_Controller
{

    private $CI;

    function __construct()
    {
        $this->CI = & get_instance();

    }

	function main()
	{

		if ( ! $this->CI->input->post_get('token')) {
			$this->CI->apilib->make_error("토큰 값이 넘어오지 않았습니다");
        }
		$token = $this->CI->input->post_get('token');


        $app_id = (int) trim($this->CI->input->post_get('app_id'));
        if (empty($app_id)) {
			$this->CI->apilib->make_error("어플 아이디가 넘어오지 않았습니다");
        }

        $qna_id = (int) trim($this->CI->input->get('qna_id'));
        if (empty($qna_id)) {
			$this->CI->apilib->make_error("문의사항 고유 아이디가 넘어오지 않았습니다");
        }

		$sessionuser = $this->CI->User_model->get_by_token($app_id, $token);
		if ( ! element('user_id', $sessionuser)) {
			$this->CI->apilib->make_error("로그인 한 회원만 접근이 가능합니다");
		}

		$user_id = element('user_id', $sessionuser);

		$this->CI->load->model(array('Qna_model', 'Upload_file_model'));

        $where = array(
			'app_id' => $app_id,
			'qna_id' => $qna_id,
		);
		$result = $this->CI->Qna_model->get_one('', '', $where);
		if ( ! element('qna_id', $result)) {
			$this->CI->apilib->make_error("존재하지 않는 문의사항입니다");
		}
		$uploadwhere = array(
			'origin_table' => 'qna',
			'origin_id' => element('v_id', $result),
		);
		$file = $this->CI->Upload_file_model->get('', '', $uploadwhere, '', '', 'ufi_id', 'ASC');
		$files = array();
		if ($file && is_array($file)) {
			foreach ($file as $key => $value) {
				$files[] = site_url('uploads/files/' . element('ufi_filename', $value));
			}
		}


		$arr = array(
			'result' => 'ok',
			'token' => $token,
			'app_id' => $app_id,
			'qna_id' => html_escape(element('qna_id', $result)),
			'user_id' => html_escape(element('user_id', $result)),
			'user_name' => html_escape(element('user_name', $result)),
			'qna_title' => html_escape(element('qna_title', $result)),
			'qna_content' => nl2br(html_escape(element('qna_content', $result))),
			'qna_answer' => nl2br(html_escape(element('qna_answer', $result))),
			'files' => $files,
			'created_at' => html_escape(element('created_at', $result)),
			'answer_at' => html_escape(element('answer_at', $result)),
			'has_answer' => element('has_answer', $result),
		);
		return $arr;

	}


}
