<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

function show_multi_tracking_details($order_id, $trackings, $tracking_settings, $products = [])
{
    ?>
    <div style="margin-top: 32px">
        <div style="margin: 32px;">
            <div style="text-align: center">
                <h1>Order #<?php echo esc_html( $order_id ); ?></h1>
            </div>
            <div style="display: flex; align-items: center; justify-content: center; margin-bottom: 64px;">
                <?php
                $count = sizeof($trackings);
                for ($i = 1; $i <= $count; $i++) { ?>
                    <h2 style="padding-right: 16px" class="trackfree_od_tl" id="tfodls_<?php echo esc_attr( $i ); ?>"><?php echo esc_html( $tracking_settings['cont_shipment'] . ' ' . $i ); ?><?php echo $i != $count ? ',' : '';?></h2>
                    <?php
                } ?>
            </div>
            <?php
            $i = 1;
            foreach ($trackings as $d) {
                $data = $d['data'];
                ?>
                <div class="trackfree_sf_dt" id="tfodts_<?php echo esc_attr( $i ); ?>">
                    <?php show_tracking_progress($data, $tracking_settings['pg_bar_color'], $tracking_settings);?>
                    <div style="margin: 64px 0;">
                        <div>
                            <div class="trackfree-sd-tt"><?php echo esc_html( $tracking_settings['cont_status'] ); ?>: <?php echo esc_html( $data['status'] ); ?></div>
                        </div>
                        <div style="display: flex; justify-content: left;">
                            <div>
                                <?php if ($data['shipmentDetails']['status']) { ?>
                                    <div style="margin-bottom: 32px;">
                                        <div class="trackfree-sd-tl">
                                            <?php echo esc_html( $data['shipmentDetails']['status'] ); ?>
                                        </div>
                                        <div>
                                            <?php echo esc_html( $data['shipmentDetails']['days'] ? $data['shipmentDetails']['days'] : $data['shipmentDetails']['date'] ); ?>
                                        </div>
                                    </div>
                                <?php }
                                if ($tracking_settings['show_cr_nm'] == 1) { ?>
                                    <div>
                                        <div class="trackfree-sd-tl">
                                            <?php echo esc_html( $tracking_settings['cont_carrier'] ); ?>
                                        </div>
                                        <div style="display: flex; align-items: center;">
                                            <div style="margin-right: 16px;">
                                                <img src="<?php echo esc_url( $data['courierLogo'] ); ?>" width="72px" height="72px" alt="" loading="lazy" />
                                            </div>
                                            <div>
                                                <a href="<?php echo esc_url( $data['carrierContact']['trackUrl'] ); ?>" target="_blank" rel="noopener noreferrer" style="color: #2c2d33;"><?php echo esc_html( $data['courierCaption'] ); ?></a>
                                                <div style="margin-top: 6px;">
                                                    <a href="tel:<?php echo esc_attr( $data['carrierContact']['phoneNumber'] ); ?>" style="color: #2c2d33;"><?php echo esc_html( $data['carrierContact']['phoneNumber'] ); ?></a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php }
                                if ($tracking_settings['show_track_num'] == 1) { ?>
                                    <div style="margin-top: 32px;">
                                        <div class="trackfree-sd-tl">
                                            <?php echo esc_html( $tracking_settings['cont_tracking_number'] ); ?>
                                        </div>
                                        <div>
                                            <?php echo esc_html( $data['trackingNum'] ); ?>
                                        </div>
                                    </div>
                                    <?php
                                }
                                if ($tracking_settings['show_product_info'] == 1 && isset($data['fulfillItems'])) {
                                   if (sizeof($data['fulfillItems']) > 0) {
                                       ?>
                                       <div style="margin-top: 32px;">
                                           <div class="trackfree-sd-tl">
                                               <?php echo esc_html( $tracking_settings['cont_product'] ); ?>
                                           </div>
                                           <div>
                                               <?php
                                               foreach ($data['fulfillItems'] as $item) {
                                                   ?>
                                                   <div class="tfree-st-fl">
                                                       <div>
                                                           <img src="<?php echo esc_url( $item['image'] ); ?>" alt="<?php echo esc_attr( $item['name'] ); ?>" width="48" height="48" loading="lazy" />
                                                       </div>
                                                       <div class="tfree-st-pn">
                                                           <?php echo esc_html( 'x' . $item['quantity'] . ' ' . $item['name'] ); ?>
                                                       </div>
                                                   </div>
                                                   <?php
                                               }
                                               ?>
                                           </div>
                                       </div>
                                       <?php
                                   }
                               } ?>
                            </div>
                        </div>
                        <div style="clear: both;"></div>
                    </div>
                    <?php
                    if ($tracking_settings['show_track_info'] == 1) {
                        show_tracking_timeline($data['trackDetails'], $tracking_settings['date_format'], $tracking_settings['time_format'], $data['courierCaption']);
                    }
                    ?>
                </div>
                <?php
                $i++;
            }
            ?>
            <?php if ($tracking_settings['show_rec_prds'] == 1 && sizeof($products) > 0) {
                show_rec_prds($products, $tracking_settings['cont_may_like']);
            } ?>
        </div>
    </div>
    <?php
}

