<?php
namespace Simcify\Controllers;
use Simcify\File;
use Simcify\Auth;
use Simcify\Database;

class Expenses{

    /**
     * Get a sample view or redirect to it
     * 
     * @return \Pecee\Http\Response
     */
    public function get() {
        $stats = array();
        $title = __('pages.sections.expenses');
        $user = Auth::user();
        $accounts = Database::table('accounts')->where('user', $user->id)->orderBy("id", false)->get();
        $categories = Database::table('categories')->where('user',$user->id)->where('type','expense')->orderBy("id", false)->get();
        $incomecategories = Database::table('categories')->where('user',$user->id)->where('type','income')->orderBy("id", false)->get();
        $expenses = Database::table("expenses")->where("expenses`.`user", $user->id)->leftJoin("accounts", "expenses.account","accounts.id")->leftJoin("categories", "expenses.category","categories.id")->orderBy("expenses.id", false)->get("`expenses.id`", "`expenses.expense_date`", "`expenses.amount`", "`expenses.title`", "`expenses.attachment`", "`accounts.name`", "`categories.name` as category");
        $stats['spent'] = Database::table('expenses')->where('user', $user->id)->where('MONTH(`expense_date`)', date("m"))->sum('amount','total')[0]->total;
        if ($user->monthly_spending > 0) {
          $stats['percentage'] = round(($stats['spent'] / $user->monthly_spending) * 100);
        }else{
          $stats['percentage'] = 0;
        }
        return view('expenses',compact("user", "title", "stats", "accounts","expenses","stats","categories", "incomecategories"));
    }

    /**
     * Add expense
     * 
     * @return Json
     */
    public function add() {
      $user = Auth::user();
      $data = array(
          'title'=>escape(input('title')),
          'user'=>$user->id,
          'amount'=>input('amount'),
          'account'=>input('account'),
          'category'=>input('category'),
          'expense_date'=>date('Y-m-d',strtotime(input('expense_date'))),
          'note' => escape(input('note'))
      );
      Database::table('expenses')->insert($data);
      $expense_id= Database::table('expenses')->insertId();

      if (!empty(input('image_attachment'))) {
        $attachment = File::upload(input('image_attachment'), "attachment", array(
            "source" => "base64",
            "extension" => "png"
        ));
        if ($attachment['status'] == "success") {
            Database::table('expenses')->where("id", $expense_id)->update(array(
                "attachment" => $attachment['info']['name']
            ));
        }
      }

      if (!empty(input('file_attachment'))) {
        $attachment = File::upload(
          $_FILES['file_attachment'], 
          "attachment",
          array(
              "allowedExtesions" => 'pdf',
          )
        );
        if ($attachment['status'] == "success") {
            Database::table('expenses')->where("id", $expense_id)->update(array(
                "attachment" => $attachment['info']['name']
            ));
        }
      }

      if (input('account') != "00") {
        self::balance(input('account'), input('amount'), "minus");
      }
      return response()->json(responder("success", __('pages.messages.alright'), __('expenses.messages.add-success'), "reload()"));
    }

    /**
     * Account balance
     * 
     * @return true
     */
    public function balance($accountId, $amount, $action) {
      $account = Database::table('accounts')->where('id', $accountId)->first();
      if ($action == "plus") {
        $balance = $account->balance + $amount;
      }elseif ($action == "minus") {
        $balance = $account->balance - $amount;
      }
      Database::table('accounts')->where('id', $accountId)->update(array("balance" => $balance));
      return true;
    }


    /**
     * Expense update modal
     * 
     * @return \Pecee\Http\Response
     */
    public function updateview() {
        $user = Auth::user();
        $categories = Database::table('categories')->where('user',$user->id)->where('type','expense')->get();
        $accounts = Database::table('accounts')->where('user', $user->id)->orderBy("id", false)->get();
        $expense = Database::table('expenses')->where('id', input("expenseid"))->first();
        return view('includes/ajax/expense',compact("expense","accounts","categories"));
    }

    /**
     * Update expense
     * 
     * @return Json
     */
    public function update(){
      $expense = Database::table('expenses')->where('id', input("expenseid"))->first();
      $user = Auth::user();
      $data = array(
          'title'=>escape(input('title')),
          'amount'=>input('amount'),
          'account'=>input('account'),
          'category'=>input('category'),
          'expense_date'=>date('Y-m-d',strtotime(input('expense_date'))),
          'note' => escape(input('note'))
      );

      if (!empty(input('image_attachment'))) {
        $attachment = File::upload(input('image_attachment'), "attachment", array(
            "source" => "base64",
            "extension" => "png"
        ));
        if ($attachment['status'] == "success") {
            Database::table('expenses')->where("id", $expense->id)->update(array(
                "attachment" => $attachment['info']['name']
            ));
        }
      }

      if (!empty(input('file_attachment'))) {
        $attachment = File::upload(
          $_FILES['file_attachment'], 
          "attachment",
          array(
              "allowedExtesions" => 'pdf',
          )
        );
        if ($attachment['status'] == "success") {
            Database::table('expenses')->where("id", $expense->id)->update(array(
                "attachment" => $attachment['info']['name']
            ));
        }
      }

      if (input('amount') != $expense->amount && $expense->account > 0) {
        self::balance($expense->account, $expense->amount, "minus");
        self::balance($expense->account, input('amount'), "plus");
      }
      Database::table('expenses')->where('id',input('expenseid'))->update($data);
      return response()->json(responder("success", __('pages.messages.alright'), __('expenses.messages.edit-success'), "reload()"));
    }

    /**
     * Delete expense record
     * 
     * @return Json
     */
    public function delete(){
      $expense = Database::table('expenses')->where('id', input("expenseid"))->first();
      if (!empty($expense->account)) {
        self::balance($expense->account, $expense->amount, "plus");
      }
      if (!empty($expense->attachment)) {
        File::delete($expense->attachment, "attachment");
      }
      Database::table('expenses')->where('id',input('expenseid'))->delete();
      return response()->json(responder("success", __('pages.messages.alright'), __('expenses.messages.delete-success'), "reload()"));
    }


    /**
     * Delete expense record
     * 
     * @return Json
     */
    public function deleteAttachment(){
      $expense = Database::table('expenses')->where('id', input("expenseid"))->first();
      $user = Auth::user();
      if ($user->id == $expense->user){
        if (!empty($expense->attachment)) {
          File::delete($expense->attachment, "attachment");
        }
        Database::table('expenses')->where("id", input('expenseid'))->update(array(
          "attachment" => ''));
        return response()->json(responder("success", __('pages.messages.alright'), __('expenses.messages.delete-success'), "reload()"));
      }
    }


    public function viewAttach($id){
      $expense = Database::table('expenses')->where('id', $id)->first();
      if (!empty($expense->attachment)) {
        $user = Auth::user();
        if ($user->id == $expense->user){

          $filePath = getcwd().asset('uploads/attachment/'.$expense->attachment);

          if (substr($expense->attachment,-3) == 'pdf' ) {
            header('Content-type: application/pdf');
          } else {
            header('Content-type: image/png');
          }
          //header('Content-length' . filesize($filePath));
          readfile($filePath);
        } else
        {
          return view('errors/404');
        }
      } else
      {
        return view('errors/404');
      }
    }
}
