<?php
session_start();
if (isset($_POST["inlineRadioOptions"])) {
    $claimStatus = $_POST["inlineRadioOptions"];
    $empId = $_POST["empId"];
    $presentDate = $_POST["presentDate"];
    $DepuationAllowance = $_POST["DepuationAllowance"];
    $DaAmount = $_POST["DaAmount"];
    $hotelBill =$_POST["hotelBill"];
    $otherBill =$_POST["otherBill"];
    $TotalAmount = $_POST["TotalAmount"];
    $deductedAmount = $_POST["loggedInUserDeductAmount"];
    $totalDeductedAmount = $_POST["allTotalDeductedAmount"];
    $deductedReason = $_POST["Reason"];
    $rejectReason = $_POST["Reason"];
    $loggedInUserID = $_SESSION['userId'];
    $loggedInUserName = $_SESSION['userName'];
    $loggedInUserDepartment = $_SESSION['teamLeads'];
    $EditedDate = (new \DateTime())->format('Y-m-d');
    $claimStatusUpdatedBy = $loggedInUserID . "," . $loggedInUserName;
    $uniqueID = $empId . '_' . $presentDate;
    
    $obj = new updateClaimStatus();
    if($claimStatus == CLAIM_TL_REJECTED || $claimStatus == CLAIM_FINANCE_REJECTED){
        $obj->rejectClaim($uniqueID, $claimStatus, $rejectReason, $EditedDate, $claimStatusUpdatedBy, $loggedInUserDepartment);
    }elseif($claimStatus==CLAIM_PAID){
        $obj->paidClaim($uniqueID, $claimStatus, $EditedDate, $claimStatusUpdatedBy);
    }elseif($claimStatus == CLAIM_TL_APPROVED || $claimStatus ==CLAIM_FINANCE_APPROVED){
    $obj->updateClaim($uniqueID, $claimStatus, $TotalAmount, $DepuationAllowance, $DaAmount, $hotelBill, $otherBill, $loggedInUserDepartment, $loggedInUserID, $loggedInUserName, $EditedDate, $claimStatusUpdatedBy, $deductedAmount, $totalDeductedAmount, $deductedReason);
    }


} else {
    header("location: login.php"); //if not login
}

