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

class Category extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Category_model');
    }

    private function _get_id() {
        $s3 = $this->uri->segment(3);
        $s4 = $this->uri->segment(4);
        return is_numeric($s3) ? (int)$s3 : (is_numeric($s4) ? (int)$s4 : NULL);
    }

    public function index()
    {
        $data['page_title']   = 'Category Master';
        $data['breadcrumb']   = [['label' => 'Masters'], ['label' => 'Category', 'active' => true]];
        $data['categorydata'] = $this->Category_model->selectall();
        $this->load->view('layout/header', $data);
        $this->load->view('layout/sidebar', $data);
        $this->load->view('category/index', $data);
        $this->load->view('layout/footer', $data);
    }

    public function create()
    {
        $data['page_title'] = 'Add Category';
        $data['page_js']    = 'categoryCreate.js';
        $data['breadcrumb'] = [['label' => 'Category', 'url' => 'category'], ['label' => 'Add', 'active' => true]];
        $this->load->view('layout/header', $data);
        $this->load->view('layout/sidebar', $data);
        $this->load->view('category/form', $data);
        $this->load->view('layout/footer', $data);
    }

    public function edit()
    {
        $id = $this->_get_id();
        $data['categorydata'] = $this->Category_model->getbyid($id);
        if (empty($data['categorydata'])) { show_404(); }
        $data['page_title'] = 'Edit Category';
        $data['page_js']    = 'categoryCreate.js';
        $data['breadcrumb'] = [['label' => 'Category', 'url' => 'category'], ['label' => 'Edit', 'active' => true]];
        $this->load->view('layout/header', $data);
        $this->load->view('layout/sidebar', $data);
        $this->load->view('category/form', $data);
        $this->load->view('layout/footer', $data);
    }

    public function insert()
    {
        $fields = [
            'category_name' => $this->input->post('category_name'),
            'status'        => $this->input->post('status'),
            'is_deleted'    => 0,
            'created_date'  => date('Y-m-d H:i:s')
        ];
        $this->Category_model->insert($fields);
        echo json_encode(['status' => 'success']);
    }

    public function updatedata()
    {
        $id = $this->input->post('id');
        $fields = [
            'category_name' => $this->input->post('category_name'),
            'status'        => $this->input->post('status'),
            'modified_date' => date('Y-m-d H:i:s'),
        ];
        $this->Category_model->update($id, $fields);
        echo json_encode(['status' => 'success']);
    }

    public function delete()
    {
        $id = $this->_get_id();
        if (empty($id)) {
            $this->session->set_flashdata('error', 'Invalid ID.');
            redirect('category');
            return;
        }

        $this->db->db_debug = FALSE;
        $deleted = $this->Category_model->delete($id);

        if ($deleted) {
            $this->session->set_flashdata('success', 'Category permanently deleted.');
        } else {
            $error = $this->db->error();
            if (!empty($error['code'])) {
                $this->session->set_flashdata('error', 'ही category आधीच product मध्ये वापरली आहे, त्यामुळे delete करता येत नाही.');
            } else {
                $this->session->set_flashdata('error', 'Category सापडली नाही किंवा आधीच delete झाली आहे.');
            }
        }
        redirect('category');
    }

    public function toggle_status()
    {
        $id = $this->_get_id();
        if (!empty($id)) {
            $row = $this->Category_model->getbyid($id);
            if (!empty($row)) {
                $new_status = $row[0]->status == 1 ? 0 : 1;
                $this->Category_model->update($id, ['status' => $new_status]);
                $this->session->set_flashdata('success', 'Status updated.');
            }
        }
        redirect('category');
    }
}