function show_tracking_details($data, $tracking_settings, $products = [])
{
    ?>
    <div style="margin-top: 32px">
        <?php if ($data['orderId']) {?>
          <div style="margin: 64px; text-align: center">
              <h1><?php echo $tracking_settings['cont_order'];?> #<?php echo $data['orderId'];?></h1>
          </div>
        <?php } ?>
        <?php show_tracking_progress($data, $tracking_settings['pg_bar_color'], $tracking_settings);
        if ($data['trackingNum']) { ?>
            <div style="margin: 64px 0;">
                <div>
                    <div class="trackfree-sd-tt"><?php echo $tracking_settings['cont_status']; ?>: <?php echo $data['status'];?></div>
                </div>
                <div style="display: flex;">
                    <?php if ($tracking_settings['show_map'] == 1 && $data['currentLat'] && $data['currentLng']) { ?>
                    <div class="trackfree-cl-5" style="margin: 16px 0;">
                        <div id="trackfree_map"></div>
                    </div>
                    <?php } ?>
                    <div class="trackfree-cl-5 <?php if ($data['shipmentDetails']['status']) { ?>trackfree-sp-dt<?php } ?>">
                        <?php if ($data['shipmentDetails']['status']) { ?>
                            <div style="margin-bottom: 32px;">
                                <div class="trackfree-sd-tl">
                                    <?php echo $data['shipmentDetails']['status'];?>
                                </div>
                                <div>
                                    <?php echo $data['shipmentDetails']['days'] ? $data['shipmentDetails']['days'] : $data['shipmentDetails']['date'];?>
                                </div>
                            </div>
                            <?php
                        }
                        if ($tracking_settings['show_cr_nm'] == 1) { ?>
                            <div>
                                <div class="trackfree-sd-tl">
                                    <?php echo $tracking_settings['cont_carrier']; ?>
                                </div>
                                <div style="display: flex; align-items: center;">
                                    <div style="margin-right: 16px;">
                                        <img src="<?php echo esc_url( $data['courierLogo'] ); ?>" width="72px" height="72px" alt="" loading="lazy" />
                                    </div>
                                    <div>
                                        <a href="<?php echo esc_url( $data['carrierContact']['trackUrl'] ); ?>" target="_blank" rel="noopener noreferrer" style="color: #2c2d33;"><?php echo esc_html( $data['courierCaption'] ); ?></a>
                                        <div style="margin-top: 6px;">
                                            <a href="tel:<?php echo $data['carrierContact']['phoneNumber'];?>" style="color: #2c2d33;"><?php echo $data['carrierContact']['phoneNumber'];?></a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php
                        }
                        if ($tracking_settings['show_track_num'] == 1) { ?>
                        <div style="margin-top: 32px;">
                            <div class="trackfree-sd-tl">
                                <?php echo $tracking_settings['cont_tracking_number']; ?>
                            </div>
                            <div>
                                <?php echo $data['trackingNum'];?>
                            </div>
                        </div>
                        <?php }
                        if ($tracking_settings['show_product_info'] == 1 && isset($data['fulfillItems'])) {
                            if (sizeof($data['fulfillItems']) > 0) {
                                ?>
                                <div style="margin-top: 32px;">
                                    <div class="trackfree-sd-tl">
                                        <?php echo $tracking_settings['cont_product']; ?>
                                    </div>
                                    <div>
                                        <?php
                                        foreach ($data['fulfillItems'] as $item) {
                                            ?>
                                            <div class="tfree-st-fl">
                                                <div>
                                                    <img src="<?php echo esc_url( $item['image'] ); ?>" alt="<?php echo esc_attr( $item['name'] ); ?>" width="48" height="48" loading="lazy" />
                                                </div>
                                                <div class="tfree-st-pn">
                                                    <?php echo 'x' . $item['quantity'] . ' ' . $item['name'];?>
                                                </div>
                                            </div>
                                            <?php
                                        }
                                        ?>
                                    </div>
                                </div>
                                <?php
                            }
                        } ?>
                    </div>
                </div>
                <div style="clear: both;"></div>
            </div>
            <?php
        }
        if ($tracking_settings['show_track_info'] == 1) {
            show_tracking_timeline($data['trackDetails'], $tracking_settings['date_format'], $tracking_settings['time_format'], $data['courierCaption']);
        }
        if ($tracking_settings['show_rec_prds'] == 1 && sizeof($products) > 0) {
            show_rec_prds($products, $tracking_settings['cont_may_like']);
        } ?>
    </div>
    <?php if ($tracking_settings['show_map'] == 1 && $data['currentLat'] && $data['currentLng']) {
        show_map($data['currentLat'], $data['currentLng'], $data['currentLocation']);
    }
}

