<?php

require_once('Model/Queries.php');
require_once('Model/Constant.php');
require_once('Utility/Utility.php');
require_once('Object/DataObject.php');
require_once('Object/ResponseObject.php');
require_once('QueryBuilder/QueryDecorator.php');
require_once('QueryBuilder/CustomQuery.php');
require_once('../../config.php');

class PaymentRequest extends Constant implements Queries
{
    private $dtObject;
    private $actionType = "";
    private $functionalityType = "";
    public $postData = [];


    function __construct(DataObject $dtObject)
    {

        parent::__construct();
        $this->dtObject = $dtObject;
        $this->actionType = $dtObject->getActionType();
        $this->functionalityType = $dtObject->getFunctionalityType();
        $this->postData = $dtObject->getPostData();


    }

    public function create(){
        $listOfData = [];
        $postdata = $this->postData;
//        Utility::DebugObject($postdata);
        if ($this->functionalityType == "insert_about_us") {
            $subjectname = "About Us";
            $Detail = isEmpty($postdata['ckeditor']);

            $queryDecorator = new QueryDecorator("INSERT INTO AboutUs(subject,detail)VALUES('$subjectname','$Detail')", new CustomQuery("0", ""));

            $result = queryRunner($queryDecorator->getQuery());
            if ($result != null) {
                session_start();
                header("location:../admin_panel/index.php");

            }

            return $this->getResponseObject("202", "Data insert Successfully", "1", "0", $listOfData);


        }
        else if ($this->functionalityType == "insert_Privacy_policy") {
            $subjectname = "Privacy Policy";
            $Detail = isEmpty($postdata['ckeditor']);

            $queryDecorator = new QueryDecorator("INSERT INTO PrivacyPolicy(subject,detail)VALUES('$subjectname','$Detail')", new CustomQuery("0", ""));

            $result = queryRunner($queryDecorator->getQuery());
            if ($result != null) {
                session_start();
                header("location:../admin_panel/index.php");

            }

            return $this->getResponseObject("202", "Data insert Successfully", "1", "0", $listOfData);


        }

    }

    public function retrieve(){
        $listOfData = [];
        $postdata = $this->postData;
        if ($this->functionalityType == "retrieve_all_payment_request") {

            $search = $postdata['search'];

            $queryDecorator = new QueryDecorator("SELECT request.*,request.id as req_id,
            request.total_sales as t_sales,request.total_earning as t_earning,request.status as req_status,branddata.*,branddata.name as brand_name
        ,bank_detail.*,bank_detail.bank_name as b_name,bank_detail.iban_no as b_iban
            FROM BrandMonthlyEarning as request
            INNER JOIN Brand as branddata ON branddata.id = request.brand_id
            INNER JOIN BankingDetail as bank_detail  ON bank_detail.term_id = branddata.id
            WHERE bank_detail.meta_key = 'brand detail'
            ", new CustomQuery("0", ""));

            if (!empty($search)){
                $and_condition= "AND";

                $queryDecorator = new QueryDecorator($queryDecorator->getQuery(),
                    new CustomQuery("0", $and_condition." branddata.name LIKE '$search%'"));

            }

            //<editor-fold desc="Total Data Counting functionality">

            $queryDecoratorCount = new QueryDecorator($queryDecorator->getQuery(),new CustomQuery("1", "SELECT count(*) as total_count ,"));

            $resultCount = queryRunner($queryDecoratorCount->getQuery());
            $rowObject = rowRetrieverObject($resultCount);
            $rowCount = $rowObject->total_count;
            if ($rowCount > 10) {
                $rowCount = ceil($rowCount / 10);
            }
            else{
                $rowCount = 1;
            }


            //</editor-fold>
            $queryDecorator = new QueryDecorator($queryDecorator->getQuery(),
                new CustomQuery("0", " LIMIT 10 "));

            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
                    'id' => $row['req_id'],
                    't_sales' => $row['t_sales'],
                    't_earning' => $row['t_earning'],
                    'req_status' => $row['req_status'],
                    'brand_name' => $row['brand_name'],
                    'bank_name' => $row['b_name'],
                    'band_iban' => $row['b_iban'],



                ]);
            }
            return $this->getResponseObject("202", "Data Retrieve Successfully", "1", "0", $listOfData,Utility::getPaginationCssCode($rowCount));
        }
        else if ($this->functionalityType == "pagination") {

            $search = $postdata['search'];
            $offset = $postdata['offset'];

            $queryDecorator = new QueryDecorator("SELECT request.*,request.id as req_id,
            request.total_sales as t_sales,request.total_earning as t_earning,request.status as req_status,branddata.*,branddata.name as brand_name
        ,bank_detail.*,bank_detail.bank_name as b_name,bank_detail.iban_no as b_iban
            FROM BrandMonthlyEarning as request
            INNER JOIN Brand as branddata ON branddata.id = request.brand_id
            INNER JOIN BankingDetail as bank_detail  ON bank_detail.term_id = branddata.id
            WHERE bank_detail.meta_key = 'brand detail'
            ", new CustomQuery("0", ""));

            if (!empty($search)){
                $and_condition= "AND";

                $queryDecorator = new QueryDecorator($queryDecorator->getQuery(),
                    new CustomQuery("0", $and_condition." branddata.name LIKE '$search%'"));

            }


            $queryDecorator = new QueryDecorator($queryDecorator->getQuery(),
                new CustomQuery("0", " LIMIT 10 OFFSET ".$offset));

            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
                    'id' => $row['req_id'],
                    't_sales' => $row['t_sales'],
                    't_earning' => $row['t_earning'],
                    'req_status' => $row['req_status'],
                    'brand_name' => $row['brand_name'],
                    'bank_name' => $row['b_name'],
                    'band_iban' => $row['b_iban'],



                ]);
            }
            return $this->getResponseObject("202", "Data Retrieve Successfully", "1", "0", $listOfData);
        }


    }

    public function update(){
        $listOfData = [];
        $postdata = $this->postData;
        if ($this->functionalityType == "update_enable_data") {
            $id = $postdata['id'];
            $enable = $postdata['enable_val'];


            $queryDecorator = new QueryDecorator("UPDATE BrandMonthlyEarning SET status='$enable'", new CustomQuery("0", " WHERE id = '$id'"));

            $result = queryRunner($queryDecorator->getQuery());

            return $this->getResponseObject($this->getSuccess_code(), "Data update Successfully", "1", "0", $listOfData, '');

        }


    }

    public function delete(){



    }

    public function getResponseObject($code, $message, $setId, $itemCount, $data, $pagination = null)
    {

        $responseObject = new ResponseObject();
        $responseObject->setCode($code);
        $responseObject->setMessage($message);
        $responseObject->setId($setId);
        $responseObject->setListOfData($data);
        $responseObject->setTotalCount($itemCount);
        $responseObject->setPagination($pagination);

        return $responseObject;

    }
    public function getRefundDetail($id){

        ///Utility::Logger("Meta Id = ".$id);
        $listOfData = [];

        $query = " SELECT * FROM RefundMeta 
         where term_id = '$id'";
        $result = queryRunner($query);
        while ($row = rowRetriever($result)) {

            array_push($listOfData, [
                'id' => $row['id'],
                'image' => $this->refundimages.$row['image'],
            ]);
        }

        return $listOfData;


    }
}