<?php
require dirname(dirname(__FILE__)) . '/inc/Config.php';
require dirname(dirname(__FILE__)) . '/inc/Crud.php';
header("Content-type: text/json");
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting(E_ALL);
$data = json_decode(file_get_contents("php://input"), true);
$store_id = $data["store_id"];
if ($store_id == "") {
    $returnArr = [
        "ResponseCode" => "401",
        "Result" => "false",
        "ResponseMsg" => "Something Went Wrong!",
    ];
} else {
    $total_medicine = $lundry->query(
        "select * from tbl_product where store_id=" . $store_id . ""
    )->num_rows;
    $product_attribute = $lundry->query(
        "select * from tbl_product_attribute where store_id=" . $store_id . ""
    )->num_rows;
    $total_category = $lundry->query(
        "select * from tbl_mcat where store_id=" . $store_id . ""
    )->num_rows;
    $total_faq = $lundry->query(
        "select * from tbl_faq where store_id=" . $store_id . ""
    )->num_rows;
    $total_timeslot = $lundry->query(
        "select * from tbl_time where store_id=" . $store_id . ""
    )->num_rows;
    $total_coupon = $lundry->query(
        "select * from tbl_coupon where store_id=" . $store_id . ""
    )->num_rows;
    $total_rider = $lundry->query(
        "select * from tbl_rider where store_id=" . $store_id . ""
    )->num_rows;
    
    $total_gallery_image = $lundry->query(
        "select * from tbl_photo where store_id=" . $store_id . ""
    )->num_rows;
    $total_normal_order = $lundry->query(
        "select * from tbl_normal_order where store_id=" . $store_id . ""
    )->num_rows;
    
	$sales  = $lundry->query("select sum(subtotal-cou_amt+d_charge+store_charge) as full_total from tbl_normal_order where status='completed'  and p_method_id=5 and  store_id=".$store_id."")->fetch_assoc();
             $payout =   $lundry->query("select sum(amt) as full_payouts from tbl_cash where rid=".$store_id."")->fetch_assoc();
                 
				
				$pb = 0;
				 if($sales['full_total'] == ''){$pb =  '0';}else {$pb  = number_format((float)($sales['full_total']) - $payout['full_payouts'], 2, '.', ''); }

    $total_earn = $lundry
        ->query(
            "select sum((subtotal-cou_amt+d_charge+store_charge) - ((subtotal-cou_amt+d_charge+store_charge) * commission/100)) as total_amt from tbl_normal_order where store_id=" .
                $store_id .
                " and status='Completed'"
        )
        ->fetch_assoc();
    $earn = empty($total_earn["total_amt"]) ? "0" : $total_earn["total_amt"];
  
    $total_payout = $lundry
        ->query(
            "select sum(amt) as total_payout from payout_setting where owner_id=" .
                $store_id .
                ""
        )
        ->fetch_assoc();
    $payout = empty($total_payout["total_payout"])
        ? "0"
        : $total_payout["total_payout"];

    $finalearn = floatval($earn)  - floatval($payout);

    $papi = [
        [
            "title" => "Product",
            "report_data" => $total_medicine,
            "url" => "images/dashboard/medicine.png",
        ],
		[
            "title" => "Category",
            "report_data" => $total_category,
            "url" => "images/dashboard/category.png",
        ],
		[
            "title" => "FAQ",
            "report_data" => $total_faq,
            "url" => "images/dashboard/faqs.png",
        ],
		[
            "title" => "Timeslot",
            "report_data" => $total_timeslot,
            "url" => "images/dashboard/timeslot.png",
        ],
		[
            "title" => "Coupon",
            "report_data" => $total_coupon,
            "url" => "images/dashboard/coupons.png",
        ],
		[
            "title" => "Rider",
            "report_data" => $total_rider,
            "url" => "images/dashboard/deliveryboy.png",
        ],
        [
            "title" => "Gallery",
            "report_data" => $total_gallery_image,
            "url" => "images/dashboard/gallery.png",
        ],
		[
            "title" => "Normal Order",
            "report_data" => $total_normal_order,
            "url" => "images/dashboard/myorders.png",
        ],
        [
            "title" => "Earning",
            "report_data" => $finalearn,
            "url" => "images/dashboard/myearning.png",
        ],
        [
            "title" => "Payout",
            "report_data" => floatval($payout),
            "url" => "images/dashboard/mypayment.png",
        ],
        [
            "title" => "Attribute",
            "report_data" => $product_attribute,
            "url" => "images/dashboard/product_attribute.png",
        ],
		[
            "title" => "COD Balance",
            "report_data" => floatval($pb),
            "url" => "images/dashboard/onhand.png",
        ]
    ];

    $returnArr = [
        "ResponseCode" => "200",
        "Result" => "true",
        "ResponseMsg" => "Report List Get Successfully!!!",
        "report_data" => $papi,
        "withdraw_limit" => $set["pstore"],
    ];
}
echo json_encode($returnArr);
?>