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

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

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

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

    public function edit()
    {
        $id = $this->uri->segment(3);
        $data['customerdata'] = $this->Customer_model->getbyid($id);
        if (empty($data['customerdata'])) { show_404(); }
        $data['page_title'] = 'Edit Customer';
        $data['page_js']    = 'customerCreate.js';
        $data['breadcrumb'] = [['label' => 'Customer', 'url' => 'customer'], ['label' => 'Edit', 'active' => true]];
        $this->load->view('layout/header', $data);
        $this->load->view('layout/sidebar', $data);
        $this->load->view('customer/form', $data);
        $this->load->view('layout/footer', $data);
    }

    public function insert()
    {
        $customer_name = trim($this->input->post('customer_name'));
        $mobile        = trim($this->input->post('mobile'));

        // Same name + same mobile chi entry already aahe ka check
        if ($this->Customer_model->checkDuplicate($customer_name, $mobile) > 0) {
            echo json_encode(['status' => 'error', 'message' => 'Customer with this Name and Mobile No. already exists.']);
            return;
        }

        $fields = [
            'customer_name' => $customer_name,
            'mobile'        => $mobile,
            'email'         => $this->input->post('email'),
            'gst_no'        => $this->input->post('gst_no'),
            'address'       => $this->input->post('address'),
            'status'        => $this->input->post('status'),
            'is_deleted'    => 0,
            'created_date'  => date('Y-m-d H:i:s'),
        ];
        $this->Customer_model->insert($fields);
        echo json_encode(['status' => 'success']);
    }

    public function updatedata()
    {
        $id            = $this->input->post('id');
        $customer_name = trim($this->input->post('customer_name'));
        $mobile        = trim($this->input->post('mobile'));

        // Same name + same mobile chi entry already aahe ka check (current id sodun)
        if ($this->Customer_model->checkDuplicate($customer_name, $mobile, $id) > 0) {
            echo json_encode(['status' => 'error', 'message' => 'Customer with this Name and Mobile No. already exists.']);
            return;
        }

        $fields = [
            'customer_name' => $customer_name,
            'mobile'        => $mobile,
            'email'         => $this->input->post('email'),
            'gst_no'        => $this->input->post('gst_no'),
            'address'       => $this->input->post('address'),
            'status'        => $this->input->post('status'),
            'modified_date' => date('Y-m-d H:i:s'),
        ];
        $this->Customer_model->update($id, $fields);
        echo json_encode(['status' => 'success']);
    }

    public function delete($id = NULL)
    {
        // Route parameter म्हणून येतो ($id), नाहीतर URI segment
        if (empty($id)) $id = $this->uri->segment(3);
        if (empty($id)) $id = $this->uri->segment(4);
        if (empty($id)) {
            $this->session->set_flashdata('error', 'Invalid ID.');
            redirect('customer');
            return;
        }
        $id = (int)$id;
        // Direct query — model bypass करून
        $this->db->where('id', $id)->update('customer_master', [
            'is_deleted'    => 1,
            'modified_date' => date('Y-m-d H:i:s'),
        ]);
        $this->session->set_flashdata('success', 'Customer deleted.');
        redirect('customer');
    }

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