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

class Unit extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Unit_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'] = 'Unit Master';
        $data['breadcrumb'] = [['label' => 'Masters'], ['label' => 'Unit', 'active' => true]];
        $data['unitdata']   = $this->Unit_model->selectall();
        $this->load->view('layout/header', $data);
        $this->load->view('layout/sidebar', $data);
        $this->load->view('unit/index', $data);
        $this->load->view('layout/footer', $data);
    }

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

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

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

    public function updatedata()
    {
        $id = $this->input->post('id');
        $fields = [
            'unit_name'     => $this->input->post('unit_name'),
            'status'        => $this->input->post('status'),
            'modified_date' => date('Y-m-d H:i:s'),
        ];
        $this->Unit_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('unit');
            return;
        }

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

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

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