<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Super_admin_message_model extends CI_Model {
     //super_admin_message_settingstart
      public function select_person_email ($user_id) {
          for($i=0;$i<count($user_id);$i++){
        $sql = "SELECT * FROM tbl_person WHERE person_id='$user_id[$i]'";
        $result[] = $this->db->query($sql)->row();
          }
        return $result;
    }
      public function get_reciever_person_id($email_address) {
        $this->db->select('person_id');
        $this->db->from('tbl_person');
        $this->db->where('person_email', $email_address);
        $query_result = $this->db->get();
        $result = $query_result->row();
        if ($result) {
            return $result->person_id;
        }
    }
    
    public function insert_email_info_into_table_by_message_id($backend_message_id, $email_address,$user_id, $receiver_id_array) {
        $message_datetime = date('Y/m/d H:i:s', (time()));
        $this->db->set('backend_email_status', 4);
        $this->db->set('backend_email_datetime', $message_datetime);
        $this->db->set('backend_email_sender_id', $user_id);
        $this->db->set('backend_email_receiver_id', $receiver_id_array);
        $this->db->set('backend_email_receiver_email', $email_address);
        $this->db->set('backend_email_type', 1);
        $this->db->where('backend_email_id', $backend_message_id);
        $this->db->where('backend_email_sender_id', $user_id);
        $this->db->update('tbl_backend_email');
        return $backend_message_id;
    }

    public function insert_email_info_into_table_without_message_id( $email_address,$user_id, $receiver_id_array) {
        
        $message_datetime = date('Y/m/d H:i:s', (time()));
       
        $data['backend_email_sender_id'] = $user_id;
        $data['backend_email_receiver_id'] = $receiver_id_array; 
        $data['backend_email_receiver_email'] = $email_address; 
        $data['backend_email_type'] = 1; 

        $data['backend_email_datetime'] = $message_datetime;
        $data['backend_email_status'] = 4;

        $this->db->insert('tbl_backend_email', $data);
        $last_id = $this->db->insert_id();
        return $last_id;
        
    }
       public function save_message_body_draft_value($message_id, $message_body, $user_id) {
        $data['backend_email_sender_id'] = $user_id;
        $message_datetime = date('Y/m/d H:i:s', (time()));
        $data['backend_email_status'] = 4;
        $data[' backend_email_type'] = 1;
        $data['backend_email_datetime'] = $message_datetime;
        $data['backend_email_subject'] = $message_body;
        if ($message_id == "") {

            $this->db->insert('tbl_backend_email', $data);
            return $this->db->insert_id();
        } else {

            $this->db->set('backend_email_subject', $message_body);
            $this->db->set('backend_email_datetime',$message_datetime);
            $this->db->set('backend_email_type', 1);
            $this->db->where('backend_email_id', $message_id);
            $this->db->where('backend_email_sender_id', $user_id);
            $this->db->update('tbl_backend_email');
            return $message_id;
        }
    }
        public function save_sender_draft_attachment_value_after_message_body($message_id, $file_name) {


        $data['message_sender_attachment'] = $file_name;

        $data['message_id'] = $message_id;
        $data['message_attachment_original_name'] = $this->input->post('message_attachment_original_name', TRUE);
        //$data['message_id'] = $mesage_tbl_id;
        $data['message_attachment_type']=2;
       
        
        $this->db->insert('tbl_message_attachment', $data);
        $msg_attach_id = $this->db->insert_id();
        return $message_id . "," . $msg_attach_id;
    }
     public function for_only_attachment_generate_message_id($user_id) {
        $data['backend_email_sender_id'] = $user_id;

        $data['backend_email_status'] = 4;
        $message_datetime = date('Y/m/d H:i:s', (time()));
        $data['backend_email_datetime'] = $message_datetime;
        $this->db->insert('tbl_backend_email', $data);
        $last_id = $this->db->insert_id();
        return $last_id;
    }
     public function cancel_upload_file($message_id, $message_attachmen_id) {

        $this->db->where('message_attachmen_id', $message_attachmen_id);
        $this->db->where('message_id', $message_id);
        $this->db->where('message_attachment_type', 2);
        
        $this->db->delete('tbl_message_attachment');
    }


  //super_admin_message_setting_end
    
}
?>