<?php

use app\libraries\FileUpload;

defined('BASEPATH') or exit('No direct script access allowed');

/**
 * @property Student_makeup_model $Student_makeup_model
 * @property Student_leave_model $Student_leave_model
 * @property Invoice_pay_model $Invoice_pay_model
 * @property Menu_badge_notification_model $Menu_badge_notification_model
 */
class Apply extends MY_ApiController
{
    public function makeup()
    {
        $this->is_post();
        $this->is_json();

        $params = $this->json;
        $tenantNo = $params['tenant_no'];

        $this->load->model('Student_makeup_model');
        $this->Student_makeup_model->setTenantNo($tenantNo, true);
        $this->Student_makeup_model->create($params);

        $this->load->model('Menu_badge_notification_model');
        $this->Menu_badge_notification_model->unread($this->Menu_badge_notification_model::NAME_MAKEUP);
        echo $this->respond(['success' => true]);
    }

    public function leave()
    {
        $this->is_post();
        $this->is_json();

        $params = $this->json;
        $base64 = $params['image'] ?? null;
        $filename = $params['name'] ?? null;
        $tenantNo = $params['tenant_no'];   
        $params['create_time'] = time();

        if ($base64 && $filename) {
            $fileUpload = new FileUpload();
            $target = sprintf(FCPATH . 'public/uploads/%s/leaves/%s', $tenantNo, $filename);
            $fileUpload->base64_image($base64, $target);
            $fileUpload->fix_image_orientation($target);
        }

        $this->load->model('Student_leave_model');
        $this->Student_leave_model->setTenantNo($tenantNo, true);
        $this->Student_leave_model->create($params);

        $this->load->model('Menu_badge_notification_model');
        $this->Menu_badge_notification_model->unread($this->Menu_badge_notification_model::NAME_LEAVE);
        echo $this->respond(['success' => true]);
    }

    public function pay()
    {
        $this->is_post();
        $this->is_json();

        $studentId = $this->json['student_id'];
        $invoiceId = $this->json['invoice_id'];
        // $image = $params['pathname'] ?? null;
        // $filename = $params['filename'] ?? null;
        $tenantNo = $this->json['tenant_no'];
        $type = $this->json['type'] == 'atm' ? 'ATM' : 'FPS';
        $filename = $this->json['filename'];
        $base64 = $this->json['base64Image'];

        $fileUpload = new FileUpload();
        $target = sprintf(FCPATH . 'public/uploads/%s/receipts/%s', $tenantNo, $filename);
        $fileUpload->base64_image($base64, $target);
        $fileUpload->fix_image_orientation($target);

        $this->load->model('Invoice_pay_model');
        $this->Invoice_pay_model->setTenantNo($tenantNo, true);
        $this->Invoice_pay_model->submit($invoiceId, $studentId, $filename, $type);

        $this->load->model('Menu_badge_notification_model');
        $this->Menu_badge_notification_model->unread($this->Menu_badge_notification_model::NAME_RECEIPT);
        echo $this->respond();
    }
}
