<?php
/**
 *
 */

include_once(dirname(dirname(__FILE__))."/library/publicBase.php");

class index extends publicBase {
    public function home() {
        $id = $this->get('id');

        $m_case = new mCase();
        $data = $m_case->getCaseInfo($id);

        $this->view['data'] = $data;
    }

    public function ajax_save_case() {
        $name = trim($this->post('name'));
        $source = trim($this->post('source'));
        $original = trim($this->post('original'));
        $method = trim($this->post('method'));
        $herbs = $this->post('herbs');

        //新增药方
        $m_case = new mCase();
        $id = $m_case->createCase($name, $source, $original, $method, $herbs);
        if (!$id) $this->ajax_json(false, $m_case->getError());

        $this->ajax_json(true, '添加成功');
    }

    public function ajax_update_case() {
        $id = $this->post('id')+0;
        $name = trim($this->post('name'));
        $source = trim($this->post('source'));
        $original = trim($this->post('original'));
        $method = trim($this->post('method'));
        $herbs = $this->post('herbs');

        $m_case = new mCase();
        $res = $m_case->updateCase($id, $name, $source, $original, $method, $herbs);
        if (!$res) $this->ajax_json(false, $m_case->getError());

        $this->ajax_json(true, '保存成功');
    }

    public function ajax_search() {
        $content = $this->post('content');
        if (empty($content))$this->ajax_json(false, '请输入方名');

        $page_num = $this->post('page_num') ? $this->post('page_num') : 1;
        $page_size = $this->post('page_size') ? $this->post('page_size') : 100;

        $m_case = new mCase();
        $data = $m_case->getCaseByName($content, $page_num, $page_size);
        $total = $m_case->getCaseByNameTotal($content);

        $this->ajax_json(true, '获取成功', array('total' => $total, 'current_page' => $page_num, 'total_page' => ceil($total/$page_size), 'list' => $data));
    }

    public function ajax_case_detail() {
        $id = $this->get('id')+0;
        if (empty($id)) $this->ajax_json(false, '非法请求');

        $m_case = new mCase();
        $data = $m_case->getCaseInfo($id);
        if (!$data) $this->ajax_json(false, $m_case->getError());

        $this->ajax_json(true, '获取成功', $data);
    }

    public function ajax_save_user_case() {
        $uid = $this->post('uid');
        $token = $this->post('token');
        $case_id = $this->post('case_id')+0;
        if (empty($uid) || empty($token) || empty($case_id)) $this->ajax_json(false, '非法请求');

        $m_user = new mUser();
        $is_login = $m_user->validateToken($uid,$token);
        if (!$is_login) $this->ajax_json(false, '请登录后操作');

        $data = array(
            'name' => trim($this->post('name')),
            'patient_name' => trim($this->post('patient_name')),
            'patient_age' => $this->post('patient_age')+0,
            'patient_sex' => $this->post('patient_sex')+0,
            'patient_say' => trim($this->post('patient_say')),
            'first_diagnosis' => trim($this->post('first_diagnosis')),
            'diagnosis' => trim($this->post('diagnosis')),
            'prescribe_num' => $this->post('prescribe_num')+0,
            'prescribe_herb' => $this->post('prescribe_herb'),
        );

        $m_user_case = new mUserCase();
        $id = $m_user_case->createUserCase($uid, $case_id, $data);
        if (!$id) $this->ajax_json(false, $m_user_case->getError());

        $this->ajax_json(true, '保存成功',array('id' => $id));
    }

    public function ajax_update_user_case() {
        $uid = $this->post('uid');
        $token = $this->post('token');
        $id = $this->post('id')+0;
        if (empty($uid) || empty($token) || empty($id)) $this->ajax_json(false, '非法请求');

        $m_user = new mUser();
        $is_login = $m_user->validateToken($uid,$token);
        if (!$is_login) $this->ajax_json(false, '请登录后操作');

        $data = array(
            'name' => trim($this->post('name')),
            'feedback' => trim($this->post('feedback')),
        );

        $m_user_case = new mUserCase();
        $res = $m_user_case->updateUserCase($uid, $id, $data);
        if (!$res) $this->ajax_json(false, $m_user_case->getError());

        $this->ajax_json(true, '保存成功',array('id' => $id));
    }

    public function ajax_user_case_list() {
        $uid = $this->post('uid');
        $token = $this->post('token');
        if (empty($uid) || empty($token)) $this->ajax_json(false, '非法请求');

        $m_user = new mUser();
        $is_login = $m_user->validateToken($uid,$token);
        if (!$is_login) $this->ajax_json(false, '请登录后操作');

        $page_num = $this->post('page_num') ? $this->post('page_num') : 1;
        $page_size = $this->post('page_size') ? $this->post('page_size') : 100;

        $m_user_case = new mUserCase();
        $data = $m_user_case->getUserCaseList($uid, $page_num, $page_size);
        if (!$data) $this->ajax_json(false, $m_user_case->getError());
        $total = $m_user_case->getUserCaseListCount($uid);

        $return = array(
            'total' => $total,
            'current_page' => $page_num,
            'total_page' => ceil($total/$page_size)
        );
        $return = array_merge($return, $data);

        $this->ajax_json(true, '获取成功', $return);
    }

