<?php

$last_synced = get_option("sync_time");
$site_url = get_site_url();
if ($last_synced == "") {
    echo "<div style='color: red;' '>WHMCS is not yet synced</div>";
    echo "<div><a href='$site_url/wp-admin/admin.php?page=whmp-sync'>Click here to Sync</a></div>";
}

$atts= shortcode_atts( [
	'html_template' => '',
	'type'          => whmpress_get_option( "dp_type" ),
	'years'         => whmpress_get_option( "dp_years" ),
	'tld'           => '.com',
	'currency'      => whmpress_get_option( "price_currency" ),
	"decimals"      => whmpress_get_option( "dp_decimals" ),
	"hide_decimal"  => whmpress_get_option( "dp_hide_decimal" ),

    "decimals_tag"  => whmpress_get_option( "dp_decimals_tag" ),
    "prefix"        => whmpress_get_option( "dp_prefix" ),
	"suffix"        => whmpress_get_option( "dp_suffix" ),
	"show_duration" => whmpress_get_option( "dp_show_duration" ),
    "duration_style" => whmpress_get_option("default_currency_duration_style"),
	"price_tax"     => whmpress_get_option( "dp_price_tax" ),
	"html_class"    => "whmpress whmpress_domain_price",
	"simple"        => "",
	"html_id"       => "",
	"no_wrapper"    => "",
], $atts );

$currency = $atts['currency'];
/*$currency_ = (empty($currency)) ? whmp_get_current_currency_id_i() : $currency;*/
if(empty($currency)) {
    $currency_ = whmp_get_current_currency_id_i();
}
else{
    $currency_  = $currency;
}



$currency_array = whmp_get_currency_info_i($currency_);





extract($atts);
//== Add by zain
if(empty($currency)){
    $currency = whmp_get_current_currency_id_i();
}

//== End
$show_duration_ = ((strtolower(trim($show_duration))) == "no") ? FALSE : TRUE;
$duration_tag = ($show_duration_) ? $show_duration : "";
$duration_tag = (strtolower($duration_tag) == "yes" || $duration_tag == "") ? "-" : $duration_tag;


$duration_style_ = $duration_style;

//$duration_connector_ = "";

$show_prefix_ = ((strtolower(trim($prefix))) == "no") ? FALSE : TRUE;
$prefix_tag = ($show_prefix_) ? $prefix : "";
$prefix_tag = (strtolower($prefix_tag) == "yes" || $prefix_tag == "" ) ? "-" : $prefix_tag;

$show_suffix_ = ((strtolower(trim($suffix))) == "no") ? FALSE : TRUE;
$suffix_tag = ($show_suffix_) ? $suffix : "";
$suffix_tag = ($suffix_tag == "yes" || $suffix_tag == "") ? "-" : $suffix_tag;

$decimals_tag = (trim($decimals_tag) == "") ? "-" : $decimals_tag;
$show_decimals_ = ((strtolower(trim($decimals_tag))) == "no") ? FALSE : TRUE;
$decimals_tag = ($show_decimals_) ? $decimals_tag : "";

$decimals_points_ = $decimals;

$hide_decimal_ = whmp_tfc($hide_decimal);

$simple_ = whmp_tfc($simple);

$html_class_ =$html_class;

$html_id_ = $html_id;
$html_class_ = $html_class;
$no_wrapper_ = whmp_tfc($no_wrapper);



$tld      = "." . ltrim( $tld, "." );

$AvailableTypes = [
	"domainregister",
	"domainrenew",
	"domaintransfer",
];


if ( ! in_array( $type, $AvailableTypes ) ) {
	$OutputString = __( 'Invalid type', 'whmpress' );
	return $OutputString;
}

$YearColumn = [
	"1"  => "msetupfee",
	"2"  => "qsetupfee",
	"3"  => "ssetupfee",
	"4"  => "asetupfee",
	"5"  => "bsetupfee",
	"6"  => "monthly",
	"7"  => "quarterly",
	"8"  => "semiannually",
	"9"  => "annually",
	"10" => "biennially",
];
if ( ! array_key_exists( $years, $YearColumn ) ) {
	$OutputString = __( "Invalid year", "whmpress" );
	return $OutputString;
}

if ( empty( $currency ) ) {

    //==Edit by zain
	/*$currency = whmp_get_currency();*/

    $currency = whmp_get_current_currency_id_i();
}

$price = whmp_domain_price_i( [
    "extension"   => $tld,
    "price_type"  => $type,
    "years"        => $years,
    "currency"    => $currency,
    "process_tax" => "1",
] );

$price_parts_array = whmp_format_price_i(
    [
        'price'       => $price,
        'paytype'     => 'recurring',
        'currency_id' => $currency,
        'decimals'    => whmpress_get_option('default_decimal_places')
    ]
);


$formated_price_array = whmp_format_domain_price_essentials_i( [
    'price' => $price_parts_array['price'],
    'billingcycle' => "annually",
    'years' => $years,
    'currency_id' => $currency,
    'duration_style' => $duration_style,
    'duration_connector' => whmpress_get_option( "default_currency_duration_connector" ),
]);



if (!($no_wrapper_)) {
    $wraper_start = "<span class='$html_class_' id='{$html_id}'>";
    $wraper_end = "</span>";
} else {
    $wraper_start = "";
    $wraper_end = "";
}

//make a function, flag, tag, string
if ($show_prefix_) {
    $prefix_s = ($prefix_tag != "-") ? "<" . $prefix_tag . ">" : "";
    $prefix_s .= whmp_get_currency_prefix($currency_array['id']);
    $prefix_s .= ($prefix_tag != "-") ? "</" . $prefix_tag . ">" : "";
} else {
    $prefix_s = "";
}

if ($show_suffix_) {
    $suffix_s = ($suffix_tag != "-") ? "<" . $suffix_tag . ">" : "";
    $suffix_s .= whmp_get_currency_suffix($currency_array['id']);
    $suffix_s .= ($suffix_tag != "-") ? "</" . $suffix_tag . ">" : "";
} else {
    $suffix_s = "";
}

$decimal_p = $price_parts_array["decimal_separator"];
$amount_p = $price_parts_array["amount"];
$fraction_p = $price_parts_array["fraction"];
if(empty($hide_decimal_)  && !empty($fraction_p))
{
    $fraction_p = $decimal_p . $fraction_p;
}


if ($show_decimals_) {
    $fraction_s = ($decimals_tag != "-") ? "<" . $decimals_tag . ">" : "";
    $fraction_s .= $fraction_p;
    $fraction_s .= ($decimals_tag != "-") ? "</" . $decimals_tag . ">" : "";
}


$price_s = $amount_p . $fraction_s;


if ($show_duration_) {
    $duration_s = ($duration_tag != "-") ? "<" . $duration_tag . ">" : "";
    $duration_s .= $formated_price_array["duration"];
    $duration_s .= ($duration_tag != "-") ? "</" . $duration_tag . ">" : "";
} else {
    $duration_s = "";
}

$OutputString = $wraper_start . $prefix_s . $price_s . $suffix_s . $duration_s . $wraper_end;

return $OutputString;