<title>Orders</title>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-6">
            <app-breadcrumb [title]="'Orders'" [active_item]="'Orders'"></app-breadcrumb>
        </div>
        <div class="col-sm-6 text-right">
            <div ngbDropdown class="btn-group">
                <div class="ml-2">

                    <button *ngIf="orders && orders.length > 0" type="button" class="btn btn-info filterbtn"
                        title="{{ 'tooltip_lbl_download_csv' |translate}}" (click)="downloadCsv()">
                        <i class="icon-download pr-1"></i>
                        <span>{{'lbl_downLoad_csv'| translate}}</span>
                    </button>
                    <button type="button" class="btn btn-info filterbtn" title="{{ 'tooltip_filter_order'|translate }}"
                        (click)="drawer.toggle();">
                        <i class="icon-filter pr-1"></i>
                        <span>{{'button_lbl_filter'| translate}}</span>
                    </button>
                </div>
            </div>
        </div>
        <div class="col-sm-12">
            <mat-drawer-container class="example-container" autosize>
                <div class="card">
                    <ng-container *ngIf="orders && orders.length > 0 else NoOrders">
                        <div class="contentcard table-responsive userlist-table lastcolumntable">
                            <table mat-table [dataSource]="orders"
                                class="mat-elevation-z8 table-striped table-vcenter box-shadow-none"
                                style="min-width: 1350px; box-shadow: none;" aria-label="">
                                <ng-container matColumnDef="id">
                                    <th mat-header-cell *matHeaderCellDef style="width:5%">#</th>
                                    <td mat-cell *matCellDef="let element; let i = index;" style="width:5%">
                                        {{(paginate.currentPage - 1) * paginate.pageSize + i + 1}}
                                    </td>
                                </ng-container>
                                <ng-container matColumnDef="imageUrl">
                                    <th mat-header-cell *matHeaderCellDef style="width: 8%;">{{'lbl_image'| translate}}
                                    </th>
                                    <td mat-cell *matCellDef="let element"
                                        style="padding-top: 8px; padding-bottom: 8px; width: 8%;">
                                        <div *ngIf="element.imageUrl else defaultServicePic">
                                            <img [src]="element.imageUrl" (error)="element.imageUrl=null"
                                                class="rounded-circle" width="50px" height="50px" alt="" />
                                        </div>
                                        <ng-template #defaultServicePic>
                                            <img src="../../../assets/admin/images/userprofile.png"
                                                class="rounded-circle" width="50px" height="50px" alt="" />
                                        </ng-template>
                                    </td>
                                </ng-container>
                                <ng-container matColumnDef="fullName">
                                    <th mat-header-cell *matHeaderCellDef style="width: 15%;">{{'lbl_full_name'|
                                        translate}}
                                    </th>
                                    <td mat-cell *matCellDef="let element" style="width: 15%;">
                                        <div *ngIf="element.fullName else NoValue">
                                            {{element.fullName}}
                                        </div>
                                        <ng-template #NoValue>
                                            --
                                        </ng-template>
                                    </td>
                                </ng-container>
                                <ng-container matColumnDef="contactNo">
                                    <th mat-header-cell *matHeaderCellDef>{{'lbl_contact_no'| translate}}</th>
                                    <td mat-cell *matCellDef="let element">
                                        <div *ngIf="element.contactNo else NoValue">
                                            {{element.contactNo}}
                                        </div>
                                        <ng-template #NoValue>
                                            --
                                        </ng-template>
                                    </td>
                                </ng-container>
                                <ng-container matColumnDef="address">
                                    <th mat-header-cell *matHeaderCellDef>{{'lbl_address'| translate}}</th>
                                    <td mat-cell *matCellDef="let element">
                                        <div *ngIf="element.addressLine1 || element.addressLine2 else NoValue">
                                            {{element.addressLine1}},{{element.addressLine2}}
                                        </div>
                                        <ng-template #NoValue>
                                            --
                                        </ng-template>
                                    </td>
                                </ng-container>
                                <ng-container matColumnDef="productName">
                                    <th mat-header-cell *matHeaderCellDef style="width: 14%;">{{'lbl_product_name'|
                                        translate}}</th>
                                    <td mat-cell *matCellDef="let element" style="width: 14%;">
                                        <div *ngIf="element.productName else NoValue">
                                            {{element.productName}}
                                        </div>
                                        <ng-template #NoValue>
                                            --
                                        </ng-template>
                                    </td>
                                </ng-container>
                                <ng-container matColumnDef="coin">
                                    <th mat-header-cell *matHeaderCellDef>{{'lbl_coin'| translate}}</th>
                                    <td mat-cell *matCellDef="let element">
                                        <div *ngIf="element.coin else NoValue">
                                            {{element.coin}}
                                        </div>
                                        <ng-template #NoValue>
                                            --
                                        </ng-template>
                                    </td>
                                </ng-container>
                                <ng-container matColumnDef="quantity">
                                    <th mat-header-cell *matHeaderCellDef>{{'lbl_quantity'| translate}}</th>
                                    <td mat-cell *matCellDef="let element">
                                        <div *ngIf="element.quantity else NoValue">
                                            {{element.quantity}}
                                        </div>
                                        <ng-template #NoValue>
                                            --
                                        </ng-template>
                                    </td>
                                </ng-container>
                                <ng-container matColumnDef="totalCoin">
                                    <th mat-header-cell *matHeaderCellDef>{{'lbl_total_coin'| translate}}</th>
                                    <td mat-cell *matCellDef="let element">
                                        <div *ngIf="element.totalCoin else NoValue">
                                            {{element.totalCoin}}
                                        </div>
                                        <ng-template #NoValue>
                                            --
                                        </ng-template>
                                    </td>
                                </ng-container>
                                <ng-container matColumnDef="orderDate">
                                    <th mat-header-cell *matHeaderCellDef style="width: 14%;">{{'orders_lbl_order_date'|
                                        translate}}</th>
                                    <td mat-cell *matCellDef="let element" style="width: 14%;">
                                        <div *ngIf="element.createdDate else NoValue">
                                            {{element.createdDate | date:'medium'}}
                                        </div>
                                        <ng-template #NoValue>
                                            --
                                        </ng-template>
                                    </td>
                                </ng-container>
                                <ng-container matColumnDef="orderStatus">
                                    <th mat-header-cell *matHeaderCellDef>{{'lbl_order_status'| translate}}</th>
                                    <td mat-cell *matCellDef="let element">
                                        <ng-container *ngIf="element.orderStatus">
                                            <span *ngIf="(element.orderStatus == 'ORDERED') else NoOrdered"
                                                style="color: #ff0000;">
                                                {{element.orderStatus}}
                                            </span>
                                            <ng-template #NoOrdered>
                                                <span style="color: #0b9d0b;">{{element.orderStatus}}</span>
                                            </ng-template>
                                        </ng-container>
                                    </td>
                                </ng-container>
                                <ng-container matColumnDef="action" stickyEnd>
                                    <th mat-header-cell *matHeaderCellDef translate class="text-right">{{'lbl_action'|
                                        translate}}</th>
                                    <td mat-cell *matCellDef="let element" class="text-right">
                                        <a *ngIf="element.orderStatus != 'DELIVERED' "
                                            title="{{ 'tooltip_lbl_change_status' |translate}}" href="javascript:;"
                                            class="btn btn-sm btn-info filterbtn"
                                            (click)="modalOpen(basicmodal,element)">
                                            <i class="icon-control-forward"></i>
                                        </a>
                                    </td>
                                </ng-container>

                                <tr mat-header-row *matHeaderRowDef="displayColumns; sticky: true"></tr>
                                <tr mat-row *matRowDef="let row; columns: displayColumns;" class="student-element-row"
                                    style="cursor: pointer;"></tr>F
                            </table>
                        </div>
                    </ng-container>

                    <ng-template #NoOrders>
                        <div class="d-flex vertical-center nodata">
                        </div>
                    </ng-template>

                    <div class="card-footer" *ngIf="orders && orders.length > 0">
                        <ul class="pagination mb-0 float-right">
                            <li [ngClass]="{ disabled: paginate.currentPage == 1 }" class="page-item page-prev">
                                <a class="page-link" href="javascript:;" tabindex="-1"
                                    (click)="setPage(paginate.currentPage - 1)">
                                    <span>{{'button_lbl_prev'| translate}}</span>
                                </a>
                            </li>
                            <li *ngFor="let page of paginate.pages" class="page-item active"
                                [ngClass]="{ active: paginate.currentPage == page }"><a
                                    [ngClass]="{current:paginate.currentPage == page}" class="page-link"
                                    href="javascript:;" (click)="setPage(page)">{{page}}</a></li>
                            <li [ngClass]="{ disabled: paginate.currentPage === paginate.totalPages}"
                                (click)="setPage(paginate.currentPage + 1)" class="page-item page-next">
                                <a class="page-link" href="javascript:;"><span>{{'button_lbl_next'|
                                        translate}}</span></a>
                            </li>
                        </ul>
                    </div>

                </div>

                <mat-drawer #drawer class="example-sidenav" mode="over" position="end">
                    <div class="col-lg-12 px-3 pt-3">
                        <div class="d-inline" style="width: 100%;"><i class="fa fa-filter"><span>{{'button_lbl_filter'|
                                    translate}}</span></i>
                            <button v class="close" aria-label="Close" (click)="drawer.toggle();">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                    </div>
                    <hr>
                    <div class="position px-3">
                        <div class="form-group">
                            <label>{{'lbl_search'| translate}}</label>
                            <input class="form-control" type="text" id="search" [(ngModel)]="searchString"
                                placeholder="{{'lbl_search'| translate}}" required>
                        </div>
                        <div class="form-group">
                            <label>{{'lbl_select_status'| translate}}</label>
                            <ng-select [items]="orderStauses" class="js-example-basic-single p-0"
                                placeholder="{{'orders_lbl_search_by_rder_tatus'|translate}}" bindLabel="status"
                                bindValue="id" name="selectedStatus" [(ngModel)]="selectedStatus" [multiple]="true">
                            </ng-select>
                        </div>
                        <div class="mt-3" style="padding-bottom: 15px;">
                            <div class="col-form-label">{{'lbl_from_date'| translate}}</div>
                            <mat-form-field appearance="outline" class="d-block w-100"
                                (click)="toggleMenuService.togglePickerStyles()">

                                <input matInput [matDatepicker]="pickerFrom" [max]="dateTo"
                                    placeholder="{{ 'lbl_from_date' |translate}}" [(ngModel)]="dateFrom" name="fromDate"
                                    [readonly]="true" (focus)="pickerFrom.open()"
                                    (click)="toggleMenuService.togglePickerStyles()">
                                <mat-datepicker-toggle matSuffix [for]="pickerFrom"
                                    (click)="toggleMenuService.togglePickerStyles()"></mat-datepicker-toggle>
                                <mat-datepicker #pickerFrom></mat-datepicker>
                            </mat-form-field>
                        </div>
                        <div class="col-form-label">{{'lbl_to_date'| translate}}</div>
                        <mat-form-field appearance="outline" class="d-block w-100"
                            (click)="toggleMenuService.togglePickerStyles()">
                            <input matInput [matDatepicker]="pickerTo" [min]="dateFrom"
                                placeholder="{{ 'lbl_to_date' |translate}}" [(ngModel)]="dateTo" name="toDate"
                                [readonly]="true" (focus)="pickerTo.open()"
                                (click)="toggleMenuService.togglePickerStyles()">
                            <mat-datepicker-toggle matSuffix [for]="pickerTo"
                                (click)="toggleMenuService.togglePickerStyles()"></mat-datepicker-toggle>
                            <mat-datepicker #pickerTo></mat-datepicker>
                        </mat-form-field>
                    </div>
                    <div class="modal-footer px-3" style="padding-bottom: 35px;justify-content: center;">
                        <div class="float-right d-inline">
                            <button type="button" class="btn btn-secondary filterbtn"
                                title="{{ 'tooltip_lbl_clear_filter' |translate}}"
                                (click)="drawer.toggle();clearSearch();">
                                <span>{{'button_lbl_clear'| translate}}</span>
                            </button>
                            <button type="button" class="btn btn-info filterbtn"
                                title="{{ 'tooltip_lbl_filter_dsa' |translate}}" (click)="drawer.toggle();setPage(1);">
                                <span>{{'lbl_search'| translate}}</span>
                            </button>
                        </div>
                    </div>

                    <div class="px-3">
                    </div>
                </mat-drawer>

            </mat-drawer-container>
        </div>
    </div>