//     $obj = new updateClaimStatus();
// $obj->updateClaim("200424", "2","2020-08-01","350", "No","0", "sdsd", "TL", "180291", "sai", "2020-09-23","180291sai","0","0", "reas");
class updateClaimStatus
{
    function __construct()
    {
        require_once dirname(__DIR__) .'../../php/DB_Connect.php';
        $DBclassObject = new DB_conncect;
        $this->db = $DBclassObject->connectToDB();

       
         }
    function updateClaim($uniqueID, $claimStatus, $TotalAmount, $DepuationAllowance, $DaAmount, $hotelBill, $otherBill, $loggedInUserDepartment, $loggedInUserID, $loggedInUserName, $EditedDate, $claimStatusUpdatedBy, $deductedAmount, $totalDeductedAmount, $deductedReason) // here  $Reason is not used, it need to insert
    {
 

        if ($loggedInUserDepartment == TL_DEPARTMENT) {
            $query = $this->db->prepare("UPDATE `tesf-2` SET `CLAIM_STATUS`=?, `TL_APPROVED_DEPUTATION_ALLOWANCE`=?, `TL_APPROVED_DA_AMOUNT`=?, `TL_APPROVED_HOTEL_BILLS`=?,`TL_APPROVED_OTHER_BILLS`=?, `TL_APPROVED_FINAL_AMOUNT`=?, `TL_APPROVED_DATE`=?, `TL_DEDUCTED_AMOUNT`=?,`TOTAL_DEDUCTED_AMOUNT`=?,`TL_DEDUCTED_REASON`=?, `FINANCE_APPROVED_DEPUTATION_ALLOWANCE`=?, `FINANCE_APPROVED_DA_AMOUNT`=?,`FINANCE_APPROVED_HOTEL_BILLS`=?, `FINANCE_APPROVED_OTHER_BILLS`=?, `FINANCE_APPROVED_FINAL_AMOUNT`=?,`CLAIM_STATUS_UPDATED_BY`=?  WHERE `UNIQUE_ID`=?");
            $query->bind_param("isddddsddssddddss", $claimStatus, $DepuationAllowance, $DaAmount, $hotelBill, $otherBill, $TotalAmount, $EditedDate, $deductedAmount, $totalDeductedAmount, $deductedReason, $DepuationAllowance, $DaAmount,$hotelBill, $otherBill, $TotalAmount, $claimStatusUpdatedBy, $uniqueID);
        } elseif ($loggedInUserDepartment == FINANCE_DEPARTMENT) {
            $query = $this->db->prepare("UPDATE `tesf-2` SET `CLAIM_STATUS`=?,`FINANCE_EMP_ID`=?,`FINANCE_EMP_NAME`=?,`FINANCE_APPROVED_DATE`=?,`FINANCE_APPROVED_DEPUTATION_ALLOWANCE`=?,`FINANCE_APPROVED_DA_AMOUNT`=?,`FINANCE_APPROVED_HOTEL_BILLS`=?,`FINANCE_APPROVED_OTHER_BILLS`=?,`FINANCE_APPROVED_FINAL_AMOUNT`=?, `FINANCE_DEDUCTED_AMOUNT`=?,`TOTAL_DEDUCTED_AMOUNT`=?,`FINANCE_DEDUCTED_REASON`=?, `CLAIM_STATUS_UPDATED_BY`=? WHERE `UNIQUE_ID`=?");
            $query->bind_param("issssddddddsss", $claimStatus, $loggedInUserID, $loggedInUserName, $EditedDate, $DepuationAllowance, $DaAmount, $hotelBill, $otherBill, $TotalAmount, $deductedAmount, $totalDeductedAmount, $deductedReason, $claimStatusUpdatedBy, $uniqueID);
        }


        $query->execute();
        //echo "Affected rows: " . mysqli_affected_rows($this->db);

        // if(mysqli_affected_rows($this->db)>0){
        if ($query->execute()) {
            echo  "UPDATED";
        } else {
            echo "FAILURE";
        }
    }

    function rejectClaim($uniqueID, $claimStatus, $rejectReason, $EditedDate, $claimStatusUpdatedBy, $loggedInUserDepartment)
    {
        // if ($claimStatus == CLAIM_TL_REJECTED && $loggedInUserDepartment == FINANCE_DEPARTMENT) {
        //     $claimStatus = CLAIM_FINANCE_REJECTED;
        // }


        $query = $this->db->prepare("UPDATE `tesf-2` SET `CLAIM_STATUS`=?, `REJECT_REASON`=?, `TL_OR_FINANCE_REJECTED_DATE`=?, `CLAIM_STATUS_UPDATED_BY`=?  WHERE `UNIQUE_ID`=?");
        $query->bind_param("issss", $claimStatus, $rejectReason, $EditedDate, $claimStatusUpdatedBy, $uniqueID);
      
        if ($query->execute()) {
            echo  "UPDATED";
        } else {
            echo "FAILURE";
        }

    }


    function paidClaim($uniqueID, $claimStatus, $EditedDate, $claimStatusUpdatedBy)
    {
        $query = $this->db->prepare("UPDATE `tesf-2` SET `CLAIM_STATUS`=?, `PAID_DATE`=?, `PAID_BY`=?  WHERE `UNIQUE_ID`=?");
        $query->bind_param("isss", $claimStatus, $EditedDate, $claimStatusUpdatedBy, $uniqueID);
        
        if ($query->execute()) {
            echo  "UPDATED";
        } else {
            echo "FAILURE";
        }

    }

}
