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

class Customers extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('DynamicModel', 'dm');
        
        if (!$this->session->userdata('logged_in')) {
            redirect('auth');
        }
    }

    public function index()
    {
        $data = ["active_menu" => "customers"];
        $this->load->view('partials/header', $data);
        $this->load->view('customers/layout');
        $this->load->view('partials/footer');
    }

    // =========================
    // DATATABLE
    // =========================
    public function get_data()
    {
        $this->dm->setTable('customers')
                 ->setColumn(
                     [null,'nama','alamat','telp','email',null],
                     ['nama','alamat','telp','email'],
                     ['id'=>'DESC']
                 );

        $list = $this->dm->get_datatables();

        $data = [];
        $no = $_POST['start'];

        foreach ($list as $row) {
            $no++;

            $aksi = '
                <button class="btn btn-sm btn-warning btn-edit" data-id="'.$row->id.'">Edit</button>
                <button class="btn btn-sm btn-danger btn-delete" data-id="'.$row->id.'">Delete</button>
            ';

            $data[] = [
                $no,
                $row->nama,
                $row->alamat,
                $row->telp,
                $row->email,
                $aksi
            ];
        }

        echo json_encode([
            "draw" => $_POST['draw'],
            "recordsTotal" => $this->dm->count_all(),
            "recordsFiltered" => $this->dm->count_filtered(),
            "data" => $data
        ]);
    }

    // =========================
    // SAVE
    // =========================
    public function save()
    {
        $data = [
            'nama'   => $this->input->post('nama'),
            'alamat' => $this->input->post('alamat'),
            'telp'   => $this->input->post('telp'),
            'email'  => $this->input->post('email')
        ];

        if (!$data['nama']) {
            echo json_encode(['status'=>false,'message'=>'Nama wajib diisi']);
            return;
        }

        $this->dm->setTable('customers')->insert($data);

        // ================= LOG ACTIVITY =================
        log_activity(
            'customers',
            'create',
            'Menambahkan customer: '.$data['nama'],
            'success'
        );

        echo json_encode(['status'=>true,'message'=>'Data berhasil disimpan']);
    }

    // =========================
    // DETAIL
    // =========================
    public function detail($id)
    {
        $data = $this->dm->setTable('customers')->get_by_id($id);
        echo json_encode($data);
    }

    // =========================
    // UPDATE
    // =========================
    public function update()
    {
        $id = $this->input->post('id');

        $data = [
            'nama'   => $this->input->post('nama'),
            'alamat' => $this->input->post('alamat'),
            'telp'   => $this->input->post('telp'),
            'email'  => $this->input->post('email')
        ];

        $this->dm->setTable('customers')->update($id, $data);
        
        // ================= LOG ACTIVITY =================
        log_activity(
            'customers',
            'update',
            'Mengubah customer ID: '.$id.' ('.$data['nama'].')',
            'success'
        );

        echo json_encode(['status'=>true,'message'=>'Data berhasil diupdate']);
    }

    // =========================
    // DELETE
    // =========================
    public function delete($id)
    {
        $this->dm->setTable('customers')->delete($id);

        // ================= LOG ACTIVITY =================
        log_activity(
            'customers',
            'delete',
            'Menghapus customer: '.($customer->nama ?? 'ID '.$id),
            'success'
        );

        echo json_encode(['status'=>true,'message'=>'Data berhasil dihapus']);
    }
}