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

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

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

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

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

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

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

        $fields = [
            'supplier_name' => $supplier_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->Supplier_model->insert($fields);
        echo json_encode(['status' => 'success']);
    }

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

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

        $fields = [
            'supplier_name' => $supplier_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->Supplier_model->update($id, $fields);
        echo json_encode(['status' => 'success']);
    }

    public function delete($id = NULL)
    {
        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('supplier');
            return;
        }
        $id = (int)$id;
        $this->db->where('id', $id)->update('supplier_master', [
            'is_deleted'    => 1,
            'modified_date' => date('Y-m-d H:i:s'),
        ]);
        $this->session->set_flashdata('success', 'Supplier deleted.');
        redirect('supplier');
    }

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