<ion-view>
  <ion-nav-bar class="bar-royal">
    <ion-nav-buttons side="primary">
      <button class="button button-clear" ui-sref="tabs.home">
        Close
      </button>
    </ion-nav-buttons>
    <ion-nav-title>BitPay Card</ion-nav-title>
    <ion-nav-buttons side="secondary">
      <button class="button button-clear" ui-sref="bitpayCard.preferences">
        <i class="icon ion-gear-b"></i>
      </button>
    </ion-nav-buttons>
  </ion-nav-bar>

  <ion-content ng-controller="bitpayCardController as bitpayCard" ng-init="bitpayCard.init()">

    <div class="box-notification warning" ng-show="network == 'testnet'">
      Sandbox version. Only for testing purpose
    </div>

    <div class="m20t text-center size-12 text-gray" ng-show="!bitpayCard.bitpayCardAuthenticated && bitpayCard.loadingSession">
      Loading...
    </div>

    <div ng-show="!bitpayCard.bitpayCardAuthenticated && !bitpayCard.loadingSession">
        <div class="text-center m20t">
          <img src="img/bitpay-card-visa.svg" width="200">
        </div>
        <h4 class="text-center">
          <span ng-show="!bitpayCard.bitpayCardTwoFactorPending">Login to your account</span>
          <span ng-show="bitpayCard.bitpayCardTwoFactorPending">2-Step Verification</span>
        </h4>

        <form
             ng-show="!bitpayCard.bitpayCardTwoFactorPending"
             name="authenticateForm"
             ng-submit="bitpayCard.authenticate()"
             novalidate>

          <div class="card list">
            <label class="item item-input item-stacked-label">
              <span class="input-label">Email</span>
              <input name="email"
                     type="email"
                     ng-model="email"
                     ng-disabled="bitpayCard.authenticating"
                     required>
            </label>

            <label class="item item-input item-stacked-label">
              <span class="input-label">Password</span>
              <input name="password"
                     type="password"
                     ng-model="password"
                     ng-disabled="bitpayCard.authenticating"
                     required>
            </label>
          </div>

          <input class="button button-block button-positive"
                 type="submit"
                 ng-disabled="!authenticateForm.$valid || bitpayCard.authenticating"
                 value="Login">
        </form>

        <p ng-show="bitpayCard.bitpayCardTwoFactorPending" class="size-12 text-center text-gray">
        Enter the verification code generated by the authenticator app on your phone.
        </p>

        <form
             ng-show="bitpayCard.bitpayCardTwoFactorPending"
             name="authenticate2FAForm"
             ng-submit="bitpayCard.authenticate2FA()"
             novalidate>

          <div class="list">
            <label class="item item-input item-stacked-label">
              <span class="input-label">Verification Code</span>
              <input name="twoFactorCode"
                     type="text"
                     ng-model="twoFactorCode"
                     ng-disabled="bitpayCard.authenticating"
                     required>
            </label>
          </div>

          <input class="button button-block button-positive"
                 type="submit"
                 ng-disabled="!authenticate2FAForm.$valid || bitpayCard.authenticating"
                 value="Login">
        </form>
    </div>

    <div ng-show="bitpayCard.bitpayCardAuthenticated && !bitpayCard.visaCardActivated && !addFunds">
      <div id="bitpayCard" class="oh pr">
        <div class="amount">
          <div ng-show="!loadingHistory && bitpayCard.bitpayCardCurrentBalance" ng-click="bitpayCard.update()">
            <strong class="size-36">${{bitpayCard.bitpayCardCurrentBalance}}</strong>
            <div class="size-12">Available balance</div>
          </div>
          <div ng-show="loadingHistory">
            <strong class="size-36">...</strong>
          </div>
        </div>
        <div class="camera-icon" ng-show="bitpayCard.bitpayCardCurrentBalance">
          <a ng-click="addFunds = true">
            <i class="icon ion-plus size-21"></i>
          </a>
        </div>
      </div>

      <select class="m10" ng-model="dateRange" ng-change="bitpayCard.update(dateRange)">
        <option value="last30Days">Recent Activity</option>
        <option value="lastMonth">Last Month</option>
        <option value="all">All Activity</option>
      </select>

      <div
        class="oh pr m20t text-gray size-12 text-center"
        ng-show="!bitpayCard.bitpayCardTransactionHistory[0] &&
        !bitpayCard.bitpayCardInvoiceHistory[0] && !loadingHistory">
        No transactions yet
      </div>

      <div ng-show="loadingHistory" class="oh pr m20t text-gray text-center">
        <i class="icon ion-android-sync"></i>
      </div>

      <div class="card list" ng-show="!loadingHistory">
        <div
          ng-repeat="tx in bitpayCard.bitpayCardTransactionHistory | orderBy: ['pending','-timestamp']"
          class="item row"
          ng-init="bitpayCard.getMerchantInfo(tx)">
          <div class="col" ng-init="icon = bitpayCard.getIconName(tx)">
            <img class="m5t" ng-src="img/mcc-icons/{{icon}}.svg" width="22">
          </div>

          <div class="col">
            <div class="size-12 text-bold">
              {{tx.merchant.name}}
            </div>
            <div class="size-12">
              {{tx.merchant.city}}, {{tx.merchant.state}}
            </div>
          </div>
          <div
            ng-init="desc = bitpayCard.processDescription(tx)"
            class="col">
            {{desc}}
          </div>
          <div class="col">
            <img ng-show="!tx.pending" ng-src="img/check.svg" width="14">
            <img ng-show="tx.pending" ng-src="img/sync.svg" width="14">
          </div>
          <div class="col text-right size-12 text-gray">
            <div class="size-14"
                 ng-class="{
                 'text-success': tx.amount.indexOf('-') == -1 && !tx.pending,
                 'text-gray': tx.amount.indexOf('-') == -1 && tx.pending}">
              {{tx.amount | currency:'$':2 }}
            </div>
            <time>{{tx.timestamp | amTimeAgo}}</time>
          </div>
        </div>
      </div>
    </div>

    <div ng-show="bitpayCard.bitpayCardAuthenticated && !bitpayCard.visaCardActivated && addFunds">
        <form
           name="createInvoiceForm"
           ng-submit="bitpayCard.sendFunds()"
           novalidate>

          <div class="card list">
            <label class="item item-input item-stacked-label">
              <span class="input-label">Amount</span>
              <input
                    type="number"
                    id="fiat"
                    name="fiat"
                    ng-attr-placeholder="{{'Amount in USD'}}"
                    min="0.01"
                    max="2000"
                    ng-model="fiat"
                    autocomplete="off"
                    required>
              <a class="postfix">USD</a>
            </label>

            <wallets ng-if="wallets[0]" wallets="wallets"></wallets>

          </div>

          <p class="size-12 text-warning" ng-show="bitpayCard.isMultisigWallet">
            You selected a multisignature wallet. Please note that the transaction will only appear on your card's
            Activity when the payment is fully signed.
          </p>

          <div class="row">
            <div class="col">
              <button class="button button-block button-light"
                      type="button"
                      ng-click="addFunds = false; fiat = null">
                Cancel
              </button>
            </div>
            <div class="col">
              <button class="button button-block button-positive"
                      ng-disabled="!fiat"
                      type="submit">
                Send
              </button>
            </div>
          </div>
        </form>
    </div>
  </ion-content>
</ion-view>
