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

class Report_header extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Report_header_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']        = 'Report Header';
        $data['breadcrumb']        = [['label' => 'Transactions'], ['label' => 'Report Header', 'active' => true]];
        $data['reportheaderdata']  = $this->Report_header_model->selectall();
        $this->load->view('layout/header', $data);
        $this->load->view('layout/sidebar', $data);
        $this->load->view('report_header/index', $data);
        $this->load->view('layout/footer', $data);
    }

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

    public function edit()
    {
        $id = $this->_get_id();
        $data['reportheaderdata'] = $this->Report_header_model->getbyid($id);
        if (empty($data['reportheaderdata'])) { show_404(); }
        $data['page_title'] = 'Edit Report Header';
        $data['page_js']    = 'reportHeaderCreate.js';
        $data['breadcrumb'] = [['label' => 'Report Header', 'url' => 'report_header'], ['label' => 'Edit', 'active' => true]];
        $this->load->view('layout/header', $data);
        $this->load->view('layout/sidebar', $data);
        $this->load->view('report_header/form', $data);
        $this->load->view('layout/footer', $data);
    }

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

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

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

        if ($deleted) {
            $this->session->set_flashdata('success', 'Report Header permanently deleted.');
        } else {
            $this->session->set_flashdata('error', 'Report Header सापडले नाही किंवा आधीच delete झाले आहे.');
        }
        redirect('report_header');
    }
}
