<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\PurchasesModel;

class Purchases extends BaseController
{


    private $PurchasesModel;

    public function __construct()
    {

        $this->PurchasesModel      = new PurchasesModel();   
    }

    public function index()
    {
    

        $data   = [
            'title' => "Produk",
        ];

        return  view('app/good/main/good-main-css', $data) .
        view('app/good/main/good-main', $data) .
        view('app/good/main/good-main-js', $data);

    }

    public function add()
    {

        $data = [
            'title' => "Tambah Produk"
        ];

        return  view('app/good/add/good-add-css', $data) .
        view('app/good/add/good-add', $data) .
        view('app/good/add/good-add-js', $data);
    }


    public function detail($id = null)
    {


        $this->GoodModel->select('tbl_good.*, tbl_category.name AS category_name, tbl_unit.name AS unit_name');
        $this->GoodModel->join('tbl_category','tbl_category.id=tbl_good.category_id');
        $this->GoodModel->join('tbl_unit','tbl_unit.id=tbl_good.unit_id');
        $row = $this->GoodModel->find($id);


        $data = [
            'title' => "Detail Barang",
            'row'  => $row
        ];

        return  view('app/good/detail/detail-css', $data) .
        view('app/good/detail/detail', $data) .
        view('app/good/detail/detail-js', $data);
    }

    public function save()
    {

        $request    = json_decode(json_encode($this->request->getPOST()));
        $rules = [
            'name' => [
                'rules' => 'required|min_length[3]|max_length[256]',
                'errors' => [
                    'required'   => 'Nama wajib diisi',
                    'min_length' => 'Nama minimal 3 karakter',
                    'max_length' => 'Nama tidak boleh lebih dari 256 karakter',
                ]
            ],
            'category' => [
                'rules' => 'required',
                'errors' => [
                    'required'   => 'Kategori wajib dipilih',
                ]
            ],
            'unit' => [
                'rules' => 'required',
                'errors' => [
                    'required'   => 'Satuan wajib dipilih',
                ]
            ],
            'price_buy' => [
                'rules' => 'required|numeric|greater_than_equal_to[0]',
                'errors' => [
                    'required' => 'Harga beli wajib diisi',
                    'numeric' => 'Harga beli harus berupa angka',
                    'greater_than_equal_to' => 'Harga beli tidak boleh negatif'
                ]
            ],
            'price_sale' => [
                'rules' => 'required|numeric|greater_than_equal_to[0]',
                'errors' => [
                    'required' => 'Harga jual wajib diisi',
                    'numeric' => 'Harga jual harus berupa angka',
                    'greater_than_equal_to' => 'Harga jual tidak boleh negatif'
                ]
            ],
            'discount' => [
                'rules' => 'permit_empty|numeric|greater_than_equal_to[0]|less_than_equal_to[100]',
                'errors' => [
                    'numeric' => 'Diskon harus berupa angka',
                    'greater_than_equal_to' => 'Diskon tidak boleh negatif',
                    'less_than_equal_to' => 'Diskon maksimal 100%'
                ]
            ],
            'description' => [
                'rules' => 'required|max_length[1000]',
                'errors' => [
                    'required' => 'Keterangan wajib diisi',
                    'max_length' => 'Keterangan maksimal 1000 karakter'
                ]
            ]
        ];


        if (!$this->validate($rules)) {
            $message = $this->validator->getErrors();
            return $this->response->setJSON(['code' => 200, 'success' => false, 'message' => $message]);
        }

            

        $this->GoodModel->name = $request->name;
        $this->GoodModel->category = $request->category;
        $this->GoodModel->unit = $request->unit;
        $this->GoodModel->price_buy = $request->price_buy;
        $this->GoodModel->price_sale = $request->price_sale;
        $this->GoodModel->discount = $request->discount;
        $this->GoodModel->description = $request->description;
        $this->GoodModel->image = isset($request->image) ? $request->image : "";

        echo json_encode($this->GoodModel->saveData());


    }


    public function getAllData()
    {
        
        $this->GoodModel->draw     = $this->request->getPOST('draw');
        $this->GoodModel->start    = $this->request->getPOST('start');
        $this->GoodModel->limit    = $this->request->getPOST('length');
        $this->GoodModel->search   = $this->request->getPOST('search')['value'];
        $this->GoodModel->columns  = $this->request->getPOST('columns');
        $this->GoodModel->order    = $this->request->getPOST('order');

        echo $this->GoodModel->getAllData();
    }

