<?php

class GeneralAjax extends mainController {

    public function __construct() {
        parent::__construct();
        $this->load->model('general');
    }
    public function landing() {
    }
    public function submit_archive() {
        $submit = GeneralClass::submit_archive($_POST);
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [$submit[0], 'danger']));
    }
    public function delete_archive() {
        $delete = $this->_registry->general_class->setData($_GET)->delete_archive();
        die(empty($delete) ? "ok" : $delete[0]);
    }

    public function getArchiveData() {
        $data = CommonFunctions::CommonView('tbl_archive', ['archive_id'], [$_GET['archive_id']])[0];
        die(json_encode($data));
    }

    public function archive_list_ajax() {
        $columns = array( array(
            'db' => 'archive_name',
            'formatter' => function( $d, $row,$is_excel ) {;
                                                           return $is_excel ? $d : "<a target='_blank' href='".SITE_LINK."/uploads/archive/".$d."'><i class='fa fa-file fa-2x'></i></a>";
                                                          }), 'archive_title', 'archive_details', 'date_created', 'date_time',
                         array(
                             'db' => 'archive_id',
                             'formatter' => function( $d, $row ) {
                                 $html = Forms::action_snippet(["color" => "primary-darker",
                                                                "isDropDown" => true,
                                                                "data_id" => $d,
                                                                "ActionOption" => [
                                                                    ["title" => CommonLanguage::getMessage("Edit"),
                                                                     'data-target' => '#archive_modal',
                                                                     "class" => "edit",
                                                                     "icon" => "fa-pencil",
                                                                     "attr"=>"data-archive-name='{$row['archive_name']}' data-archive-title='{$row['archive_title']}' data-archive-details='{$row['archive_details']}' data-origin='{$row['main_type']}'",
                                                                     "permission" => "edit_archive"
                                                                    ],
                                                                    ["title" => CommonLanguage::getMessage("delete"),
                                                                     "class" => "delete",
                                                                     "icon" => "fa-trash",
                                                                     "attr"=>"data-archive-name='{$row['archive_name']}' data-origin='{$row['main_type']}'",
                                                                     "permission" => "delete_archive"
                                                                    ]
                                                                ]]);
                                 return $html;
                             }),'main_type'
                        );
        $key = 'archive_id';
        $table = 'tbl_archive';
        $data = DataTable::complex($_GET, $table, $key, $columns);
        echo json_encode($data);
    }
    
    public function route_list() {
        $columns = array('route_name',
                         array('db' => 'route_color','formatter' => function( $d, $row, $excel ) {
                             return empty($d) ? '-- -- ' : ($excel ? $d : '<span class="label route_color" style="background-color:'.$d.'" color="'.$d.'">&nbsp;</span>');
                         }),
                         array(
                             'db' => 'route_id',
                             'formatter' => function( $d, $row ) {
                                 $html = Forms::action_snippet(["color" => "primary-darker",
                                                                "isDropDown" => true,
                                                                "data_id" => $d,
                                                                "ActionOption" => [
                                                                    ["title" => CommonLanguage::getMessage("Edit"),
                                                                     'data-target' => '#route_modal',
                                                                     "class" => "edit",
                                                                     "icon" => "fa-pencil",
                                                                     "permission" => "edit_route"
                                                                    ],
                                                                    ["title" => CommonLanguage::getMessage("delete"),
                                                                     "class" => "delete",
                                                                     "icon" => "fa-trash",
                                                                     "permission" => "delete_route"
                                                                    ]
                                                                ]]);
                                 return $html;
                             })
                        );
        $key = 'route_id';
        $table = 'tbl_route';
        $data = DataTable::complex($_GET, $table, $key, $columns);
        echo json_encode($data);
    }
    public function submitRoute() {
        $submit = $this->_registry->route->setData($_POST)->submitRoute();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [$submit[0], 'danger']));
    }
    public function deleteRoute() {
        $delete = $this->_registry->route->setData($_GET)->deleteRoute();
        die(empty($delete) ? "ok" : $delete[0]);
    }
    
    public function route_schedule_list() {
        $columns = array(array('db' => 'route_ids','formatter' => function( $d, $row, $excel ) {
                             return empty($d) ? '-- -- ' : $this->_registry->route->getRoutes($d)['route_names'];
                         }),
                         array('db' => 'is_one_time','formatter' => function( $d, $row, $excel ) {
                             return '<span class="label label-'.(empty($d) ? 'danger' : 'success').'">'.CommonLanguage::getMessage(empty($d) ? 'no' : 'yes').'</span>';
                         }),'schedule_date','employee','User_Name',
                         array(
                             'db' => 'schedule_id',
                             'formatter' => function( $d, $row ) {
                                 $html = Forms::action_snippet(["color" => "primary-darker",
                                                                "isDropDown" => true,
                                                                "data_id" => $d,
                                                                "ActionOption" => [
                                                                    ["title" => CommonLanguage::getMessage("Edit"),
                                                                     'data-target' => '#route_schedule_modal',
                                                                     "class" => "edit",
                                                                     "icon" => "fa-pencil",
                                                                     "permission" => "edit_route_schedule"
                                                                    ],
                                                                    ["title" => CommonLanguage::getMessage("delete"),
                                                                     "class" => "delete",
                                                                     "icon" => "fa-trash",
                                                                     "permission" => "delete_route_schedule"
                                                                    ]
                                                                ]]);
                                 return $html;
                             })
                        );
        $key = 'schedule_id';
        $table = 'route_schedule';
        $data = DataTable::complex($_GET, $table, $key, $columns);
        echo json_encode($data);
    }
    public function submitRouteSchedule() {
        $submit = $this->_registry->route->setData($_POST)->submitRouteSchedule();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [$submit[0], 'danger']));
    }
    public function getRouteSchedule() {
        $data = $this->_registry->route->getRouteSchedule($_GET['schedule_id']);
        die(json_encode($data));
    }
    public function deleteRouteSchedule() {
        $delete = $this->_registry->route->setData($_GET)->deleteRouteSchedule();
        die(empty($delete) ? "ok" : $delete[0]);
    }
    
    public function get_customers_route_map() {
        $data = json_encode(array("data"=>$this->_registry->customer->setData($_GET)->get_customers_route_map()));
        die($data);
    }
    public function submitCustomersRoute() {
        $submit = $this->_registry->route->setData($_POST)->submitCustomersRoute();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [$submit[0], 'danger']));
    }

    public function route_report() {
        $data = $this->_registry->route->setData($_GET)->route_report();
        $html = Forms::build_table_html($data);
        die($html);
    }

}