function show_tracking_progress($data, $color, $tracking_settings)
{ ?>
    <style>
    .trackfree-pg-id>li.trackfree-pg-ct .trackfree-pg-bl, .trackfree-pg-id>li.trackfree-pg-ct .trackfree-pg-bl:after, .trackfree-pg-id>li.trackfree-pg-ct .trackfree-pg-bl:before {
        background-color: <?php echo $color; ?>;
    }
    .trackfree-pg-dn::after {
      content: "";
      position: absolute;
      z-index: 2;
      right: 0;
      top: 0;
      transform: translateX(50%);
      width: 24px;
      height: 24px;
      background-color: <?php echo $color; ?>;
      border-radius: 50%;
    }
    .trackfree-pg-sn::before {
      content: "";
      position: absolute;
      z-index: 1;
      left: -2px;
      height: 100%;
      border-left: 5px <?php echo $color;?> solid;
    }
    </style>
    <div class="trackfree-pg-hr">
        <ul class="trackfree-pg-id">
            <li class="trackfree-pg-ct">
                <span class="trackfree-pg-bl"></span>
                <span class="trackfree-pr-tx"><?php echo $tracking_settings['cont_ordered']; ?></span>
                <div class="trackfree-pg-dt"><?php echo $data['orderDate'];?></div>
            </li>
            <li <?php if ($data['shipmentDate']) { ?> class="trackfree-pg-ct" <?php } ?>>
                <span class="trackfree-pg-bl"></span>
                <span class="trackfree-pr-tx"><?php echo $tracking_settings['cont_order_ready']; ?></span>
                <div class="trackfree-pg-dt"><?php echo $data['shipmentDate'];?></div>
            </li>
            <li <?php if ($data['currentStatus'] > 1) { ?> class="trackfree-pg-ct" <?php } ?>>
                <span class="trackfree-pg-bl"></span>
                <span class="trackfree-pr-tx"><?php echo $tracking_settings['cont_in_transit']; ?></span>
                <div class="trackfree-pg-dt"><?php echo $data['transitDate'];?></div>
            </li>
            <li <?php if ($data['currentStatus'] > 2) { ?> class="trackfree-pg-ct" <?php } ?>>
                <span class="trackfree-pg-bl"></span>
                <span class="trackfree-pr-tx"><?php echo $tracking_settings['cont_out_for_delivery']; ?></span>
                <div class="trackfree-pg-dt"><?php echo $data['pickupDate'];?></div>
            </li>
            <li  <?php if ($data['currentStatus'] > 3) { ?> class="trackfree-pg-ct" <?php } ?>>
                <span class="trackfree-pg-bl"></span>
                <span class="trackfree-pr-tx"><?php echo $tracking_settings['cont_delivered']; ?></span>
                <div class="trackfree-pg-dt"><?php echo $data['deliveryDate'];?></div>
            </li>
        </ul>
    </div>

    <div class="trackfree-pg-vr">
      <ul class="trackfree-sp-ev">
        <li>
            <span class="trackfree-pg-dn">
            </span>
            <span class="trackfree-pg-sn">
             <span class="trackfree-pr-tx"><?php echo $tracking_settings['cont_ordered']; ?></span>
             <div class="trackfree-pg-dt"><?php echo $data['orderDate'];?></div>
            </span>
        </li>
        <li>
            <span class=<?php echo $data['shipmentDate'] ? "trackfree-pg-dn" : "trackfree-pg-tm";?>>
            </span>
            <span class=<?php echo $data['shipmentDate'] ? "trackfree-pg-sn" : "trackfree-sp-sn";?>>
             <span class="trackfree-pr-tx"><?php echo $tracking_settings['cont_order_ready']; ?></span>
             <div class="trackfree-pg-dt"><?php echo $data['shipmentDate'];?></div>
            </span>
        </li>
        <li>
            <span class=<?php echo $data['currentStatus'] > 1 ? "trackfree-pg-dn" : "trackfree-pg-tm";?>>
            </span>
            <span class=<?php echo $data['currentStatus'] > 2 ? "trackfree-pg-sn" : "trackfree-sp-sn";?>>
             <span class="trackfree-pr-tx"><?php echo $tracking_settings['cont_in_transit']; ?></span>
             <div class="trackfree-pg-dt"><?php echo $data['transitDate'];?></div>
            </span>
        </li>
        <li>
            <span class=<?php echo $data['currentStatus'] > 2 ? "trackfree-pg-dn" : "trackfree-pg-tm";?>>
            </span>
            <span class=<?php echo $data['currentStatus'] > 3 ? "trackfree-pg-sn" : "trackfree-sp-sn";?>>
            <span class="trackfree-pr-tx"><?php echo $tracking_settings['cont_out_for_delivery']; ?></span>
             <div class="trackfree-pg-dt"><?php echo $data['pickupDate'];?></div>
            </span>
        </li>
        <li>
            <span class=<?php echo $data['currentStatus'] > 3 ? "trackfree-pg-dn" : "trackfree-pg-tm";?>>
            </span>
            <span class="trackfree-sp-sf">
            <span class="trackfree-pr-tx"><?php echo $tracking_settings['cont_delivered']; ?></span>
            <div class="trackfree-pg-dt"><?php echo $data['deliveryDate'];?></div>
            </span>
        </li>
      </ul>
    </div>
    <?php
}

