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

class Shifts extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        if (!isset($_SESSION['qurbani_user_id']) || $_SESSION['qurbani_role'] != 'sa') {
            redirect('/admin', 'refresh');
        }
        
        // Common Models
        $this->load->model('User_model', 'user');
        $this->load->model('Settings_model', 'setup');
        $this->load->model('Payment_model', 'payment');

        // Model for this Controller
        $this->load->model('Shifts_model', 'shifts');
        
    }

    public function index()
    {
        $data['total_sifts'] = $this->shifts->count_all();
        $data['AllShifts'] = $this->shifts->get_all();
        $data['settings'] = $this->setup->get_by_id(1);
        $data['title'] = 'Shifts';
        $data['mainContent'] = 'admin/shifts/index';
        $this->load->view('admin/layout/master', $data);
    }

    // function to add a Shift
    public function add()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $name = $this->input->post('name');
            $description = $this->input->post('description');
            $start_time = $this->input->post('start_time');
            $end_time = $this->input->post('end_time');

            // Prepare data to save the shift
            $options = [
                'name' => $name,
                'description' => $description,
                'start_time' => $start_time,
                'end_time' => $end_time
            ];

            // Call the model to create the shift
            $affected = $this->shifts->create($options);
            if ($affected) {
                $data['message'] = "Shift Created Successfully";
                redirect(base_url().'admin/settings#shiftsTab'); // Redirect to the list of shifts
            } else {
                $data['error'] = "Something Went Wrong! Please Try Again.";
                redirect(base_url().'admin/settings#shiftsTab'); // Redirect to the list of shifts
            }
        }
    }


    // function to update a Shift
public function edit()
{
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        $id = $this->input->post('id'); // ✅ fetch the ID first
        $name = $this->input->post('name');
        $description = $this->input->post('description');
        $start_time = $this->input->post('start_time');
        $end_time = $this->input->post('end_time');

        $options = [
            'name' => $name,
            'description' => $description,
            'start_time' => $start_time,
            'end_time' => $end_time
        ];

        $affected = $this->shifts->update($id, $options);
        if ($affected) {
            $this->session->set_flashdata('message', "Shift Updated Successfully");
        } else {
            $this->session->set_flashdata('error', "Something Went Wrong! Please Try Again.");
        }
        redirect(base_url().'admin/settings#shiftsTab');
    }
}
















    // function to delete a Shift
    public function delete($id)
    {
        $affected = $this->shifts->destroy($id);
        if($affected)
        {
            $data['message'] = "Shift Deleted Successfully";
        }
        redirect(base_url().'admin/settings#shiftsTab');
    }
}