    public function ajax_user_case_detail() {
        $uid = $this->post('uid');
        $token = $this->post('token');
        $id = $this->post('id')+0;
        if (empty($uid) || empty($token) || empty($id)) $this->ajax_json(false, '非法请求');

        $m_user = new mUser();
        $is_login = $m_user->validateToken($uid,$token);
        if (!$is_login) $this->ajax_json(false, '请登录后操作');

        $m_user_case = new mUserCase();
        $data = $m_user_case->getUserCaseInfo($uid, $id);
        if (!$data) $this->ajax_json(false, $m_user_case->getError());

        $this->ajax_json(true, '获取成功', $data);
    }

    public function ajax_collect_log() {
        $page_num = $this->post('page_num') ? $this->post('page_num') : 1;
        $page_size = $this->post('page_size') ? $this->post('page_size') : 100;

        $m_case = new mCase();
        $data = $m_case->getCollectLog($page_num, $page_size);
        $total = $m_case->getCollectLogTotal();

        $this->ajax_json(true, '获取成功', array('total' => $total, 'current_page' => $page_num, 'total_page' => ceil($total/$page_size), 'list' => $data));
    }

    public function ajax_login(){
        $code = $this->get('code');
        if (!$code)$this->ajax_json(false, '非法请求');

        $m_user = new mUser();
        $uid = $m_user->getUid($code);
        $token = $m_user->getToken($uid);

        $this->ajax_json(true, '登录成功', array('uid' => $uid, 'token'=> $token));
    }

    public function ajax_contact_us(){
        $this->ajax_json(true, '获取成功',array('img_url'=> CONTACT_US_IMG_URL));
    }

    public function export_user_case_list(){
        $uid = $this->post('uid');
        $token = $this->post('token');
        if (empty($uid) || empty($token)) $this->ajax_json(false, '非法请求');

        $m_user = new mUser();
        $is_login = $m_user->validateToken($uid,$token);
        if (!$is_login) $this->ajax_json(false, '请登录后操作');

        $page_num = $this->post('page_num') ? $this->post('page_num') : 1;
        $page_size = $this->post('page_size') ? $this->post('page_size') : 100;

        $m_user_case = new mUserCase();
        $data = $m_user_case->getUserCaseList($uid, $page_num, $page_size, true, true);
        if (!$data) $this->ajax_json(false, $m_user_case->getError());
        $total = $m_user_case->getUserCaseListCount($uid);

        $data_key = sprintf(RQ_USER_CASE_DATA, $uid, 0);

        $robj = $m_user_case->initRedis();
        $robj->setex($data_key, 60, json_encode($data));
        $pdf_url = $m_user_case->createPdf($uid, $data_key);

        $this->ajax_json(true, '获取成功', array('total' => $total, 'current_page' => $page_num, 'total_page' => ceil($total/$page_size), 'pdf_url' => $pdf_url));
    }

    public function export_user_case(){
        $uid = $this->post('uid');
        $token = $this->post('token');
        $id = $this->post('id')+0;
        if (empty($uid) || empty($token) || empty($id)) $this->ajax_json(false, '非法请求');

        $m_user = new mUser();
        $is_login = $m_user->validateToken($uid,$token);
        if (!$is_login) $this->ajax_json(false, '请登录后操作');

        $m_user_case = new mUserCase();
        $data = $m_user_case->getUserCaseInfo($uid, $id, true);
        if (!$data) $this->ajax_json(false, $m_user_case->getError());

        $data_key = sprintf(RQ_USER_CASE_DATA, $uid, $id);

        $robj = $m_user_case->initRedis();
        $robj->setex($data_key, 60, json_encode($data));
        $pdf_url = $m_user_case->createPdf($uid, $data_key);

        $this->ajax_json(true, '获取成功',array('pdf_url'=> $pdf_url));
    }

    public function export_pdf(){
        $key = $this->get('key');

        $m_user_case = new mUserCase();
        $robj = $m_user_case->initRedis();
        $data = $robj->get($key);

        $this->view['data'] = json_decode($data,true);
    }

    public function ajax_mail(){
        $uid = $this->post('uid');
        $token = $this->post('token');
        if (empty($uid) || empty($token)) $this->ajax_json(false, '非法请求');

        $mUser = new mUser();
        $is_login = $mUser->validateToken($uid,$token);
        if (!$is_login) $this->ajax_json(false, '请登录后操作');

        $email = $this->post('email');
        if (empty($email)) $this->ajax_json(false, '邮箱地址不能为空');
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $this->ajax_json(false, '邮箱地址无效');

        $pdf_url = $this->post('pdf_url');
        if (empty($pdf_url)) $this->ajax_json(false, 'pdf地址不能为空');
        if (!filter_var($pdf_url, FILTER_VALIDATE_URL)) $this->ajax_json(false, 'pdf地址无效');

        $directory_name = basename(dirname($pdf_url));
        if ($directory_name != $uid) $this->ajax_json(false, '非法请求');

        $mUserCase = new mUserCase();
        $res = $mUserCase->sendMail(array($email),date('Y年m月d日',time()).'-医案导出','',sprintf(USER_CASE_PDF_PATH, $uid) . basename($pdf_url));
        if (!$res) $this->ajax_json(true, '发送失败');

        $this->ajax_json(true, '发送成功');
    }
}