<?php
session_start();

require_once '../Config/Database.php';
require_once '../Model/PaymentType.php';
$payment_type = new PaymentType();

$payment_types = $payment_type->read_all_payment_types();

if(isset($_GET['all_payment_types'])){
    echo json_encode(
        array("data"=>$payment_types)
    ); 
}

if(isset($_POST['add_payment_type'])){
    $payment_type_name = $_POST['payment_type_name'];
    $data = array(
        "payment_type_name"=>$payment_type_name
    );
    $success = $payment_type->add_payment_type($data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_payment_type'])){
    $payment_type_id = $_POST['payment_type_id'];
    $payment_type_name = $_POST['payment_type_name'];
    $data = array(
        "payment_type_name"=>$payment_type_name
    );
    $key = array(
        "payment_type_id"=>$payment_type_id
    );
    $success = $payment_type->update_payment_type($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['remove_payment_type'])){
    $payment_type_id = $_POST['payment_type_id'];
    $data = $payment_type->remove_payment_type($payment_type_id);
    if(!$data){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}