<vn-crud-model
    vn-id="ticketsModel"
    url="Tickets"
    link="{clientFk: $ctrl.client.id}"
    filter="::$ctrl.ticketFilter"
    limit="10"
    data="tickets"
    order="shipped DESC, id">
</vn-crud-model>
<vn-card class="summary">
    <h5>
        <a ng-if="::$ctrl.summary.id"
            vn-tooltip="Go to the client"
            ui-sref="client.card.summary({id: {{::$ctrl.summary.id}}})"
            name="goToSummary">
            <vn-icon-button icon="launch"></vn-icon-button>
        </a>
        <span>
            {{$ctrl.summary.name}} - {{$ctrl.summary.id}} - {{$ctrl.summary.salesPersonUser.name}}
        </span>
    </h5>
    <vn-horizontal>
        <vn-one>
            <h4 ng-show="$ctrl.isEmployee">
                <a 
                    ui-sref="client.card.basicData({id:$ctrl.client.id})"
                    target="_self">
                    <span translate vn-tooltip="Go to">Basic data</span>
                </a>
            </h4>
            <h4 
                translate
                ng-show="!$ctrl.isEmployee">
                Basic data
            </h4>
            <vn-label-value label="Id" 
                value="{{$ctrl.summary.id}}">
            </vn-label-value>
            <vn-label-value label="Comercial Name" 
                value="{{$ctrl.summary.name}}">
            </vn-label-value>
            <vn-label-value label="Contact" 
                value="{{$ctrl.summary.contact}}">
            </vn-label-value>
            <vn-label-value label="Phone" 
                value="{{$ctrl.summary.phone}}">
            </vn-label-value>
            <vn-label-value label="Mobile" 
                value="{{$ctrl.summary.mobile}}">
            </vn-label-value>
            <vn-label-value label="Email" no-ellipsize
                value="{{$ctrl.listEmails($ctrl.summary.email)}}">
            </vn-label-value>
            <vn-label-value label="Sales person">
                <span 
                    ng-click="workerDescriptor.show($event, $ctrl.summary.salesPersonFk)"
                    class="link">
                    {{$ctrl.summary.salesPersonUser.name}}
                </span>
            </vn-label-value>
            <vn-label-value label="Channel" 
                value="{{$ctrl.summary.contactChannel.name}}">
            </vn-label-value>
        </vn-one>
        <vn-one>
            <h4 ng-show="$ctrl.isEmployee">
                <a 
                    ui-sref="client.card.fiscalData({id:$ctrl.client.id})"
                    target="_self">
                    <span translate vn-tooltip="Go to">Fiscal address</span>
                </a>
            </h4>
            <h4 
                translate
                ng-show="!$ctrl.isEmployee">
                Fiscal address
            </h4>
            <vn-label-value label="Social name" 
                value="{{$ctrl.summary.socialName}}">
            </vn-label-value>
            <vn-label-value label="NIF / CIF" 
                value="{{$ctrl.summary.fi}}">
            </vn-label-value>
            <vn-label-value label="City" 
                value="{{$ctrl.summary.city}}">
            </vn-label-value>
            <vn-label-value label="Postcode" 
                value="{{$ctrl.summary.postcode}}">
            </vn-label-value>
            <vn-label-value label="Province" 
                value="{{$ctrl.summary.province.name}}">
            </vn-label-value>
            <vn-label-value label="Country" 
                value="{{$ctrl.summary.country.country}}">
            </vn-label-value>
            <vn-label-value label="Street" no-ellipsize
                value="{{$ctrl.summary.street}}">
            </vn-label-value>
        </vn-one>
        <vn-one>
            <h4 ng-show="$ctrl.isEmployee">
                <a 
                    ui-sref="client.card.fiscalData({id:$ctrl.client.id})"
                    target="_self">
                    <span translate vn-tooltip="Go to">Fiscal data</span>
                </a>
            </h4>
            <h4 
                translate
                ng-show="!$ctrl.isEmployee">
                Fiscal data
            </h4>
            <vn-vertical>
                <vn-check
                    label="Is equalizated" 
                    ng-model="$ctrl.summary.isEqualizated" 
                    disabled="true">
                </vn-check>
                <vn-check
                    label="Active" 
                    ng-model="$ctrl.summary.isActive"
                    disabled="true">
                </vn-check>
                <vn-check
                    label="Invoice by address" 
                    ng-model="$ctrl.summary.hasToInvoiceByAddress" 
                    disabled="true">
                </vn-check>
                <vn-check
                    label="Verified data" 
                    ng-model="$ctrl.summary.isTaxDataChecked" 
                    disabled="true">
                </vn-check>
                <vn-check
                    label="Has to invoice" 
                    ng-model="$ctrl.summary.hasToInvoice" 
                    disabled="true">
                </vn-check>
                <vn-check
                    label="Notify by email" 
                    ng-model="$ctrl.summary.isToBeMailed" 
                    disabled="true">
                </vn-check>
                <vn-check
                    label="Vies" 
                    ng-model="$ctrl.summary.isVies" 
                    disabled="true">
                </vn-check>
            </vn-vertical>
        </vn-one>
        <vn-one>
            <h4 ng-show="$ctrl.isEmployee">
                <a 
                    ui-sref="client.card.billingData({id:$ctrl.client.id})"
                    target="_self">
                    <span translate vn-tooltip="Go to">Billing data</span>
                </a>
            </h4>
            <h4 
                translate
                ng-show="!$ctrl.isEmployee">
                Billing data
            </h4>
            <vn-label-value label="Pay method" 
                value="{{$ctrl.summary.payMethod.name}}">
            </vn-label-value>
            <vn-label-value label="IBAN" 
                value="{{$ctrl.summary.iban}}">
            </vn-label-value>
            <vn-label-value label="Due day" 
                value="{{$ctrl.summary.dueDay}}">
            </vn-label-value>
            <vn-vertical>
                <vn-check
                    label="Received LCR" 
                    ng-model="$ctrl.summary.hasLcr" 
                    disabled="true">
                </vn-check>
                <vn-check
                    label="Received core VNL" 
                    ng-model="$ctrl.summary.hasCoreVnl" 
                    disabled="true">
                </vn-check>
                <vn-check
                    label="Received B2B VNL" 
                    ng-model="$ctrl.summary.hasSepaVnl" 
                    disabled="true">
                </vn-check>
            </vn-vertical>
        </vn-one>
        <vn-one>
            <h4 ng-show="$ctrl.isEmployee">
                <a 
                    ui-sref="client.card.address.index({id:$ctrl.client.id})"
                    target="_self">
                    <span translate vn-tooltip="Go to">Address</span>
                </a>
            </h4>
            <h4
                translate
                ng-show="!$ctrl.isEmployee">
                Address
            </h4>
            <vn-label-value label="Name" 
                value="{{$ctrl.summary.defaultAddress.nickname}}">
            </vn-label-value>
            <vn-label-value label="City" 
                value="{{$ctrl.summary.defaultAddress.city}}">
            </vn-label-value>
            <vn-label-value label="Street" no-ellipsize
                value="{{$ctrl.summary.defaultAddress.street}}">
            </vn-label-value>
        </vn-one>
        <vn-one>
            <h4 ng-show="$ctrl.isEmployee">
                <a 
                    ui-sref="client.card.webAccess({id:$ctrl.client.id})"
                    target="_self">
                    <span translate vn-tooltip="Go to">Web access</span>
                </a>
            </h4>
            <h4 
                translate
                ng-show="!$ctrl.isEmployee">Web access
            </h4>
            <vn-label-value label="User" 
                value="{{$ctrl.summary.account.name}}">
            </vn-label-value>
            <vn-vertical>
                <vn-check
                    label="Enable web access"
                    ng-model="$ctrl.summary.account.active"
                    disabled="true">
                </vn-check>
            </vn-vertical>
        </vn-one>
        <vn-one>
            <h4 translate>Business data</h4>
            <vn-label-value label="Total greuge" 
                value="{{$ctrl.summary.totalGreuge | currency: 'EUR':2}}">
            </vn-label-value>
            <vn-label-value label="Mana" 
                value="{{$ctrl.summary.mana.mana | currency: 'EUR':2}}">
            </vn-label-value>
            <vn-label-value label="Rate" 
                value="{{$ctrl.claimRate($ctrl.summary.claimsRatio.priceIncreasing / 100) | percentage}}">
            </vn-label-value>
            <vn-label-value label="Average invoiced" 
                value="{{$ctrl.summary.averageInvoiced.invoiced | currency: 'EUR':2}}">
            </vn-label-value>
            <vn-label-value label="Claims" 
                value="{{$ctrl.claimingRate($ctrl.summary.claimsRatio.claimingRate) | percentage}}">
            </vn-label-value>
        </vn-one>
        <vn-one>
            <h4 translate>Financial information</h4>
            <vn-label-value label="Risk" 
                value="{{$ctrl.summary.debt.debt | currency: 'EUR':2}}"
                ng-class="{alert: $ctrl.summary.debt.debt > $ctrl.summary.credit}"
                info="Invoices minus payments  plus orders not yet invoiced">  
            </vn-label-value>
            <vn-label-value label="Credit" 
                value="{{$ctrl.summary.credit | currency: 'EUR':2 }} "
                ng-class="{alert: $ctrl.summary.credit > $ctrl.summary.creditInsurance ||
                                ($ctrl.summary.credit  && $ctrl.summary.creditInsurance == null)}"
                info="Verdnatura's maximum risk">
            </vn-label-value>
            <vn-label-value label="Secured credit" 
                value="{{$ctrl.summary.creditInsurance | currency: 'EUR':2}} ({{$ctrl.summary.classifications[0].insurances[0].grade}})"
                info="Solunion's maximum risk">
            </vn-label-value>
            <vn-label-value label="Balance" 
                value="{{$ctrl.summary.sumRisk | currency: 'EUR':2}}"
                info="Invoices minus payments">
            </vn-label-value>
            <vn-label-value label="Balance due" 
                value="{{($ctrl.summary.defaulters[0].amount >= 0 ? $ctrl.summary.defaulters[0].amount : '-') | currency: 'EUR':2}}"
                ng-class="{alert: $ctrl.summary.defaulters[0].amount}"
                info="Deviated invoices minus payments">
            </vn-label-value>
            <vn-label-value label="Recovery since" 
                ng-if="$ctrl.summary.recovery.started"
                value="{{$ctrl.summary.recovery.started | date:'dd/MM/yyyy'}}">
            </vn-label-value>
        </vn-one>
    </vn-horizontal>
    <vn-horizontal>
        <vn-one>
            <h4 translate>Latest tickets</h4>
            <vn-table model="ticketsModel">
                <vn-thead>
                    <vn-tr>
                        <vn-th field="id" number>Id</vn-th>
                        <vn-th field="nickname" expand>Nickname</vn-th>
                        <vn-th field="agencyModeFk" expand>Agency</vn-th>
                        <vn-th field="routeFk" expand>Route</vn-th>
                        <vn-th field="packages" shrink>Packages</vn-th>
                        <vn-th field="shipped" shrink-date>Date</vn-th>
                        <vn-th field="stateFk">State</vn-th>
                        <vn-th field="totalWithVat" shrink>Total</vn-th>
                        <vn-th></vn-th>
                    </vn-tr>
                </vn-thead>
                <vn-tbody>
                    <a ng-repeat="ticket in ticketsModel.data"
                        class="clickable vn-tr search-result"
                        ui-sref="ticket.card.summary({id: {{::ticket.id}}})">
                        <vn-td number>{{::ticket.id}}</vn-td>
                        <vn-td expand>
                            <span
                                title="{{::ticket.nickname}}"
                                vn-click-stop="clientDescriptor.show($event, ticket.clientFk)"
                                class="link">
                                {{::ticket.nickname}}
                            </span>
                        </vn-td>
                        <vn-td expand>
                            {{::ticket.agencyMode.name}}
                        </vn-td>
                        <vn-td shrink>
                            <span
                                title="{{::ticket.route}}"
                                vn-click-stop="routeDescriptor.show($event, ticket.routeFk)"
                                class="link">
                                {{::ticket.routeFk}}
                            </span>
                        </vn-td>
                        <vn-td shrink>
                            {{::ticket.packages}}
                        </vn-td>
                        <vn-td shrink-date>
                            <span class="chip {{::$ctrl.chipColor(ticket.shipped)}}">
                                {{::ticket.shipped | date: 'dd/MM/yyyy'}}
                            </span>
                        </vn-td>
                        <vn-td>
                            <span
                                ng-show="::ticket.refFk"
                                title="{{::ticket.refFk}}"
                                vn-click-stop="invoiceOutDescriptor.show($event, ticket.invoiceOut.id)"
                                class="link">
                                {{::ticket.refFk}}
                            </span>
                            <span 
                                ng-show="::!ticket.refFk"
                                class="chip {{::$ctrl.stateColor(ticket)}}">
                                {{::ticket.ticketState.state.name}}
                            </span>
                        </vn-td>
                        <vn-td shrink>
                            <span class="chip {{::$ctrl.totalPriceColor(ticket)}}">
                                {{::(ticket.totalWithVat ? ticket.totalWithVat : 0) | currency: 'EUR': 2}}
                            </span>
                        </vn-td>
                        <vn-td actions>
                            <vn-icon-button
                                vn-anchor="::{
                                    state: 'ticket.card.sale', 
                                    params: {id: ticket.id}, 
                                    target: '_blank'
                                }"
                                vn-tooltip="Go to lines"
                                icon="icon-lines">
                            </vn-icon-button>
                            <vn-icon-button
                                vn-click-stop="$ctrl.preview(ticket)"
                                vn-tooltip="Preview"
                                icon="preview">
                            </vn-icon-button>
                        </vn-td>
                    </a>
                </vn-tbody>
            </vn-table>
            <vn-pagination
                model="ticketsModel"
                class="vn-pt-xs"
                scroll-selector="vn-table[model='ticketsModel']"
                scroll-offset="100">
            </vn-pagination>
        </vn-one>
    </vn-horizontal>
</vn-card>
<vn-client-descriptor-popover
    vn-id="clientDescriptor">
</vn-client-descriptor-popover>
<vn-route-descriptor-popover
    vn-id="routeDescriptor">
</vn-route-descriptor-popover>
<vn-worker-descriptor-popover 
    vn-id="workerDescriptor">
</vn-worker-descriptor-popover>
<vn-invoice-out-descriptor-popover 
    vn-id="invoiceOutDescriptor">
</vn-invoice-out-descriptor-popover>
<vn-popup vn-id="summary">
    <vn-ticket-summary
        ticket="$ctrl.selectedTicket"
        model="model">
    </vn-ticket-summary>
</vn-popup>