<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Demandform extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Demandform_model');
    }

    /*!
     * @author LYT <lyt@hainatravel.com>
     * @date   2018-01-23
     * @param  [type]     $coli_sn 订单主表的COLI_SN
     * @param  integer    $form_id 默认值2是0.1版本固定的
     */
    public function index($coli_sn = null, $form_id = 2)
    {
        header("Cache-Control: no-store");

        // 已填则返回成功
        $is_replied = $this->Demandform_model->get_reply($coli_sn);

        $form_id = intval(trim($form_id));
        $_raw_data = $this->Demandform_model->get_form_detail($form_id);
        $part = array();
        foreach ($_raw_data->formdetail as $key => $fd) {
            $tmp_a = array();
            $tmp_r = array();
            foreach ($_raw_data->answer as $ka => $a) {
                if ($fd->FD_id === $a->A_FD_id) {
                    $tmp_a[] = $a;
                }
            }
            if ( ! empty($is_replied)) {
                foreach ($is_replied as $kr => $re) {
                    if ($re->FD_id === $fd->FD_id) {
                        $tmp_r[] = $re->CRD_content;
                    }
                }
            }
            $fd->reply = $tmp_r;
            $fd->answer = (object) $tmp_a;
            // part set
            $part["part1"]["title"] = "PART 1 -- Travel Preference";
            $part["part2"]["title"] = "PART 2";
            if (intval(trim($fd->FD_section)) === 1) {
                $part["part1"]["data"][] = $fd;
            }
            if (intval(trim($fd->FD_section)) === 2) {
                $part["part2"]["data"][] = $fd;
            }
        }
        $_raw_data->part = $part;
        unset($_raw_data->formdetail);
        unset($_raw_data->answer);
        $_raw_data->coli = $coli_sn;

        if ( ! empty($is_replied)) {
            $this->load->view('demandform/thanks', $_raw_data);
            return;
        }

        $this->load->view('demandform/index', $_raw_data);
        return;
    }

    public function form_save()
    {
        $all_post  = $this->input->post();

        $ret["code"] = 1;
        $ret["msg"]  = "";
        // 已填则返回成功 -- todo

        $check_input = array();
        foreach ($all_post["q"] as $key => $value) {
            if ($value["required"] == 1 && empty($value["answer"])) {
                $ret["code"] = 0;
                $ret["msg"][] = $key;
            }
        }
        if ($ret["code"] == 0) {
            return $this->output->set_content_type('text/plain')->set_output(json_encode($ret));
        }
        // CD_CustomerReply
        $this->Demandform_model->CR_COLI_sn = trim($this->input->post("coli_sn"));
        $this->Demandform_model->CR_F_id    = trim($this->input->post("fid"));
        $d = $a = $c = "";
        foreach ($all_post["q"] as $key => $value) {
            $split_answer = null;
            $class_max = 0;
            if (trim($value["ismain"]) != 1) {
                continue;
            }
            $class_max = (count($value["answer"])-1);
            $split_answer = explode("=_", $value["answer"][$class_max]);
            if (trim($value["qclass"]) === 'D' && ! empty($split_answer)) {
                $d = (isset($split_answer) ? $split_answer[1] : "?");
            }
            if (trim($value["qclass"]) === 'A' && ! empty($split_answer)) {
                $a = (isset($split_answer) ? $split_answer[1] : "?");
            }
            if (trim($value["qclass"]) === 'C' && ! empty($split_answer)) {
                $c = (isset($split_answer) ? $split_answer[1] : "?");
            }
        }
        $this->Demandform_model->CR_result = $d . $a . $c;
        // CD_CustomerReplyDetail
        $this->Demandform_model->CRD_CR_id = $this->Demandform_model->reply_save();
        $reply_detail = array();
        foreach ($all_post["q"] as $key => $value) {
            if (empty($value["atext"]) && empty($value["answer"])) {
                continue;
            }
            $this->Demandform_model->CRD_FD_id   = $value["fdid"];
            $this->Demandform_model->CRD_result  = null;
            $this->Demandform_model->CRD_Answer  = null;
            $this->Demandform_model->CRD_content = null;

            if (isset($value["answer"])) {
                foreach ($value["answer"] as $ka => $va) {
                    $split_answer = null;
                    $split_answer = explode("=_", trim($value["answer"][$ka]));
                    $this->Demandform_model->CRD_Answer = $split_answer[0];
                    $this->Demandform_model->CRD_content = (!empty($split_answer) ? ($split_answer[2]) : "");
                    if ( ! empty($value["qclass"]) && ! empty($split_answer)) {
                        $this->Demandform_model->CRD_result = (!empty($split_answer) ? $split_answer[1] : "?");
                    }
                    // 这里是 选择题others选项的可填入项
                    $this->Demandform_model->CRD_content .= (isset($value["atext"]) ? $value["atext"] : "");
                    $this->Demandform_model->replydetail_save();
                }
            } else {
                // 这里是填空题的输入
                $this->Demandform_model->CRD_content = $value["atext"];
                $this->Demandform_model->replydetail_save();
            }
        }
        return $this->output->set_content_type('text/plain')->set_output(json_encode($ret));
    }

    public function view_reply($coli)
    {
        $reply = $this->Demandform_model->get_reply($coli);
        $reply_arr = array();
        $reply_fd_arr = array();
        foreach ($reply as $key => $r) {
            $class_key = trim($r->FD_class) ? trim($r->FD_class) : "normal";
            $reply_arr["CR_result"] = $r->CR_result;
            $reply_arr[$class_key]["q" . $r->FD_id][] = $r;
        }
        $this->load->view('demandform/reply', $reply_arr);
    }

    public function converet_answer($answer)
    {
        $ret = ord($answer)-64;
        $ret = ($ret > 3) ? 3 : $ret;
        return $ret;
    }


}