function show_tracking_timeline($trackDetails, $date_format, $time_format, $courier_caption)
{ ?>
    <div style="margin-top: 32px">
      <ul class="trackfree-sp-ev">
        <?php $length = sizeof($trackDetails);
        $i = 0;
        $df = $tf = array(
            1 => 'M d, Y',
            2 => 'M d',
            3 => 'M jS, Y',
            4 => 'd M Y',
            5 => 'd-M-Y',
            6 => 'm/d/Y',
            7 => 'd/m/Y',
        );

        $datefm = $df[$date_format];

        $tf = array(
            1 => 'h:i a',
            2 => 'H:i',
        );
        $timefm = $tf[$time_format];

        $total_trans = 0;
        $tf_org_txt = [];
        $tf_rep_txt = [];
        $tf_tns_stn = get_option('trackfree_translate_strings');
        if ($tf_tns_stn) {
            $total_trans = sizeof ($tf_tns_stn['original_text']);
            $tf_org_txt = $tf_tns_stn['original_text'];
            $tf_rep_txt = $tf_tns_stn['replace_text'];
        }

        foreach ($trackDetails as $track) {
            $desc = $track['statusDescription'];
            $location = $track['city'];
            if ($total_trans > 0) {
                $desc = str_replace($tf_org_txt, $tf_rep_txt, $track['statusDescription']);
                $location = str_replace($tf_org_txt, $tf_rep_txt, $track['city']);
            }
            $track_date_time = $track['dateTime'] ? date($datefm . ' ' . $timefm, strtotime($track['dateTime'])) : '';
            if ($courier_caption == 'Shree Maruti Courier') {
                $track_date_time = $track['dateTime'] ? date($datefm, strtotime($track['dateTime'])) : '';
            }
            ?>
            <li>
            <span class="trackfree-sp-tm">
            </span>
            <span class=<?php echo $i + 1 === $length ? "trackfree-sp-sf" : "trackfree-sp-sn";?>>
              <span class="trackfree-st-at"><?php echo esc_html( ucfirst( trim( (string) $desc ) ) ); ?></span>
              <span class="trackfree-st-at"><?php echo esc_html( ucfirst( trim( (string) $location ) ) ); ?></span>
              <span class="trackfree-st-tm"><?php echo esc_html( $track_date_time ); ?></span>
            </span>
            </li>
            <?php $i++;?>
        <?php } ?>
      </ul>
    </div>
    <?php
}

