<?php

class Demandform_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->INFO = $this->load->database('INFO', TRUE);
    }

    public function get_form_detail($form_id)
    {
        $ret = (object) array();
        $form_sql = "SELECT top 1 * FROM CD_form WHERE f_id=$form_id " ;
        $f_query = $this->INFO->query($form_sql);
        $ret->form = $f_query->row();

        $formdetail_sql = "SELECT fd.*
                            FROM CD_formdetail fd
                            WHERE fd.FD_F_id=$form_id order by FD_sort asc" ;
        $fd_query = $this->INFO->query($formdetail_sql);
        $ret->formdetail = $fd_query->result();

        $answer_sql = "SELECT a.*
                        FROM CD_formdetail fd
                        INNER JOIN CD_answer a ON a.A_FD_id=fd.FD_id
                        WHERE fd.FD_F_id=$form_id " ;
        $a_query = $this->INFO->query($answer_sql);
        $ret->answer = $a_query->result();

        return $ret;
    }

    // CD_CustomerReply
    public $CR_COLI_sn;
    public $CR_F_id;
    public $CR_result;

    public function reply_save()
    {
        $rsql = "INSERT INTO CD_CustomerReply (
                   CR_COLI_sn
                   ,CR_F_id
                   ,CR_replytime
                   ,CR_result)
                 VALUES
                       (?
                       ,?
                       ,GETDATE()
                       ,N?)";
     // log_message('error',$this->INFO->compile_binds($rsql,
        $rquery = $this->INFO->query($rsql,
            array($this->CR_COLI_sn, $this->CR_F_id, $this->CR_result));
        $cr_id_q = "SELECT TOP 1 CR_id
                    FROM CD_CustomerReply
                    WHERE CR_COLI_sn=?
                    ORDER BY CR_replytime DESC";
        $cr_query = $this->INFO->query($cr_id_q, array($this->CR_COLI_sn));
        $cr_id_r = $cr_query->row();
        return $cr_id_r->CR_id;
    }

    // CD_CustomerReplyDetail
    public $CRD_FD_id;
    public $CRD_Answer;
    public $CRD_content;
    public $CRD_result;
    public $CRD_CR_id;

    public function replydetail_save()
    {
        $rdsql = "INSERT INTO CD_CustomerReplyDetail
                   (CRD_CR_id
                   ,CRD_FD_id
                   ,CRD_Answer
                   ,CRD_content
                   ,CRD_result
                )
                 VALUES (?,?,?,N?,?) ";
     // log_message('error',$this->INFO->compile_binds($rdsql,
        $rdquery = $this->INFO->query($rdsql,
                array(
                    $this->CRD_CR_id,
                    $this->CRD_FD_id,
                    $this->CRD_Answer,
                    $this->CRD_content,
                    $this->CRD_result
                    )
            );
        return $this->INFO->insert_id();
    }

    public function get_reply($coli)
    {
        $sql = "SELECT fd.FD_id,
                       fd.FD_F_id,
                       fd.FD_section,
                       cr.CR_replytime,
                       cr.CR_COLI_sn,
                       cr.CR_result,
                       fd.FD_class,
                       fd.FD_ismain,
                       crd.CRD_result,
                       fd.FD_question,
                       crd.CRD_content
                FROM CD_CustomerReply cr
                INNER JOIN CD_CustomerReplyDetail crd ON crd.CRD_CR_id=cr.CR_id
                INNER JOIN CD_FormDetail fd ON CRD_FD_id=FD_id
                WHERE CR_COLI_sn='$coli'
                ORDER BY fd.FD_section ASC,fd.FD_sort ASC
        ";
        $query = $this->INFO->query($sql);

        return $query->result();
    }



}
