<?xml version="1.0" encoding="utf-8"?>
<modification>
    <name>Order List Product Image</name>
    <code>dearsoft_order_list_image</code>
    <version>1.0.3</version>
    <author>DearSoft</author>
    <link>https://dev.deargroup.co</link>

    <!--
        1.0.3: two changes.

        (a) Fix column misalignment on the Order List page: the "Image"
        header (inserted right after the checkbox header) and the image
        CELL (previously inserted right before the customer cell, i.e. AFTER
        the order_id cell) were one column position apart from each other -
        so "Image" as a header sat above the order_id data, and "Order ID"
        sat above the image thumbnail. Fixed by anchoring the row's image
        cell on the checkbox cell's closing tag instead, so it lands in the
        same relative position as the header.

        (b) Add the same product-image column to the admin dashboard's
        "Latest Orders" widget (admin/controller/extension/dashboard/
        recent.php + admin/view/template/extension/dashboard/recent_info.twig),
        positioned after Order ID (no checkbox column exists there).
    -->

    <!-- ===== CONTROLLER: admin/controller/sale/order.php ===== -->
    <file path="admin/controller/sale/order.php">

        <operation error="skip">
            <search trim="true" index="0"><![CDATA[$this->load->model('sale/order');]]></search>
            <add position="after"><![CDATA[
		$this->load->model('tool/image');
		$this->load->model('catalog/product');]]></add>
        </operation>

        <operation error="skip">
            <search trim="true" index="0"><![CDATA[foreach ($results as $result) {]]></search>
            <add position="after"><![CDATA[
			$order_product_image = '';
			$order_products_for_image = $this->model_sale_order->getOrderProducts($result['order_id']);
			if (!empty($order_products_for_image) && !empty($order_products_for_image[0]['product_id'])) {
				$order_first_product = $this->model_catalog_product->getProduct($order_products_for_image[0]['product_id']);
				if ($order_first_product && !empty($order_first_product['image'])) {
					$order_product_image = $this->model_tool_image->resize(html_entity_decode($order_first_product['image'], ENT_QUOTES, 'UTF-8'), 50, 50);
				}
			}]]></add>
        </operation>

        <operation error="skip">
            <search trim="true" index="0"><![CDATA[$data['orders'][] = array(]]></search>
            <add position="after"><![CDATA[
				'product_image' => $order_product_image,]]></add>
        </operation>

    </file>

    <!-- ===== VIEW: admin/view/template/sale/order_list.twig ===== -->
    <file path="admin/view/template/sale/order_list.twig">

        <operation error="skip">
            <search trim="true"><![CDATA[<td style="width: 1px;" class="text-center"><input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /></td>]]></search>
            <add position="after"><![CDATA[
                    <td class="text-center" style="width:60px;">Image</td>]]></add>
        </operation>

        <!-- Row image cell: anchored on the checkbox cell's closing tag (not
             "before order.customer") so it lands directly after the checkbox
             cell, in the same column position as the "Image" header above -
             fixes the 1.0.2 misalignment. -->
        <operation error="skip">
            <search trim="true"><![CDATA[<input type="hidden" name="shipping_code[]" value="{{ order.shipping_code }}" /></td>]]></search>
            <add position="after"><![CDATA[
              <td class="text-center">
                {% if order.product_image %}
                  <img src="{{ order.product_image }}" alt="" width="50" height="50" style="border-radius:4px; object-fit:cover; border:1px solid #ddd;">
                {% else %}
                  <span style="color:#ccc;">-</span>
                {% endif %}
              </td>]]></add>
        </operation>

    </file>

    <!-- ===== DASHBOARD "Latest Orders" widget ===== -->
    <file path="admin/controller/extension/dashboard/recent.php">

        <operation error="skip">
            <search trim="true" index="0"><![CDATA[$this->load->model('sale/order');]]></search>
            <add position="after"><![CDATA[
		$this->load->model('tool/image');
		$this->load->model('catalog/product');]]></add>
        </operation>

        <operation error="skip">
            <search trim="true" index="0"><![CDATA[foreach ($results as $result) {]]></search>
            <add position="after"><![CDATA[
			$order_product_image = '';
			$order_products_for_image = $this->model_sale_order->getOrderProducts($result['order_id']);
			if (!empty($order_products_for_image) && !empty($order_products_for_image[0]['product_id'])) {
				$order_first_product = $this->model_catalog_product->getProduct($order_products_for_image[0]['product_id']);
				if ($order_first_product && !empty($order_first_product['image'])) {
					$order_product_image = $this->model_tool_image->resize(html_entity_decode($order_first_product['image'], ENT_QUOTES, 'UTF-8'), 50, 50);
				}
			}]]></add>
        </operation>

        <operation error="skip">
            <search trim="true" index="0"><![CDATA[$data['orders'][] = array(]]></search>
            <add position="after"><![CDATA[
				'product_image' => $order_product_image,]]></add>
        </operation>

    </file>

    <file path="admin/view/template/extension/dashboard/recent_info.twig">

        <operation error="skip">
            <search trim="true"><![CDATA[<td class="text-right">{{ column_order_id }}</td>]]></search>
            <add position="after"><![CDATA[
          <td class="text-center" style="width:60px;">Image</td>]]></add>
        </operation>

        <operation error="skip">
            <search trim="true"><![CDATA[<td class="text-right">{{ order.order_id }}</td>]]></search>
            <add position="after"><![CDATA[
          <td class="text-center">
            {% if order.product_image %}
              <img src="{{ order.product_image }}" alt="" width="40" height="40" style="border-radius:4px; object-fit:cover; border:1px solid #ddd;">
            {% else %}
              <span style="color:#ccc;">-</span>
            {% endif %}
          </td>]]></add>
        </operation>

    </file>

</modification>
