<?php

class RestaurantAjax extends mainController {

    public function __construct() {
        parent::__construct();
        $this->load->model('restaurant');
    }

    public function landing() {
        //        self::RestaurantAjax();
    }

    public function submituser() {
        $UpdateRole = $this->_registry->restaurant_pos->setData($_POST)->SubmitRestaurantUser();
        die(json_encode(empty($UpdateRole) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [CommonLanguage::getMessage('secret_code_exist'), 'danger']));
    }

    //    public function restaurant_users() {
    //        $columns = array('User_Name', 'Role_Name', 'user_email', 'user_mobile_phone',
    //                         array(
    //                             'db' => 'User_ID',
    //                             'dt' => 4,
    //                             'formatter' => function( $d ) {
    //                                 $html = Forms::action_snippet(["color" => "success",
    //                                                                "isDropDown" => true,
    //                                                                "data_id" => $d,
    //                                                                "ActionOption" => [
    //                                                                    ["title" => CommonLanguage::getMessage("delete"),
    //                                                                     "class" => "delete_user",
    //                                                                     "icon" => "fa-trash",
    //                                                                     "permission"=>"delete_user"
    //                                                                    ],
    //                                                                    ["title" => CommonLanguage::getMessage("Edit"),
    //                                                                     'data-target' => '#user_modal',
    //                                                                     "class" => "edit_user",
    //                                                                     "icon" => "fa-pencil",
    //                                                                     "permission"=>"edit_user"
    //                                                                    ]
    //                                                                ]]);
    //                                 return $html;
    //                             }
    //                         )
    //                        );
    //        $key = 'User_ID';
    //        $table = 'restaurant_users';
    //        $data = DataTable::complex($_GET, $table, $key, $columns, ' Status_ID = 1');
    //        echo json_encode($data);
    //    }
    public function submitfloor() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->SubmitRestaurantFloor();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }

    public function submitVoidInvoice() {
        $submit = $this->_registry->restaurant_pos->setData($_GET)->submitVoidInvoice();
         die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [CommonLanguage::getMessage('Failed_Submit'), $submit]));
    }

    public function submitVoidReason() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->submitVoidReason();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }

    public function deleteVoidReason() {
        $submit = $this->_registry->restaurant_pos->setData($_GET)->deleteVoidReason();
        die(empty($submit) ? "ok" : "error");
    }

    public function submitKitchenNote() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->submitKitchenNote();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }

    public function deleteKitchenNote() {
        $submit = $this->_registry->restaurant_pos->setData($_GET)->deleteKitchenNote();
        die(empty($submit) ? "ok" : "error");
    }

    public function submittransferUser() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->changeUser();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }

    public function floors_list() {
        $columns = array('floor_name',
            array(
                'db' => 'floor_id',
                'dt' => 1,
                'formatter' => function( $d , $row ) {
                    $employee_ids = explode(",",$row['employee_id']);
                    $emp_name = '';
                    foreach($employee_ids as $employee_id){
                        $emp_name .= Employee::get_emp_name_by_id($employee_id).',';
                    }
                    $emp_name = substr($emp_name,0,-1);
                    
                    $html = Forms::action_snippet(["color" => "success",
                                "isDropDown" => true,
                                "data_id" => $d,
                                "ActionOption" => [
                                    ["title" => CommonLanguage::getMessage("tables"),
                                        'href' => 'Inventory/table_setting/' . $d,
                                        "class" => "",
                                        "icon" => "fa-eye",
                                        "permission" => "tables"
                                    ]
                                    ,
                                    ["title" => CommonLanguage::getMessage("Edit"),
                                        'data-target' => '#floor_modal',
                                        "class" => "edit_floor",
                                        "attr" => 'employee-id="'.$row['2'].'" employee-ids="'.$row['employee_id'].'" employee-name="'.$emp_name.'"',
                                        "icon" => "fa-pencil",
                                        "permission" => "edit_floor"
                                    ],
                                    ["title" => CommonLanguage::getMessage("delete"),
                                        "class" => "delete_floor",
                                        "icon" => "fa-trash",
                                        "permission" => "delete_floor"
                                    ]
                    ]]);
                    return $html;
                }),
                array('db' => 'employee_id', 'dt' => 2),
                array('db' => 'employee_name','dt' => 3),
        );
        $key = 'floor_id';
        $table = 'tbl_floor';
        $table = '(SELECT f.*, if(CONCAT( e.Employee_FirstName, " ", e.Employee_MiddleName, " ", e.Employee_LastName ) IS NULL , "Select",CONCAT( e.Employee_FirstName, " ", e.Employee_MiddleName, " ", e.Employee_LastName ) ) AS employee_name FROM tbl_floor f LEFT JOIN tbl_hrm_employee e ON e.Employee_ID = f.employee_id
        ) as collection';

        $data = DataTable::complex($_GET, $table, $key, $columns, ' status_id = 1');
        echo json_encode($data);
    }

    public function void_reason_list() {
        $columns = array('reason_title',
            array(
                'db' => 'reason_type',
                'dt' => 1,
                'formatter' => function( $d ) {
                    $types = ['both', 'Item', 'invoice'];
                    return CommonLanguage::getMessage($types[$d]);
                }),
            array(
                'db' => 'reason_id',
                'dt' => 2,
                'formatter' => function( $d, $row ) {
                    $html = Forms::action_snippet(["color" => "success",
                                "isDropDown" => true,
                                "data_id" => $d,
                                "ActionOption" => [
                                    ["title" => CommonLanguage::getMessage("Edit"),
                                        'data-target' => '#add_edit_void_reason_modal',
                                        "class" => "edit",
                                        "icon" => "fa-pencil",
                                        "attr" => "data-reason-type={$row[1]}",
                                        "permission" => "edit_void_reason"
                                    ],
                                    ["title" => CommonLanguage::getMessage("delete"),
                                        "class" => "delete",
                                        "icon" => "fa-trash",
                                        "permission" => "delete_void_reason"
                                    ]
                    ]]);
                    return $html;
                })
        );
        $key = 'reason_id';
        $table = 'tbl_void_reason';
        $data = DataTable::complex($_GET, $table, $key, $columns);
        echo json_encode($data);
    }

    public function kitchen_note_list() {
        $columns = array('note_title',
            array('db'=>'type','formatter'=>function($d){
                return CommonLanguage::getMessage($d==0 ? 'order' : ($d==1 ? 'invoice' : 'posponted'));
            }),
            array(
                'db' => 'note_id',
                'formatter' => function( $d, $row ) {
                    $html = Forms::action_snippet(["color" => "success",
                                "isDropDown" => true,
                                "data_id" => $d,
                                "ActionOption" => [
                                    ["title" => CommonLanguage::getMessage("Edit"),
                                        'data-target' => '#add_edit_kitchen_note_modal',
                                        'attr'=>'data-type="'.$row['type'].'"',
                                        "class" => "edit",
                                        "icon" => "fa-pencil",
                                        "permission" => "edit_kitchen_note"
                                    ],
                                    ["title" => CommonLanguage::getMessage("delete"),
                                        "class" => "delete",
                                        "icon" => "fa-trash",
                                        "permission" => "delete_kitchen_note"
                                    ]
                    ]]);
                    return $html;
                })
        );
        $key = 'note_id';
        $table = 'tbl_kitchen_note';
        $data = DataTable::complex($_GET, $table, $key, $columns);
        echo json_encode($data);
    }

    public function voidInvoices() {
        $columns = array('invoice_prefix_nb', 'Customer_Name', 'reason_title', 'User_Name', 'date_created', 'void_time');
        $key = 'Invoice_ID_Auto';
        $table = 'void_invoices';
        $data = DataTable::complex($_GET, $table, $key, $columns);
        echo json_encode($data);
    }

    public function voidItems() {
        $columns = array('invoice_prefix_nb', 'Product_Name', 'Item_Quantity_Current',
            array(
                'db' => 'Product_Total_Price',
                'dt' => 3,
                'formatter' => function( $d ) {
                    return CommonFunctions::formatNumber($d);
                })
            , 'date_time', 'User_Name');
        $key = 'Entery_ID';
        $table = 'void_items';
        $data = DataTable::complex($_GET, $table, $key, $columns);
        echo json_encode($data);
    }

    public function submittable() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->SubmitRestaurantTable();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }

    public function updateAliasTableName() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->updateAliasTableName();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }

    public function tables_list() {
        $columns = array('table_name', 'table_alias_name',
            array(
                'db' => 'table_id',
                'dt' => 2,
                'formatter' => function( $d , $row ) {
                    $employee_ids = explode(",",$row['employee_id']);
                    $emp_name = '';
                    foreach($employee_ids as $employee_id){
                        $emp_name .= Employee::get_emp_name_by_id($employee_id).',';
                    }
                    $emp_name = substr($emp_name,0,-1);

                    $html = Forms::action_snippet(["color" => "success",
                                "isDropDown" => true,
                                "data_id" => $d,
                                "ActionOption" => [
                                    ["title" => CommonLanguage::getMessage("Edit"),
                                        'data-target' => '#table_modal',
                                        "class" => "edit_table",
                                        "icon" => "fa-pencil",
                                        "attr" => 'employee-id="'.$row['3'].'"   employee-name="'.$emp_name.'"  is-radius="'.$row['is_radius'].'"',
                                        "permission" => "edit_table"
                                    ],
                                    ["title" => CommonLanguage::getMessage("delete"),
                                        "class" => "delete_table",
                                        "icon" => "fa-trash",
                                        "permission" => "delete_table"
                                    ]
                    ]]);
                    return $html;
                }),'employee_id','is_radius'
        );
        $key = 'table_id';
        $table = 'tbl_table';
        $data = DataTable::complex($_GET, $table, $key, $columns, ' status_id >= 1 AND floor_id = "' . $_GET['floor_id'] . '"');
        echo json_encode($data);
    }

    public function UpdateFloorTableRecords() {
        $floor_updates = CommonFunctions::UpdateRecordStatus('tbl_floor', 'floor_id', $_GET['entry_value'], 'status_id', '0');
        if (empty($floor_updates)) {
            $tables_updates = CommonFunctions::UpdateRecordStatus('tbl_table', 'floor_id', $_GET['entry_value'], 'status_id', '0');
            die(empty($tables_updates) ? "ok" : "error");
        } else {
            die("error");
        }
    }

    public function get_all_tables() {
        $all_tables = $this->_registry->restaurant_pos->setdata($_GET)->get_all_tables();
        die(json_encode($all_tables));
    }

    public function update_transfer_table() {
        $submit = $this->_registry->restaurant_pos->setdata($_POST)->transferTable();
        die(empty($submit) ? 'ok' : 'error');
    }

    public function ReportUser() {
        $condition = '';
        $condition .= $_GET['User_ID'] != 0 ? ' AND Close_ID = "' . $_GET['User_ID'] . '"' : '';
        $condition .= ' AND date_created >= "' . $_GET['start_date'] . '" ';
        $condition .= ' AND date_created <= "' . $_GET['end_date'] . '" ';
        $columns = array('User_Name', 'total_amount', 'total_paid', 'date_created');
        $key = 'Invoice_ID_Auto';
        $table = 'report_user_invoices';
        //                                echo $condition;
        $data = DataTable::complex($_GET, $table, $key, $columns, ' 1=1 ' . $condition);
        echo json_encode($data);
    }

    public function delivery_list() {
        $columns = array('Invoice_ID_Auto', 'customer_name', 'address_name', 'customer_phone', 'Invoice_Net_Total', array(
                'db' => 'Invoice_ID_Auto',
                'dt' => 5,
                'formatter' => function( $d) {
                    return '<button class="btn print_order" data-id="' . $d . '"><i class="fa fa-print"></i></button>
                       <button class="btn view_order openFrame" data-src="POSAjax/viewInvoicePOS?Invoice_ID_Auto=' . $d . '"><i class="fa fa-eye"></i>  </button>
                       <button class="btn generate_invoice" data-id="' . $d . '"><i class="fa fa-dollar"></i>  </button>';
                }));
        $key = 'Invoice_ID_Auto';
        $table = 'delivery_list';
        //                                echo $condition;
        $data = DataTable::complex($_GET, $table, $key, $columns, ' Is_Closed <> 1');
        echo json_encode($data);
    }

    //    public function AjaxAmount(){
    //        $data = $this->_registry->restaurant_pos->getTotalCash($_GET['User_ID'],$_GET['start_date'],$_GET['end_date']);
    //        echo json_encode($data);
    //    }
    public function submitcriteria() {
        //        die('heree');
        $_POST['criteria_details'] = json_decode($_POST['criteria_details'], true);
        $submit = $this->_registry->restaurant_pos->setData($_POST)->SubmitCriteria();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }

    public function submitcustomeraddress() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->submitAddress();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }

    public function submitService() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->submitService();
        if (empty($submit)) {
            smc_session()['default_setting']['pos_service'] = $_POST['setting_value'];
            die(json_encode([CommonLanguage::getMessage('Submitted_Successfully'), 'success']));
        }
        die(json_encode([CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }


    public function submitProductRate() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->submitProductRate();
        if (empty($submit)) {
            smc_session()['default_setting']['product_rate'] = $_POST['setting_value2'];
            die(json_encode([CommonLanguage::getMessage('Submitted_Successfully'), 'success']));
        }
        die(json_encode([CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }

    public function submitProductRateTypeOne() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->submitProductRateTypeOne();
        if (empty($submit)) {
            smc_session()['default_setting']['rate_type_one'] = $_POST['setting_value3'];
            die(json_encode([CommonLanguage::getMessage('Submitted_Successfully'), 'success']));
        }
        die(json_encode([CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }
    public function submitProductRateTypeTwo() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->submitProductRateTypeTwo();
        if (empty($submit)) {
            smc_session()['default_setting']['rate_type_two'] = $_POST['setting_value4'];
            die(json_encode([CommonLanguage::getMessage('Submitted_Successfully'), 'success']));
        }
        die(json_encode([CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }


    public function submitLebanonVat() {
         $submit = $this->_registry->restaurant_pos->setData($_POST)->submitLebanonVat();
        if (empty($submit)) {
            smc_session()['default_setting']['lebanon_vat_amount'] = $_POST['setting_value5'];
            die(json_encode([CommonLanguage::getMessage('Submitted_Successfully'), 'success']));
        }
        die(json_encode([CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }


    public function submitDelivery() {
        $submit = $this->_registry->restaurant_pos->setdata($_POST)->submitDeliveryData();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }

    public function getProductCriteria() {
        $id = $_GET['Product_ID'];
        $data = $this->_registry->restaurant_pos->setData($_GET)->getProductCriteria($id);
        echo json_encode($data);
    }

    public function addOnsList() {
        $columns = array('add_ons_title',
            array(
                'db' => 'add_ons_id',
                'dt' => 1,
                'formatter' => function( $d ) {
                    $html = Forms::action_snippet(["color" => "success",
                                "isDropDown" => true,
                                "data_id" => $d,
                                "ActionOption" => [
                                    ["title" => CommonLanguage::getMessage("Edit"),
                                        'data-target' => '#add_ons_modal',
                                        "class" => "edit_add_ons",
                                        "icon" => "fa-pencil",
                                        "permission" => "edit_add_ons"
                                    ],
                                    ["title" => CommonLanguage::getMessage("delete"),
                                        "class" => "delete_add_ons",
                                        "icon" => "fa-trash",
                                        "permission" => "delete_add_ons"
                                    ]
                    ]]);
                    return $html;
                })
        );
        $key = 'add_ons_id';
        $table = 'tbl_add_ons';
        $data = DataTable::complex($_GET, $table, $key, $columns);
        echo json_encode($data);
    }

    public function addOnsDetailsList() {
        $columns = array('add_ons_title', 'add_ons_details_title',
            array(
                'db' => 'add_ons_details_id',
                'dt' => 2,
                'formatter' => function( $d ) {
                    $html = Forms::action_snippet(["color" => "success",
                                "isDropDown" => true,
                                "data_id" => $d,
                                "ActionOption" => [
                                    ["title" => CommonLanguage::getMessage("Edit"),
                                        'data-target' => '#add_ons_details_modal',
                                        "class" => "edit_add_ons_details",
                                        "icon" => "fa-pencil",
                                        "permission" => "edit_add_ons_details"
                                    ],
                                    ["title" => CommonLanguage::getMessage("delete"),
                                        "class" => "delete_add_ons_details",
                                        "icon" => "fa-trash",
                                        "permission" => "delete_add_ons_details"
                                    ]
                    ]]);
                    return $html;
                })
        );
        $key = 'add_ons_details_id';
        $table = 'add_ons_details_list';
        $data = DataTable::complex($_GET, $table, $key, $columns);
        echo json_encode($data);
    }

    public function submitAddOns() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->submitAddOns();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [$submit[0], 'danger']));
    }

    public function submitAddOnsDetails() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->submitAddOnsDetails();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [$submit[0], 'danger']));
    }

    public function deleteAddOnsDetails() {
        $submit = $this->_registry->restaurant_pos->setData($_GET)->deleteAddOnsDetails();
        die(empty($submit) ? "ok" : $submit[0]);
    }

    public function deleteAddOns() {
        $submit = $this->_registry->restaurant_pos->setData($_GET)->deleteAddOns();
        die(empty($submit) ? "ok" : $submit[0]);
    }

    public function getOfflineIds() {
        $data = $this->_registry->restaurant_pos->getOfflineIds();
        die(json_encode($data));
    }

    public function print_table_invoice() {
        $id = $this->_registry->restaurant_pos->get_invoice_id_by_table($_GET['id']);
        $submit = $this->_registry->restaurant_pos->update_invoice_printed($id);
        if (empty($submit)) {
            if (smc_session()['default_setting']['sumatra_print'] == 1) {
                $this->_registry->invoicepos->posInvPOS($id);
            } else {
                die($id);
            }
        }
    }

    public function update_states() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->update_states();
        die(json_encode(empty($submit) ? [CommonLanguage::getMessage('Submitted_Successfully'), 'success'] : [CommonLanguage::getMessage('Failed_Submit'), 'danger']));
    }
    public function update_data() {
        $submit = $this->_registry->restaurant_pos->setData($_POST)->get_takeaway_pos();
        die(json_encode($submit));
    }

}
