<?php

namespace App\Http\Controllers;

use App\HargaEmas;
use App\HistoryEmasSandaran;

use Illuminate\Http\Request;

use Carbon\Carbon;

class HargaEmasController extends Controller
{
    public function showAllHargaEmas()
    {
        return response()->json(HargaEmas::on('mysql7')->orderby('recno')->get());
    }

    public function getOneHargaEmas($karat){

        $karat_string = str_replace('-','.',$karat);

        $harga_emas = HargaEmas::on('mysql7')->select('harga')->where('karat','=',$karat_string)->first();

        return response()->json($harga_emas);
    }

    public function update($id, Request $request)
    {
        $harga_emas = HargaEmas::findOrFail($id);
        $harga_emas->update($request->all());

        return response()->json($harga_emas, 200);
    }

    public function updatehargaemas(Request $request)
    {
        $harini = Carbon::now();
        $harini = new Carbon;

        $emas_asal = HargaEmas::on('mysql7')->where('recno','=',$request->recno)->first();

        $hargaemas=HargaEmas::on('mysql7')->where('recno','=',$request->recno)->update(array('harga'=> $request->harga,'tarikhupdate' => $harini->toDateString()));

        $history_emas = HistoryEmasSandaran::on('mysql7')->create([
            'karat' => $emas_asal['karat'],
            't_efektif' => $harini->toDateString(),
            'm_efektif' => $harini->toTimeString(),
            'harga' => $request->harga
        ]);

        return response()->json($hargaemas, 200);
    }

    public function createhargaemas(Request $request)
    {
        $harini = Carbon::now();
        $harini = new Carbon;

        $karat_db = HargaEmas::on('mysql7')->where('karat','=',$request->karat)->count();

        if($karat_db == 0){
            $hargaemas=HargaEmas::on('mysql7')->create([ 
                'karat' => $request->karat,
                'keterangan'=> $request->keterangan,
                'harga' => $request->harga,
                'harga2' => $request->harga2,
                'tarikhupdate' => $harini->toDateString()
            ]);

            $history_emas = HistoryEmasSandaran::on('mysql7')->create([
                'karat' => $request->karat,
                't_efektif' => $harini->toDateString(),
                'm_efektif' => $harini->toTimeString(),
                'harga' => $request->harga
            ]);
            if($hargaemas){
                return response()->json(1, 201);
            }else{
                return response()->json("unsuccess", 201);
            }
        }else{
            return response()->json("unsuccess", 201);
        }
    }

    public function deletehargaemas($id)
    {
        $jemas = HargaEmas::on('mysql7')->where('recno','=',$id)->delete();

        return response('Deleted Successfully', 200);
    }

    public function rekodEmasSandaran(Request $request){
        $rekod = HistoryEmasSandaran::on('mysql7')->where('karat','=',$request->karat)->orderBy('t_efektif','desc')->orderBy('m_efektif','desc')->get();

        return response()->json($rekod);
    }


    public function showAllHargaEmasMobile(){
        $emas = HargaEmas::on('mysql7')->orderby('recno')->get();
        $hargaemas = [];
        foreach($emas as $index=>$harga){
            array_push($hargaemas,['mutu'=>$harga['keterangan'],'karat'=>$harga['karat'],'harga'=>$harga['harga']]);
        }
        return response()->json($hargaemas);
    }

    public function laporanhargaemas(Request $request)
    {
        $rekod = HistoryEmasSandaran::on('mysql7')
                ->where('t_efektif','>=',$request->mula)
                ->where('t_efektif','<=',$request->hingga)
                ->orderBy('t_efektif','desc')
                ->orderBy('m_efektif','desc')
                ->get();

        return response()->json($rekod);
    }

}