<title>Products</title>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-6">
            <app-breadcrumb [title]="'Products'" [active_item]="'Products'"></app-breadcrumb>
        </div>
        <div class="col-sm-6 text-right">
            <div ngbDropdown class="btn-group">
                <div class="ml-2">
                    <a *ngIf="isWritePermission" title="{{ 'products_tooltip_add_gift_product' |translate}}"
                        href="javascript:;" class="btn btn-sm btn-info filterbtn iconplusbtn" (click)="modalOpen(basicmodal)">
                        <i class="icon-plus"></i>
                    </a>

                    <button *ngIf="giftProducts && giftProducts.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="{{ 'products_tooltip_filter_gift_product' |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 autosize>
                <div class="card">
                    <ng-container *ngIf="giftProducts && giftProducts.length > 0 else NoGiftProducts">
                        <div class="contentcard table-responsive mat-elevation-z8">
                            <table mat-table [dataSource]="giftProducts"
                                class="mat-elevation-z8 table-striped table-vcenter"
                                style="min-width: 1200px; 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" alt="" width="50px" height="50px" />
                                        </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="name">
                                    <th mat-header-cell *matHeaderCellDef>{{'lbl_name'| translate}}</th>
                                    <td mat-cell *matCellDef="let element">
                                        <div *ngIf="element.name else NoValue">
                                            {{element.name}}
                                        </div>
                                        <ng-template #NoValue>
                                            --
                                        </ng-template>
                                    </td>
                                </ng-container>
                                <ng-container matColumnDef="description">
                                    <th mat-header-cell *matHeaderCellDef>{{'lbl_description'| translate}}</th>
                                    <td mat-cell *matCellDef="let element">
                                        <div class="a" *ngIf="element.description else NoValue">
                                            {{element.description}}
                                        </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="duration">
                                    <th mat-header-cell *matHeaderCellDef>{{'lbl_duration'| translate}}</th>
                                    <td mat-cell *matCellDef="let element">
                                        <div *ngIf="element.duration else NoValue">
                                            {{element.duration}}
                                        </div>
                                        <ng-template #NoValue>
                                            --
                                        </ng-template>
                                    </td>
                                </ng-container>
                                <!-- <ng-container matColumnDef="status">
                                    <th mat-header-cell *matHeaderCellDef translate>{{'lbl_status'| translate}}</th>
                                    <td mat-cell *matCellDef="let element">
                                        <ng-container *ngIf="element.isActive else InActive">
                                            <span class="label text-success d-flex">
                                                <div class="dot-label bg-success mr-1"></div>
                                                <span>{{'lbl_active'| translate}}</span>
                                            </span>
                                        </ng-container>
                                        <ng-template #InActive>
                                            <span class="label text-warning d-flex">
                                                <div class="dot-label bg-secondary mr-1"></div>
                                                <span>{{'lbl_in_active'| translate}}</span>
                                            </span>
                                        </ng-template>
                                    </td>
                                </ng-container> -->
                                <ng-container matColumnDef="createdDate">
                                    <th mat-header-cell *matHeaderCellDef>{{'lbl_request_date'| translate}}</th>
                                    <td mat-cell *matCellDef="let element">
                                        {{element.createdDate | date :'medium'}}
                                    </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" style="display: flex;">
                                        <div class="media-body text-right icon-state">
                                            <label
                                                title="{{ 'products_tooltip_active_inactive_gift_product' |translate}}"
                                                class="switch" style="top:7px" *ngIf="isEditPermission">
                                                <input type="checkbox" (click)="changeStatus(element)"
                                                    [(ngModel)]="element.isActive"><span class="switch-state"
                                                    [ngClass]="element.isActive ? 'bg-success' : 'bg-secondary'"></span>
                                            </label>
                                        </div>
                                        <a *ngIf="isEditPermission"
                                            title="{{ 'products_tooltip_edit_gift_product' |translate}}"
                                            href="javascript:;" class="btn btn-sm btn-info filterbtn text-right"
                                            style="padding: 10px 12px;border-radius: 10px;margin: 2px;"
                                            (click)="editDialog(element,basicmodal)">
                                            <i class="icon-pencil"></i>
                                        </a>
                                        <a *ngIf="isReadPermission" title="{{ 'tooltip_lbl_view_detail' |translate}}"
                                        style="padding: 10px 12px;border-radius: 10px;margin: 2px;"
                                            href="javascript:;" class="btn btn-sm btn-info filterbtn text-right"
                                            (click)="viewDetail(element,basicmodal)">
                                            <i class="icon-eye"></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>
                            </table>
                        </div>
                    </ng-container>

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

                    <div class="card-footer" *ngIf="giftProducts && giftProducts.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">
                            <input class="form-control" type="text" id="search" [(ngModel)]="searchString"
                                placeholder="{{'message_lbl_serach_by_name'|translate}}" required>
                        </div>
                    </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-sm 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-sm btn-info filterbtn"
                                title="{{ 'tooltip_lbl_filter_loans' |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 [formGroup]="giftForm">
            <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 mt-1 mb-1">
                <div class="form-group">
                    <label for="name">{{'lbl_name'| translate}} <span style="color: red;">*</span></label>
                    <input class="form-control" type="text" name="name" formControlName="name" onlyAlphabets
                        [readonly]="isReadonly" required>
                    <mat-error
                        *ngIf="(giftForm.get('name') && !giftForm.get('name')!.valid && giftForm.get('name')!.touched)">
                        {{'message_lbl_name_is_required'| translate}}
                    </mat-error>
                </div>
                <div class="form-group">
                    <label for="description">{{'lbl_description'| translate}} </label>
                    <textarea class="form-control" type="text" id="description" formControlName="description" rows="4"
                        cols="10" [readonly]="isReadonly"></textarea>

                </div>
                <div class="form-group">
                    <label for="coin">{{'lbl_coin'| translate}} <span style="color: red;">*</span></label>
                    <input class="form-control" type="text" name="coin" formControlName="coin" onlyNumbers
                        [readonly]="isReadonly" required>
                    <mat-error
                        *ngIf="(giftForm.get('coin') && !giftForm.get('coin')!.valid && giftForm.get('coin')!.touched)">
                        {{'message_lbl_coin_is_required'| translate}}
                    </mat-error>
                </div>
                <div class="form-group">
                    <label for="imageUrl">{{'lbl_gift_product_image'| translate}} <span
                            style="color: red;">*</span></label>
                    <div *ngIf="!giftForm.get('imageUrl')!.value else Image">
                        <a class="form-control" type="upload" id="imageUrl"
                            style="height: 150px; width: 100%; text-align: center; border-style: dashed; border-color: rgb(63, 105, 223);display: flex;align-items: center;"
                            required (click)="fileUpload.click()">
                            <div class="absolute text-center m-auto">
                                <i class="icon-plus" style="font-size:30px"></i>
                                <h4>{{'lbl_in_tap_to_upload_image'| translate}}</h4>
                            </div>
                        </a>
                    </div>
                    <ng-template #Image>
                        <div *ngIf="!isReadonly else Edit">
                            <a type="upload" id="imageUrl" required (click)="fileUpload.click()"
                                style="height: 200px; width: 80%; text-align: center; display: flex;align-items: center;">
                                <img [src]="giftForm.get('imageUrl')!.value" style="height: 200px; width: 80%;" alt="">
                            </a>
                        </div>
                        <ng-template #Edit>
                            <div>
                                <a type="upload" id="imageUrl"
                                    style="height: 200px; width: 80%; text-align: center; display: flex;align-items: center;">
                                    <img [src]="giftForm.get('imageUrl')!.value" style="height: 200px; width: 80%;"
                                        alt="">
                                </a>
                            </div>

                        </ng-template>

                    </ng-template>

                    <input type="file" #fileUpload id="imageUrl" (change)="selectedImage($event)" name="imageUrl"
                        style="display:none;" accept="image/*" />
                    <mat-error
                        *ngIf="(giftForm.get('imageUrl') && !giftForm.get('imageUrl')!.valid && giftForm.get('imageUrl')!.touched)">
                        {{'message_lbl_image_is_required'| translate}}
                    </mat-error>

                </div>
                <div class="form-group">
                    <label for="name">{{'lbl_duration'| translate}}</label>
                    <ng-select class="form-select" name="duration" formControlName="duration">
                        <ng-option value="Monthly" name="experienceTime">{{'Monthly'| translate}}</ng-option>
                        <ng-option value="Quarterly" name="experienceTime">{{'Quarterly'| translate}}</ng-option>
                        <ng-option value="Yearly" name="experienceTime">{{'Yearly'| translate}}</ng-option>
                    </ng-select>
                </div>
            </div>
            <div class="modal-footer">
                <button *ngIf="!isReadonly" class="btn btn-sm btn-info filterbtn" (click)="insertGiftProduct()">
                    {{'button_lbl_save'| translate}}
                </button>
                <button *ngIf="!isReadonly" class="btn btn-sm btn-secondary filterbtn" (click)="cancelUser()">
                    {{'button_lbl_cancel'| translate}}
                </button>
            </div>
        </form>
    </div>
</ng-template>