<div class="modal-header">
  <h3 class="text-center red" translate>{{ 'app.admin.invoices.close_accounting_period' }}</h3>
</div>
<div class="modal-body">
  <form name="closePeriodForm" novalidate="novalidate" class="row">
    <div class="form-group col-md-6" ng-class="{'has-error': closePeriodForm.start_at.$dirty && closePeriodForm.start_at.$invalid }">
      <label translate>{{ 'app.admin.invoices.close_from_date' }}</label>
      <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
        <input type="text"
               class="form-control"
               name="start_at"
               ng-model="period.start_at"
               uib-datepicker-popup="{{datePicker.format}}"
               datepicker-options="datePicker.options"
               is-open="datePicker.startOpened"
               min-date="datePicker.minDate"
               max-date="datePicker.minDate"
               init-date="period.start_at"
               placeholder="{{datePicker.format}}"
               readonly
               required/>
      </div>
      <span class="help-block" ng-show="closePeriodForm.start_at.$dirty && closePeriodForm.start_at.$error.required" translate>{{ 'app.admin.invoices.start_date_is_required' }}</span>
      <span class="help-block error" ng-show="errors.start_at">{{ errors.start_at[0] }}</span>
    </div>
    <div class="form-group col-md-6" ng-class="{'has-error': closePeriodForm.end_at.$dirty && closePeriodForm.end_at.$invalid }">
      <label translate>{{ 'app.admin.invoices.close_until_date' }}</label>
      <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
        <input type="text"
               class="form-control as-writable"
               name="end_at"
               ng-model="period.end_at"
               uib-datepicker-popup="{{datePicker.format}}"
               datepicker-options="datePicker.options"
               is-open="datePicker.endOpened"
               min-date="datePicker.minDate"
               max-date="datePicker.maxDate"
               init-date="period.end_at"
               placeholder="{{datePicker.format}}"
               ng-click="toggleDatePicker($event)"
               required
               readonly/>
      </div>
      <span class="help-block" ng-show="closePeriodForm.end_at.$dirty && closePeriodForm.end_at.$error.required" translate>{{ 'app.admin.invoices.end_date_is_required' }}</span>
      <span class="help-block error" ng-show="errors.end_at">{{ errors.end_at[0] }}</span>
    </div>
  </form>
  <div ng-repeat="(key, value) in errors" ng-if="invoiceErrorRE.test(key)" class="row col-md-12">
    <span class="help-block error">{{ $parent.invoiceErrorRE.exec(key)[1] }} : {{ value[0] }}</span>
  </div>
  <div>
    <h4 translate>{{ 'app.admin.invoices.previous_closings' }}</h4>
    <table class="table closings-table" ng-show="accountingPeriods.length > 0">
      <thead>
        <tr>
          <th translate>{{ 'app.admin.invoices.start_date' }}</th>
          <th translate>{{ 'app.admin.invoices.end_date' }}</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="period in accountingPeriods">
          <td>{{period.start_at | amDateFormat:'L'}}</td>
          <td>{{period.end_at | amDateFormat:'L'}}</td>
          <td class="actions">
            <span class="show-more" uib-popover-template="'/admin/invoices/_period.html'"><i class="fa fa-info-circle"></i></span>
            <span class="download-archive" ng-click="downloadArchive(period)" ng-show="period.archive_ready"><i class="fa fa-archive"></i></span>
            <span class="no-pointer" ng-hide="period.archive_ready"><i class="fa fa-spinner fa-pulse"></i></span>
          </td>
        </tr>
      </tbody>
    </table>
    <div ng-show="accountingPeriods.length === 0" translate>{{ 'app.admin.invoices.no_periods'}}</div>
  </div>
</div>
<div class="modal-footer">
  <button class="btn btn-warning" ng-click="ok()" ng-disabled="closePeriodForm.$invalid || pendingCreation" translate>{{ 'app.shared.buttons.confirm' }}</button>
  <button class="btn btn-default" ng-click="cancel()" ng-disabled="pendingCreation" translate>{{ 'app.shared.buttons.cancel' }}</button>
</div>