function show_rec_prds($products, $title)
{ ?>
    <h2 style="text-align: center; margin: 32px 0;"><?php echo esc_html( $title ); ?></h2>
    <div class="trackfree-rp-cn">
        <?php foreach ($products as $product) { ?>
        <a class="trackfree-rp-ct" href="<?php echo esc_url( (string) $product['slug'] ); ?>" target="_blank" rel="noopener noreferrer">
            <img class="trackfree_prd_img" src="<?php echo esc_url( $product['image'] ); ?>" alt="<?php echo esc_attr( $product['name'] ); ?>" loading="lazy" />
            <div class="trackfree-rp-pd">
                <?php echo esc_html( $product['name'] ); ?>
            </div>
            <div class="trackfree-rp-pc">
                <?php echo wc_price($product['price']);?>
            </div>
        </a>
        <?php } ?>
    </div>
    <?php
}

function show_map($lat, $lng, $loc)
{ ?>
    <script>
    var map = L.map('trackfree_map').setView([<?php echo (float) $lat; ?>, <?php echo (float) $lng; ?>], 10);

    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
    }).addTo(map);

    L.marker([<?php echo (float) $lat; ?>, <?php echo (float) $lng; ?>]).addTo(map)
        .bindPopup('<b>Current location</b><br> <?php echo esc_js( $loc ); ?>')
        .openPopup();
    </script>
    <?php
}

function get_recommend_products($order_id)
{
    list($exclude_ids, $category_slugs, $category_names) = tfree_get_order_recommendation_context($order_id);
    $product_ids = tfree_get_recommended_product_ids($exclude_ids, $category_slugs, $category_names, 12);
    // Tracking page expects permalink + medium image.
    return tfree_build_recommend_products_payload($product_ids, 4, 'medium', 'permalink');
}

