<?php

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


class Mails_model extends CI_Model {
        
    function __construct() 
    {
        parent::__construct();
        $this->DEST = $this->load->database('DEST', TRUE);
        $this->mailconfig=$this->lang->line('mail_user');
    }

    public function save_mail($value_cols)
    {
        $sql="INSERT INTO mails 
            (   mail_uid,
                mail_o_orderno,
                mail_msgno,
                mail_messageid,
                mail_from,
                mail_to,
                mail_cc,
                mail_from_name,
                mail_to_name,
                mail_subject,
                mail_body,
                mail_seen,
                mail_date,
                mail_deleted,
                mail_importance,
                mail_box,
                mail_attachs,
                mail_sendstate
            ) 
            VALUES $value_cols";

        $query=$this->DEST->query($sql);
        $insertid = $this->DEST->last_id('mails');
        return $insertid;
    }

    public function sendmail($frommail,$tolist_array,$cclist_array,$subject,$mailbody,$attachment=array(),$embedded_attachs_arr=array())
    {
        $mail_user=$this->lang->line('mail_user');
        $this->load->library('Phpmailer_lib');
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->Host = 'ssl://smtp.chinahighlights.net';
        $mail->Port = '465';
        $mail->SMTPAuth = true;
        $mail->Username = $mail_user['user'];
        $mail->Password = $mail_user['password'];
        $mail->SMTPSecure = 'tls';
        $mail->CharSet    = "utf-8";
        $mail->Encoding   = "base64";
        $mail->IsHTML(true);
        foreach ($attachment as $key => $attach) {
            $mail->AddAttachment($attach,$key);
        }

        //处理正文中的嵌套的图片
        foreach ($embedded_attachs_arr as $img) {
            $mail->AddEmbeddedImage($img[0], $img[1], $img[2], 'base64', $img[3]);
        }
        
        $mail->From = $frommail;         //发件人
        foreach ($tolist_array as $v) {
            $mail->AddAddress($v);       //收件人
        }
        foreach ($cclist_array as $c) {
            $mail->addCC($c);            //抄送
        }
        $mail->Subject = $subject;       //邮件主题
        $mail->Body = $mailbody;         //邮件内容

        if (!$mail->Send()) {
            $result= "error:".$mail->ErrorInfo;
        } else {
            $result= $mail->getLastMessageID();
        }
        return $result;
    }

    public function set_read($mail_sn)
    {
        $sql="UPDATE mails SET mail_seen=1 WHERE mail_sn in($mail_sn) ";
        $query=$this->DEST->query($sql);
        return $query;
    }

    public function set_sent($mail_sn)
    {
        $sql="UPDATE mails SET mail_sendstate=1 WHERE mail_sn in($mail_sn) ";
        $query=$this->DEST->query($sql);
        return $query;
    }

    //邮件列表
    public function get_mail_list($mail_sn=false,$last_id=false,$top=30,$filed='*',$order='mail_date desc,mail_msgno desc')
    {
        $mailowner=$this->mailconfig['user'];
        $mapsql=$last_id?" and mail_box!='SENT' AND mail_sn>$last_id ":'';
        $mapsql=$mail_sn?" AND mail_sn=$mail_sn ":$mapsql;
        $sql="SELECT top $top $filed FROM mails WHERE (mail_from like '%$mailowner%' or mail_to like '%$mailowner%') $mapsql ORDER BY $order";
        $query=$this->DEST->query($sql);
        return $query->result();
    }

    //获取订单的邮件列表
    public function get_search_mails($mail_o_orderno,$mail_subject=false)
    {
        if($mail_subject!=false){
            $sql="SELECT TOP 300 mail_sn FROM mails WHERE mail_o_orderno like '%$mail_o_orderno%' or mail_subject like '%$mail_subject%' ORDER BY mail_date desc";
        }else{
            $sql="SELECT * FROM mails WHERE mail_o_orderno='$mail_o_orderno' ORDER BY mail_date desc";
        }
        $query=$this->DEST->query($sql);
        return $query->result();
    }

    //根据邮件信件ID获取邮件
    public function get_list_by_msgid($message_id)
    {
        $sql="SELECT top 1 mail_sn FROM mails WHERE mail_messageid=?";
        $query=$this->DEST->query($sql,array($message_id));
        $result=$query->result();
        return $result;
    }

    //shou件箱邮件
    public function get_inbox_list($mail_sn_string='',$page_flag=false)
    {
        if ($page_flag) {
            $mailowner=$this->mailconfig['user'];
            $sql="SELECT TOP 360 mail_sn FROM mails WHERE mail_to like '%$mailowner%' AND mail_box!='SENT' AND mail_deleted!=1 ORDER BY mail_date desc,mail_msgno desc";
        }else{
            $sql="SELECT * FROM mails WHERE mail_sn in ($mail_sn_string) ORDER BY mail_date desc,mail_msgno desc";
        }
        $query=$this->DEST->query($sql);
        return $query->result();
    }

    //发件箱邮件
    public function get_sended_list($top=30,$mail_sendstate=false,$mail_sn_string='',$page_flag=false)
    {
        $sendstate_sql=$mail_sendstate!==false?" AND mail_sendstate=$mail_sendstate":'';
        if ($mail_sn_string=='') {
            $mailowner=$this->mailconfig['user'];
            if (!$page_flag) {
                $sql="SELECT top $top  * FROM mails WHERE mail_from like '%$mailowner%' AND mail_box='SENT' $sendstate_sql ORDER BY mail_date desc,mail_sn desc";
            }else{
                $sql="SELECT top 360  mail_sn FROM mails WHERE mail_from like '%$mailowner%' AND mail_box='SENT' $sendstate_sql ORDER BY mail_date desc,mail_sn desc";
            }
        }else{
            $sql="SELECT * FROM mails WHERE mail_sn in($mail_sn_string) ORDER BY mail_date desc,mail_sn desc";
        }
        $query=$this->DEST->query($sql);
        return $query->result();
    }
    //垃圾箱
    public function get_deleted_list($mail_sn_string='',$page_flag=false)
    {
        if ($mail_sn_string=='') {
            $mailowner=$this->mailconfig['user'];
            if (!$page_flag) {
                $sql="SELECT top 30 * FROM mails WHERE (mail_from like '%$mailowner%' or mail_to like '%$mailowner%') AND mail_deleted=1 ORDER BY mail_date desc,mail_msgno desc";
            }else{
                $sql="SELECT top 360 mail_sn FROM mails WHERE (mail_from like '%$mailowner%' or mail_to like '%$mailowner%') AND mail_deleted=1 ORDER BY mail_date desc,mail_msgno desc";
            }
        }else{
            $sql="SELECT * FROM mails WHERE mail_sn in($mail_sn_string) ORDER BY mail_date desc,mail_msgno desc";
        }
        $query=$this->DEST->query($sql);
        return $query->result();
    }

    public function unseen_list()
    {
        $mailowner=$this->mailconfig['user'];
        $sql="SELECT count(mail_sn) as unseen_count FROM mails WHERE mail_to like '%$mailowner%' AND mail_seen=0";
        $query=$this->DEST->query($sql);
        $result=$query->result();
        $num=0;
        if (!empty($result)) {
            $num=$result[0]->unseen_count;
        }
        return $num;
    }

    public function update_mail($mail_sn,$filed,$value)
    {
        $sql="UPDATE mails SET $filed=? WHERE mail_sn =? ";
        $query=$this->DEST->query($sql,array($value,$mail_sn));
        return $query;
    }

}