    public function delete()
    {

        $request;
        if ($this->request->hasHeader('Content-Type') && strpos($this->request->getHeaderLine('Content-Type'), 'application/json') !== false) {
            $request    = $this->request->getJSON();
        }else{
            $request    = $this->request->getPOST();
        }

        $request = json_decode(json_encode($request));


        $this->GoodModel->where('id', $request->id);
        if(!$this->GoodModel->delete()){

            return $this->response->setJSON([
                'code'      => 200,
                'success'   => false,
                'message'   => "Data gagal dihapus. pastikan anda terkoneksi dengan internet"
            ]);
        }

        return $this->response->setJSON([
            'code'      => 200,
            'success'   => true,
            'message'   => "Data berhasil dihapus"
        ]);

    }


    public function edit($id = null)
    {


        $this->GoodModel->select('tbl_good.*, tbl_category.name AS category_name, tbl_unit.name AS unit_name');
        $this->GoodModel->join('tbl_category','tbl_category.id=tbl_good.category_id');
        $this->GoodModel->join('tbl_unit','tbl_unit.id=tbl_good.unit_id');
        $row = $this->GoodModel->find($id);

        $data = [
            'title' => "Edit Barang",
            'row'   => $row
        ];

        return  view('app/good/edit/good-edit-css', $data) .
        view('app/good/edit/good-edit', $data) .
        view('app/good/edit/good-edit-js', $data);
        
    }



    public function update()
    {

        

        $request    = json_decode(json_encode($this->request->getPOST()));
        $rules = [
            'name' => [
                'rules' => 'required|min_length[3]|max_length[256]',
                'errors' => [
                    'required'   => 'Nama wajib diisi',
                    'min_length' => 'Nama minimal 3 karakter',
                    'max_length' => 'Nama tidak boleh lebih dari 256 karakter',
                ]
            ],
            'category_id' => [
                'rules' => 'required',
                'errors' => [
                    'required'   => 'Kategori wajib dipilih',
                ]
            ],
            'unit_id' => [
                'rules' => 'required',
                'errors' => [
                    'required'   => 'Satuan wajib dipilih',
                ]
            ],
            'price_buy' => [
                'rules' => 'required|numeric|greater_than_equal_to[0]',
                'errors' => [
                    'required' => 'Harga beli wajib diisi',
                    'numeric' => 'Harga beli harus berupa angka',
                    'greater_than_equal_to' => 'Harga beli tidak boleh negatif'
                ]
            ],
            'price_sale' => [
                'rules' => 'required|numeric|greater_than_equal_to[0]',
                'errors' => [
                    'required' => 'Harga jual wajib diisi',
                    'numeric' => 'Harga jual harus berupa angka',
                    'greater_than_equal_to' => 'Harga jual tidak boleh negatif'
                ]
            ],
            'discount' => [
                'rules' => 'permit_empty|numeric|greater_than_equal_to[0]|less_than_equal_to[100]',
                'errors' => [
                    'numeric' => 'Diskon harus berupa angka',
                    'greater_than_equal_to' => 'Diskon tidak boleh negatif',
                    'less_than_equal_to' => 'Diskon maksimal 100%'
                ]
            ],
            'description' => [
                'rules' => 'required|max_length[1000]',
                'errors' => [
                    'required' => 'Keterangan wajib diisi',
                    'max_length' => 'Keterangan maksimal 1000 karakter'
                ]
            ]
        ];



        if (!$this->validate($rules)) {
            $message = $this->validator->getErrors();
            return $this->response->setJSON(['code' => 200, 'success' => false, 'message' => $message]);
        }
            

        $this->GoodModel->id = $request->hide_id;
        $this->GoodModel->name = $request->name;
        $this->GoodModel->category_id = $request->category_id;
        $this->GoodModel->unit_id = $request->unit_id;
        $this->GoodModel->price_buy = $request->price_buy;
        $this->GoodModel->price_sale = $request->price_sale;
        $this->GoodModel->discount = $request->discount;
        $this->GoodModel->description = $request->description;
        $this->GoodModel->image = isset($request->image) ? $request->image : "";

        echo json_encode($this->GoodModel->updateData());



    }



}