function show_trackfree_order_details($data, $tracking_settings, $products = [])
{
    ?>
    <div>
        <div style="margin: 64px 0 32px;">
            <div>
                Order #<strong><?php echo $data['id'];?></strong> was placed on <strong><?php echo date('F d, Y', strtotime($data['date_created']));?></strong> and is currently <strong><?php echo ucfirst($data['status']);?></strong>.
            </div>
        </div>
        <div>
            <h3>Order details</h3>
            <table>
                <tbody>
                    <?php
                    $subtotal = 0;
                    foreach ($data['line_items'] as $item) {
                        $subtotal += $item['total'];
                        ?>
                        <tr>
                            <td>
                                <?php echo $item['name'];?> <strong>x <?php echo $item['quantity'];?></strong>
                            </td>
                            <td style="text-align: right;">
                                <?php echo wc_price($item['total']);?>
                            </td>
                        </tr>
                        <?php
                    } ?>
                    <tr>
                        <td>
                            <strong>Subtotal</strong>
                        </td>
                        <td style="text-align: right;">
                            <?php echo wc_price($subtotal);?>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <strong>Shipping</strong>
                        </td>
                        <td style="text-align: right;">
                            <?php echo $data['shipping_total'] ? wc_price($data['shipping_total']) : 'Free shipping';?>
                        </td>
                    </tr>
                    <?php if ($data['total_tax'] > 0) { ?>
                        <tr>
                            <td>
                                <strong>Subtotal</strong>
                            </td>
                            <td style="text-align: right;">
                                <?php echo wc_price($data['total_tax']);?>
                            </td>
                        </tr>
                    <?php } ?>
                    <?php if ($data['discount_total'] > 0) { ?>
                        <tr>
                            <td>
                                <strong>Discount</strong>
                            </td>
                            <td style="text-align: right;">
                                <?php echo wc_price($data['discount_total']);?>
                            </td>
                        </tr>
                    <?php } ?>
                    <tr>
                        <td>
                            <strong>Total</strong>
                        </td>
                        <td style="text-align: right;">
                            <?php echo $data['total'];?>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div>
            <div class="trackfree-cl-6">
                <h3>Billing address</h3>
                <p>
                    <?php $billing_address = $data['billing']['first_name'] . ' ' . $data['billing']['last_name'] . '<br />';
                    $billing_address .= $data['billing']['address_1'] . '<br />';
                    if ($data['billing']['address_2']) {
                        $billing_address .= $data['billing']['address_2'] . '<br />';
                    }
                    $billing_address .= $data['billing']['city'] . ' ' .  $data['billing']['postcode'] . '<br />' . $data['billing']['state'];
                    $billing_address .= '<br /><span class="woocommerce-customer-details--phone">' . $data['billing']['phone'] . '</span>';
                    $billing_address .= '<br /><span class="woocommerce-customer-details--email">' . $data['billing']['email'] . '</span>';
                    echo $billing_address;?>
                </p>
            </div>
            <div class="trackfree-cl-6">
                <h3>Shipping address</h3>
                <p>
                    <?php $shipping_address = $data['shipping']['first_name'] . ' ' . $data['shipping']['last_name'] . '<br />';
                    $shipping_address .= $data['shipping']['address_1'] . '<br />';
                    if ($data['shipping']['address_2']) {
                        $shipping_address .= $data['shipping']['address_2'] . '<br />';
                    }
                    $shipping_address .= $data['shipping']['city'] . ' ' .  $data['shipping']['postcode'] . '<br />' . $data['shipping']['state'];
                    echo $shipping_address;?>
                </p>
            </div>
        </div>
        <div style="clear: both;"></div>
        <?php if ($tracking_settings['show_rec_prds'] == 1 && sizeof($products) > 0) {
            show_rec_prds($products, $tracking_settings['cont_may_like']);
        } ?>
    </div>
    <?php
}
?>
