{% set offers = __SELF__.offers %}
{% set deleteConfirm = __SELF__.wantToDelete %}

<!-- This message is shawn when a post is succesfully published. -->
{% flash success %}
<p data-control="flash-message" data-interval="5" class="success">
    {{ message }}
</p>
{% endflash %}

<!-- My post ========================================================= -->
<section class="library">
    <div class="auto_container">
        <div class="library_wrap">
            <div class="product_head">
                <div class="product_title">
                    {{'auth.my_announces'|_}}
                </div>
                <div class="product_head-row">
                    <a href="{{ 'kabinet/add_offer'|page }}" class="add_post_btn">
                        <div class="post_icon">
                            <img src="{{'assets/images/svg/add.svg'|theme}}" alt="">
                        </div>
                        <div class="post_text">
                            {{ 'auth.add_announce'|_ }}
                        </div>
                    </a>
                </div>
            </div>

            {% if offers is empty %}
            <div class="my_advert-bg">
                <div class="chat_wall">
                    <div class="chat_wall_img">
                        <img src="{{'assets/images/big_logo.png'|theme}}" alt="logo">
                    </div>
                    <div class="chat_wall_text">
                        {{'auth.no_my_announces'|_}}
                    </div>
                </div>
            </div>
            <div class="mobile_add-btn">
                <a href="{{ 'kabinet/add_offer'|page }}" class="add_post_btn">
                    <div class="post_icon">
                        <img src="{{'assets/images/svg/add.svg'|theme}}" alt="">
                    </div>
                    <div class="post_text">
                        {{ 'auth.add_announce'|_ }}
                    </div>
                </a>
            </div>
            {% endif %}

            
            <div class="product_box">
                {% for offer in offers %}
                <div class="product_item wow bounceInUp"
                    
                    data-wow-duration="1s"

                    {% if loop.index > 4 %}
                        {% if loop.index - 3 < 10 %}
                            data-wow-delay=".{{ loop.index - 3 }}s"
                        {% else %}
                            data-wow-delay="{{ (loop.index - 3) / 10 }}s"
                        {% endif %}
                    {% else %}
                        {% if loop.index < 10 %}
                            data-wow-delay=".{{ loop.index }}s"
                        {% else %}
                            data-wow-delay="{{ loop.index / 10 }}s"
                        {% endif %}
                    {% endif %}
                >
                    <div class="item_head">
                        <div class="item_lot">
                            {{ 'page.prod_id'|_ }} №:
                        </div>
                        <div class="item_num">
                            № {{offer.id}}
                        </div>
                    </div>
                    <div class="inline_num">
                        № {{offer.id}}
                    </div>
                    <div class="item_photo">
                        {% if offer.images[0] %}
                        <img src="{{ offer.images[0].thumb(308,180,{'mode':'crop'}) }}" alt="">
                        {% else %}
                        <img src="{{'assets/images/big_logo.png'|theme}}" alt="">
                        {% endif %}
                    </div>
                    <div class="item_group">
                        <div class="item_title">
                            {{ offer.name }}
                        </div>
                        <div class="item_sub_title">
                            {{ offer.description }}
                        </div>
                    </div>
                    <div class="item_group">
                        <div class="item_sub_name">
                            {{ 'page.prod_amount'|_ }}:
                        </div>
                        <div class="item_name">
                            {{ offer.quantity }} {{ offer.measure.code }}.
                        </div>
                    </div>
                    <div class="item_group">
                        <div class="item_sub_name">
                            {{ 'page.prod_price'|_ }}:
                        </div>
                        <div class="item_name">
                            {{ offer.price }} {{offer.currency.code}} / 1 {{ offer.measure.code }}.
                        </div>
                    </div>
                    <div class="item_group">
                        <div class="item_sub_name">
                            {{ 'page.prod_finishdate'|_ }}:
                        </div>
                        <div class="item_name">
                            {{ offer.ends_at ? offer.ends_at|date('d.m.Y') : '' }}
                        </div>
                    </div>


                    {% if offer.status == "approved" and offer.ends_at >= __SELF__.today %}
                        <span class="show_span" style="color:green">{{ 'status_approved'|_ }}</span>
                        <div class="new_group">
                            <a href="{{ 'offer'|page({ slug: offer.slug, id: offer.id }) }}" class="item_btn">
                                {{'account.on_site'|_}}
                            </a>
                            <!-- 
                                data-request="onDeleteOffer"
                                data-request-data="deleting_product_id: {{offer.id}}"
                             -->
                            <a href="#"
                                class="item_btn delete_item_btn approved-post-delete-btn"
                                onclick="deletePostHandler({{ offer.id }})">
                                {{'account_delete'|_}}
                            </a>
                        </div>
                    {% elseif offer.status == "approved" and offer.ends_at < __SELF__.today %}
                        <span class="show_span" style="color:rgb(61, 58, 58)">{{ 'status_expired'|_ }}</span>
                        <div class="new_group">
                            <a href="{{ 'kabinet/edit_post'|page({ id: offer.id }) }}" class="item_btn">
                                {{'account.prolong'|_}}
                            </a>
                            <a href="#"
                                data-request="onDeleteOffer"
                                data-request-data="deleting_product_id: {{offer.id}}"
                                class="item_btn delete_item_btn">
                                {{'account_delete'|_}}
                            </a>
                        </div>

                    {% elseif offer.status == "denied" %}
                        <span class="show_span" style="color:red">{{ 'status_denied'|_ }}</span> <br>
                        <span class="show_span" style="color:red">{{ offer.status_note }}</span>
                        <div class="new_group">
                            <a href="{{ 'kabinet/edit_post'|page({ id: offer.id }) }}" class="item_btn">
                                {{'account.edit'|_}}
                            </a>
                            <a href="#"
                                data-request="onDeleteOffer"
                                data-request-data="deleting_product_id: {{offer.id}}"
                                class="item_btn delete_item_btn">
                                {{'account_delete'|_}}
                            </a>
                        </div>
                    {% elseif offer.status == "draft" %}
                        <span class="show_span" style="color:#ff9800">{{ 'status_draft'|_ }}</span>
                        <div class="new_group">
                            <a href="{{ 'kabinet/edit_post'|page({ id: offer.id }) }}" class="item_btn">
                                {{'account.complete'|_}}
                            </a>
                            <a href="#"
                                data-request="onDeleteOffer"
                                data-request-data="deleting_product_id: {{offer.id}}"
                                class="item_btn delete_item_btn">
                                {{'account_delete'|_}}
                            </a>
                        </div>
                    {% elseif offer.status == "new" %}
                        <span class="show_span" style="color:blue">{{ 'status_new'|_ }}</span>
                        <a href="#" style="pointer-events: none; background-color: cornflowerblue;" class="item_btn full_btn">
                            {{'account.admin_checking'|_}}
                        </a>
                    {% endif %}
                </div>
                {% endfor %}
            </div>

            {% if offers.total > offers.perPage %}

            <div class="cat_end">
                <a href="{{ offers.previousPageUrl|default('#') }}" class="cat_arrow left">
                    <img src="{{ 'assets/images/svg/arrow-right.svg'|theme }}" alt="">
                </a>
                <form method="get" class="cat_form">
                    <input type="text" value="{{offers.currentPage|default('1')}}" name="page">
                </form>
                <a href="{{ offers.nextPageUrl|default('#') }}" class="cat_arrow ">
                    <img src="{{ 'assets/images/svg/arrow-right.svg'|theme }}" alt="">
                </a>
                <div class="cat_page">
                    {{ (offers.total / offers.perPage)|round(0,'ceil') }} {{ 'page.pages'|_ }}
                </div>
            </div>

            {% endif %}

        </div>
    </div>
</section>
<!-- My post end ===================================================== -->

<script>
    function deletePostHandler(offerId) {
        let answer = confirm('{{ deleteConfirm }}');
        if(answer) {
            $(this).request('onDeleteOffer',{
                data: {
                    deleting_product_id: offerId
                }
            });
        }
        event.preventDefault()
    }
</script>
