<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin_Email_Format extends CI_Controller 
{
    public function __construct() 
    {
        parent::__construct();   
        $this->load->library('session'); 
        if($this->session->userdata('validated') != true)            
        {
            redirect('login');
        }      
        $this->load->helper('url');
        $this->load->model('Admin_Email_Format_Model');
        $this->load->model('Admin_Email_Format_Model','category'); 
    }

    public function index() 
    {
        $this->load->helper('url');        
        $data["extra_js"] = array("admin_email_format");
        $data['controller'] = base_url('admin_email_format');
        $data['page_title'] = "Manage Admin Email Format | CMS - Vinayak Webtech +91 90336 22526";
        $data['page_heading'] = "Manage Admin Email Format";
        $data['main_content'] = 'admin_email_format';
        $this->load->view('main_content', $data);
    } 

    public function edit($id)
    {
        $row = $this->category->get_by_id($id);

        $data = array(
            'id' => $row->id,
            'title' => $row->title,
            'subject' => $row->subject,
            'top_description' => $row->top_description,
            'bottom_description' => $row->bottom_description
        );

        echo json_encode($data);
    }
  
    public function update_data()
    {           
        $data = array(
            'title' => $this->input->post('title'), 
            'subject' => $this->input->post('subject'), 
            'top_description' => $this->input->post('top_description'),  
            'bottom_description' => $this->input->post('bottom_description'),  
            'date_time' => date("Y-m-d H:i:s"),
        ); 
             
        $this->category->update(array('id' => $this->input->post('id')), $data);

        $response = array(
            "status" => "ok", 
            "heading" => "Updated", 
            "message" => "Admin Email Format Updated Successfully."
        );
        
        echo json_encode($response);
        die;
    }

    public function ajax_list()
    {
        $list = $this->category->get_datatables();
        $data = array();
        $no = $_POST['start'] + 1;
        foreach ($list as $category) {
            $row = array();
            $row[] = $no++; 
            $row[] = $category->title;
            $row[] = $category->subject; 
            $row[] = '<div class="email-desc" title="'.htmlspecialchars(strip_tags($category->top_description)).'">'.strip_tags($category->top_description).'</div>'; 
            $row[] = date("d-m-Y g:i A", strtotime($category->date_time));
            
            $row[] = '<a type="button" href="javascript:void(0);" onclick="edit_data('."'".$category->id."'".')" class="edit_btn btn btn-sm btn-icon" title="Edit Record"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" class="eva eva-edit-2-outline"><g data-name="Layer 2"><g data-name="edit-2"><rect width="24" height="24" opacity="0"></rect><path d="M19 20H5a1 1 0 0 0 0 2h14a1 1 0 0 0 0-2z"></path><path d="M5 18h.09l4.17-.38a2 2 0 0 0 1.21-.57l9-9a1.92 1.92 0 0 0-.07-2.71L16.66 2.6A2 2 0 0 0 14 2.53l-9 9a2 2 0 0 0-.57 1.21L4 16.91a1 1 0 0 0 .29.8A1 1 0 0 0 5 18zM15.27 4L18 6.73l-2 1.95L13.32 6zm-8.9 8.91L12 7.32l2.7 2.7-5.6 5.6-3 .28z"></path></g></g></svg></a>';
            
            $data[] = $row;
        }
        $output = array(
            "draw" => $_POST['draw'],
            "recordsTotal" => $this->category->count_all(),
            "recordsFiltered" => $this->category->count_filtered(),
            "data" => $data,
        ); 
        echo json_encode($output);
    }
}