<title>Partner Payout</title>

<div class="container-fluid">
  <div class="row">
    <div class="col-sm-6">
      <app-breadcrumb [title]="'Payouts'" [active_item]="'Payouts'">
      </app-breadcrumb>
    </div>
    <div class="col-sm-6 text-right">
      <div ngbDropdown class="btn-group">
        <div class="ml-2">
          <button type="button" class="btn btn-info filterbtn"
            title="{{ 'connector_tooltip_filter_partner_payout' |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="partnerPayouts && partnerPayouts.length > 0 else NoPayout">
            <div class="contentcard table-responsive userlist-table">
              <table mat-table [dataSource]="partnerPayouts"
                class=" mat-elevation-z8 table-striped table-vcenter box-shadow-none" aria-label="">
                <ng-container matColumnDef="id">
                  <th mat-header-cell *matHeaderCellDef style="width:75px">#</th>
                  <td mat-cell *matCellDef="let element; let i = index;" style="width:75px">
                    {{(paginate.currentPage - 1) * paginate.pageSize + i + 1}}
                  </td>
                </ng-container>
                <ng-container matColumnDef="partner">
                  <th mat-header-cell *matHeaderCellDef style="width: 30%;">{{ 'lbl_partner' | translate}}</th>
                  <td mat-cell *matCellDef="let element" style="width: 30%;">
                    <div *ngIf="element.fullName else NoValue">
                      {{element.fullName}}-{{element.contactNo}}
                    </div>
                    <ng-template #NoValue>
                      --
                    </ng-template>
                  </td>
                </ng-container>
                <ng-container matColumnDef="totalIN">
                  <th mat-header-cell *matHeaderCellDef>{{ 'payout_lbl_total_commission' | translate}}</th>
                  <td mat-cell *matCellDef="let element">
                    <div *ngIf="element.totalIn else NoValue">
                      {{element.totalIn | currency:'INR'}}
                    </div>
                    <ng-template #NoValue>
                      --
                    </ng-template>
                  </td>
                </ng-container>
                <ng-container matColumnDef="totalOut">
                  <th mat-header-cell *matHeaderCellDef>{{ 'payout_lbl_release_commission' | translate}}</th>
                  <td mat-cell *matCellDef="let element">
                    <div *ngIf="element.totalOut else NoValue">
                      {{element.totalOut | currency:'INR'}}
                    </div>
                    <ng-template #NoValue>
                      --
                    </ng-template>
                  </td>
                </ng-container>
                <ng-container matColumnDef="action">
                  <th mat-header-cell *matHeaderCellDef translate class="text-right">{{ 'lbl_action' | translate}}</th>
                  <td mat-cell *matCellDef="let element" class="text-right">
                    <a title="{{ 'connector_tooltip_view_history' |translate}}" href="javascript:;"
                      class="btn btn-sm btn-info filterbtn" (click)="viewPayoutHistory(element.id,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 #NoPayout>
            <div class="d-flex vertical-center nodata">
            </div>
          </ng-template>

          <div class="card-footer" *ngIf="partnerPayouts && partnerPayouts.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 pr-1"><span class="ml-1">{{
                  '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 for="fullName">{{ 'lbl_search' | translate}}</label>
              <input class="form-control" type="text" name="search" [(ngModel)]="searchString" />
            </div>
            <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]="filterPickerFrom" [max]="toDate"
                placeholder="{{ 'lbl_from_date' |translate}}" [(ngModel)]="fromDate" name="fromDate" [readonly]="true"
                (focus)="filterPickerFrom.open()" (click)="toggleMenuService.togglePickerStyles()">
              <mat-datepicker-toggle matSuffix [for]="filterPickerFrom"
                (click)="toggleMenuService.togglePickerStyles()">
              </mat-datepicker-toggle>
              <mat-datepicker #filterPickerFrom></mat-datepicker>
            </mat-form-field>
            <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]="FilterPickerTo" placeholder="{{ 'lbl_to_date' |translate}}"
                [(ngModel)]="toDate" name="toDate" [readonly]="true" (focus)="FilterPickerTo.open()"
                (click)="toggleMenuService.togglePickerStyles()">
              <mat-datepicker-toggle matSuffix [for]="FilterPickerTo" (click)="toggleMenuService.togglePickerStyles()">
              </mat-datepicker-toggle>
              <mat-datepicker #FilterPickerTo></mat-datepicker>
            </mat-form-field>
            <div class="form-group">
              <label>{{ 'lbl_select_service' | translate}}</label>
              <ng-select [items]="services" class="js-example-basic-single p-0" [(ngModel)]="selectedService"
                placeholder="{{'lbl_select_service'| translate}}" bindLabel="displayName" bindValue="id" name="service">
              </ng-select>
            </div>
          </div>
          <div class="modal-footer p-0" style="margin-bottom: 20px;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"
                style="margin-right: 5px;border-radius: 10px; padding:8px 14px"
                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>

    <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">
      <ng-container *ngIf="isInsertPayout">
        <div class="d-inline">
          <h3 class="mb-1">{{ 'lbl_bank_detail' | translate}} <span style="color: red;">*</span></h3>
        </div>
        <div class="d-inline float-right">
          <a title="{{ 'connector_tooltip_edit_bank_details' |translate}}" href="javascript:;"
            class="btn btn-sm btn-info filterbtn" style="padding: 5px;border-radius: 10px;margin: 5px 3px;"
            (click)="openEditBankDetailDialog()">
            <i class="icon-pencil"></i>
          </a>
        </div>
        <div class="border p-3">
          <h4 class="mb-1">{{ 'lbl_account_holder_name' | translate}}: {{dsa.partnerBankDetail.accountHolderName}} </h4>
          <h4 class="mb-1">{{ 'lbl_account_no' | translate}}: {{dsa.partnerBankDetail.accountNo}} </h4>
          <h4 class="mb-1">{{ 'lbl_bank' | translate}}: {{dsa.partnerBankDetail.bankName}} </h4>
          <h4 class="mb-1">{{ 'payout_lbl_ifsc' | translate}}: {{dsa.partnerBankDetail.ifscCode}} </h4>
        </div>
        <h4>{{ 'payout_lbl_partner_commission' | translate}}:{{partnerCommission | currency:"INR"}}</h4>
        <div class="form-group">
          <label for="name">{{ 'payout_lbl_payout_release' | translate}} <span style="color: red;">*</span></label>
          <input class="form-control" type="text" id="name" [formControl]="payout" required onlyNumbers
            (keyup)="checkPayout()">
          <mat-error *ngIf="(payout && !payout?.valid && payout!.touched)">
            {{ 'payout_message_lbl_payout_is_required' | translate}}
          </mat-error>
          <mat-error *ngIf="(commissionError)">
            {{ 'payout_message_lbl_payout_must_be_less_than_actual_commission' | translate}}
          </mat-error>
        </div>
      </ng-container>
      <ng-container *ngIf="isPayoutHistory">
        <button type="button" class="float-right mb-2 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>
        <div class="table-responsive border-top userlist-table">
          <table mat-table [dataSource]="payoutHistory" class=" maintable mat-elevation-z8 table-striped table-vcenter"
            aria-label="">
            <ng-container matColumnDef="id">
              <th mat-header-cell *matHeaderCellDef style="width:75px">#</th>
              <td mat-cell *matCellDef="let element; let i = index;" style="width:75px">
                {{i + 1}}
              </td>
            </ng-container>

            <ng-container matColumnDef="commission">
              <th mat-header-cell *matHeaderCellDef>{{ 'lbl_commission' | translate}}</th>
              <td mat-cell *matCellDef="let element">
                <div *ngIf="element.commission else NoValue">
                  {{element.commission | currency:'INR'}}
                </div>
                <ng-template #NoValue>
                  --
                </ng-template>
              </td>
            </ng-container>
            <ng-container matColumnDef="commissionPercent">
              <th mat-header-cell *matHeaderCellDef>{{ 'lbl_commission_in_percent' | translate}}</th>
              <td mat-cell *matCellDef="let element">
                <div *ngIf="element.commissionPercent else NoValue">
                  {{element.commissionPercent}}%
                </div>
                <ng-template #NoValue>
                  --
                </ng-template>
              </td>
            </ng-container>
            <ng-container matColumnDef="serviceName">
              <th mat-header-cell *matHeaderCellDef>{{ 'lbl_service' | translate}}</th>
              <td mat-cell *matCellDef="let element">
                <div *ngIf="element.serviceName else NoService">
                  {{element.serviceName}}
                </div>
                <ng-template #NoService>
                  --
                </ng-template>
              </td>
            </ng-container>
            <ng-container matColumnDef="bankName">
              <th mat-header-cell *matHeaderCellDef>{{ 'lbl_bank' | translate}}</th>
              <td mat-cell *matCellDef="let element">
                <div *ngIf="element.bankName else NoBank">
                  {{element.bankName}}
                </div>
                <ng-template #NoBank>
                  --
                </ng-template>
              </td>
            </ng-container>
            <ng-container matColumnDef="loanDetail">
              <th mat-header-cell *matHeaderCellDef colspan="2">{{ 'lbl_loan_detail' | translate}}</th>
              <td mat-cell *matCellDef="let element" colspan="2">
                <div *ngIf="element.amountDisbursed else NoDetail">
                  {{currency}}{{element.amountDisbursed}}<br>
                  {{element.fullName}}-
                  {{element.contactNo}}
                </div>
                <ng-template #NoDetail>
                  --
                </ng-template>
              </td>
            </ng-container>
            <ng-container matColumnDef="view">
              <th mat-header-cell *matHeaderCellDef class="text-right">{{ 'payout_lbl_invoice' | translate}}</th>
              <td mat-cell *matCellDef="let element" class="text-right">
                <a *ngIf="element.url else NoUrl" title="{{ 'connector_tooltip_view_invoice' |translate}}"
                  class="btn btn-sm btn-info filterbtn" [href]="element.url" target="_blanck">
                  <i class="icon-download"></i>
                </a>

                <ng-template #NoUrl>
                  --
                </ng-template>

              </td>
            </ng-container>
            <ng-container matColumnDef="releaseDate">
              <th mat-header-cell *matHeaderCellDef class="text-right">{{ 'payout_lbl_release_date' | translate}}</th>
              <td mat-cell *matCellDef="let element" class="text-right">
                <ng-container *ngIf="element.releaseDate else NoRelease">{{element.releaseDate |
                  date:'medium'}}
                </ng-container>
                <ng-template #NoRelease>--</ng-template>
              </td>
            </ng-container>
            <ng-container matColumnDef="release">
              <th mat-header-cell *matHeaderCellDef class="text-right">{{ 'payout_lbl_release' | translate}}</th>
              <td mat-cell *matCellDef="let element" class="text-right">
                <a *ngIf="!element.isReleased else NoRelease" title="{{ 'connector_tooltip_payout_release' |translate}}"
                  href="javascript:;" class="btn btn-sm btn-info filterbtn"
                  (click)="payoutRelease(element.id,basicmodal)">
                  <i class="fa fa-forward"></i>
                </a>
                <ng-template #NoRelease>--</ng-template>

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

        <form [formGroup]="bankDetailForm">
          <div class="form-group mb-2">
            <label>{{ 'lbl_select_bank' | translate}} <span style="color: red;">*</span></label>
            <ng-select [items]="banks" class="js-example-basic-single p-0" formControlName="bankId"
              placeholder="{{'lbl_select_bank'| translate}}" bindLabel="name" bindValue="id" name="bank">
            </ng-select>
            <mat-error
              *ngIf="(bankDetailForm.get('bankId') && !bankDetailForm.get('bankId')!.valid && bankDetailForm.get('bankId')!.touched)">
              {{ 'message_lbl_bank_is_required' | translate}}
            </mat-error>
          </div>
          <div class="form-group">
            <label for="name">{{ 'lbl_account_holder_name' | translate}} <span style="color: red;">*</span></label>
            <input class="form-control" type="text" id="name" formControlName="accountHolderName" required>
            <mat-error
              *ngIf="(bankDetailForm.get('accountHolderName') && !bankDetailForm.get('accountHolderName')!.valid && bankDetailForm.get('accountHolderName')!.touched)">
              {{ 'payout_message_lbl_account_holder_name_is_required' | translate}}
            </mat-error>
          </div>
          <div class="form-group">
            <label for="name">{{ 'lbl_account_no' | translate}} <span style="color: red;">*</span></label>
            <input class="form-control" type="text" id="name" formControlName="accountNo" required onlyNumbers>
            <mat-error
              *ngIf="(bankDetailForm.get('accountNo') && !bankDetailForm.get('accountNo')!.valid && bankDetailForm.get('accountNo')!.touched)">
              {{ 'message_lbl_bank_account_no_is_required' | translate}}
            </mat-error>
          </div>
          <div class="form-group">
            <label for="name">{{ 'lbl_ifsc_code' | translate}} <span style="color: red;">*</span></label>
            <input class="form-control" type="text" id="name" formControlName="ifscCode" required
              oninput="this.value = this.value.toUpperCase()" maxlength="10" (keypress)="keyPressAlphanumeric($event)"
              oninput="this.value = this.value.toUpperCase()">
            <mat-error
              *ngIf="(bankDetailForm.get('ifscCode') && !bankDetailForm.get('ifscCode')!.valid && bankDetailForm.get('ifscCode')!.touched)">
              {{ 'payout_message_lbl_ifsc_code_is_required' | translate}}
            </mat-error>
          </div>

        </form>
      </ng-container>
    </div>
    <div class="modal-footer">
      <button *ngIf="isInsertPayout" class="btn btn-info filterbtn" (click)="insertPayout()">
        {{ 'button_lbl_save' | translate}}
      </button>
      <button *ngIf="isInsertBankDetail" class="btn btn-info filterbtn" (click)="insertUpdateBankDetail()">
        {{ 'button_lbl_save' | translate}}
      </button>
      <button class="btn btn-secondary filterbtn" (click)="closeDialog()">
        {{ 'button_lbl_cancel' | translate}}
      </button>
    </div>
  </div>
</ng-template>