</div>

<ng-template #basicmodal let-modal>
    <div>
        <form #OrderForm="ngForm">
            <div class="modal-header">
                <h4 class="modal-title" id="modal-basic-title">{{title}}</h4>
                <button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body pd-20">
                <div class="form-group">
                    <label for="orderStatusId">{{'lbl_order_status'| translate}} <span
                            style="color: red;">*</span></label>
                    <ng-select [items]="selectedOrderStatuses" class="js-example-basic-single p-0"
                        [(ngModel)]="selectedOrderStatus" placeholder="Select Loan Status" bindLabel="status"
                        bindValue="id" name="Status" required #orderStatusId="ngModel" name="orderStatusId">
                    </ng-select>
                    <mat-error
                        *ngIf="(orderStatusId!.touched) && (orderStatusId.errors && orderStatusId.errors['required'])">
                        {{'message_lbl_order_status_is_required'| translate}}
                    </mat-error>
                </div>
                <div *ngIf="selectedOrderStatus == 3">
                    <div class="form-group">
                        <label for="remark">{{'Remark'| translate}}</label>
                        <input class="form-control" type="text" name="orderremark" #orderRemark="ngModel"
                            [(ngModel)]="order.remark" onlyAlphabets>
                        <mat-error *ngIf="(orderRemark!.touched) && (orderRemark.errors?.['required'])">
                            {{'message_lbl_order_status_is_required'| translate}}
                        </mat-error>
                    </div>
                </div>

            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-sm btn-info filterbtn" (click)="changeStatus(OrderForm)">
                    {{'button_lbl_change'| translate}}
                </button>
                <button type="reset" class="btn btn-sm btn-secondary filterbtn" (click)="cancelUser()">
                    {{'button_lbl_cancel'| translate}}
                </button>
            </div>
        </form>
    </div>
</ng-template>