<template>
    <div>
        <div class="panel px-0 pb-1.5 border-[#e0e6ed] dark:border-[#1b2e4b]">
            <div class="datatable invoice-table">
                <div class="mb-4.5 px-5 flex md:items-center md:flex-row flex-col gap-5">
                    <div class="flex items-center gap-2">
                        <button type="button" class="btn btn-danger gap-2" @click="deleteRow()">
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none"
                                xmlns="http://www.w3.org/2000/svg" class="w-5 h-5">
                                <path d="M20.5001 6H3.5" stroke="currentColor" stroke-width="1.5"
                                    stroke-linecap="round"></path>
                                <path
                                    d="M18.8334 8.5L18.3735 15.3991C18.1965 18.054 18.108 19.3815 17.243 20.1907C16.378 21 15.0476 21 12.3868 21H11.6134C8.9526 21 7.6222 21 6.75719 20.1907C5.89218 19.3815 5.80368 18.054 5.62669 15.3991L5.16675 8.5"
                                    stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></path>
                                <path opacity="0.5" d="M9.5 11L10 16" stroke="currentColor" stroke-width="1.5"
                                    stroke-linecap="round"></path>
                                <path opacity="0.5" d="M14.5 11L14 16" stroke="currentColor" stroke-width="1.5"
                                    stroke-linecap="round"></path>
                                <path opacity="0.5"
                                    d="M6.5 6C6.55588 6 6.58382 6 6.60915 5.99936C7.43259 5.97849 8.15902 5.45491 8.43922 4.68032C8.44784 4.65649 8.45667 4.62999 8.47434 4.57697L8.57143 4.28571C8.65431 4.03708 8.69575 3.91276 8.75071 3.8072C8.97001 3.38607 9.37574 3.09364 9.84461 3.01877C9.96213 3 10.0932 3 10.3553 3H13.6447C13.9068 3 14.0379 3 14.1554 3.01877C14.6243 3.09364 15.03 3.38607 15.2493 3.8072C15.3043 3.91276 15.3457 4.03708 15.4286 4.28571L15.5257 4.57697C15.5433 4.62992 15.5522 4.65651 15.5608 4.68032C15.841 5.45491 16.5674 5.97849 17.3909 5.99936C17.4162 6 17.4441 6 17.5 6"
                                    stroke="currentColor" stroke-width="1.5"></path>
                            </svg>
                            Delete
                        </button>
                        <link to="/apps/invoice/add" class="btn btn-primary gap-2">
                        <svg xmlns="http://www.w3.org/2000/svg" width="24px" height="24px" viewBox="0 0 24 24"
                            fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"
                            stroke-linejoin="round" class="w-5 h-5">
                            <line x1="12" y1="5" x2="12" y2="19"></line>
                            <line x1="5" y1="12" x2="19" y2="12"></line>
                        </svg>
                        Add New
                        </link>
                    </div>
                    <div class="ltr:ml-auto rtl:mr-auto">
                        <input v-model="search" type="text" class="form-input" placeholder="Search..." />
                    </div>
                </div>

                <vue3-datatable ref="datatable" :rows="items" :columns="cols" :totalRows="items?.length"
                    :hasCheckbox="true" :sortable="true" :search="search" skin="whitespace-nowrap bh-table-hover"
                    firstArrow='<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5 rtl:rotate-180"> <path d="M13 19L7 12L13 5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> <path opacity="0.5" d="M16.9998 19L10.9998 12L16.9998 5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> </svg>'
                    lastArrow='<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5 rtl:rotate-180"> <path d="M11 19L17 12L11 5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> <path opacity="0.5" d="M6.99976 19L12.9998 12L6.99976 5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> </svg> '
                    previousArrow='<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5 rtl:rotate-180"> <path d="M15 5L9 12L15 19" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> </svg>'
                    nextArrow='<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5 rtl:rotate-180"> <path d="M9 5L15 12L9 19" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> </svg>'>
                    <template #invoice="data">
                        <link to="/apps/invoice/preview"
                            class="text-primary underline font-semibold hover:no-underline">#{{ data.value.invoice }}
                        </link>
                    </template>
                    <template #name="data">
                        <div class="flex items-center font-semibold">
                            <div class="p-0.5 bg-white-dark/30 rounded-full w-max ltr:mr-2 rtl:ml-2">
                                <img class="h-8 w-8 rounded-full object-cover"
                                    :src="`/assets/images/profile-${data.value.id}.jpeg`" />
                            </div>
                            {{ data.value.name }}
                        </div>
                    </template>
                    <template #amount="data">
                        <div class="font-semibold ltr:text-right rtl:text-left">${{ data.value.amount }}</div>
                    </template>
                    <template #status="data">
                        <span class="badge"
                            :class="[data.value.status.toLowerCase() === 'paid' ? 'badge-outline-success' : 'badge-outline-danger']">{{
                                data.value.status
                            }}</span>
                    </template>
                    <template #actions="data">
                        <div class="flex gap-4 items-center justify-center">
                            <link to="/apps/invoice/edit" class="hover:text-info">
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none"
                                xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5">
                                <path opacity="0.5"
                                    d="M22 10.5V12C22 16.714 22 19.0711 20.5355 20.5355C19.0711 22 16.714 22 12 22C7.28595 22 4.92893 22 3.46447 20.5355C2 19.0711 2 16.714 2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2H13.5"
                                    stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></path>
                                <path
                                    d="M17.3009 2.80624L16.652 3.45506L10.6872 9.41993C10.2832 9.82394 10.0812 10.0259 9.90743 10.2487C9.70249 10.5114 9.52679 10.7957 9.38344 11.0965C9.26191 11.3515 9.17157 11.6225 8.99089 12.1646L8.41242 13.9L8.03811 15.0229C7.9492 15.2897 8.01862 15.5837 8.21744 15.7826C8.41626 15.9814 8.71035 16.0508 8.97709 15.9619L10.1 15.5876L11.8354 15.0091C12.3775 14.8284 12.6485 14.7381 12.9035 14.6166C13.2043 14.4732 13.4886 14.2975 13.7513 14.0926C13.9741 13.9188 14.1761 13.7168 14.5801 13.3128L20.5449 7.34795L21.1938 6.69914C22.2687 5.62415 22.2687 3.88124 21.1938 2.80624C20.1188 1.73125 18.3759 1.73125 17.3009 2.80624Z"
                                    stroke="currentColor" stroke-width="1.5"></path>
                                <path opacity="0.5"
                                    d="M16.6522 3.45508C16.6522 3.45508 16.7333 4.83381 17.9499 6.05034C19.1664 7.26687 20.5451 7.34797 20.5451 7.34797M10.1002 15.5876L8.4126 13.9"
                                    stroke="currentColor" stroke-width="1.5"></path>
                            </svg>
                            </link>
                            <link to="/apps/invoice/preview" class="hover:text-primary">
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none"
                                xmlns="http://www.w3.org/2000/svg" class="w-5 h-5">
                                <path opacity="0.5"
                                    d="M3.27489 15.2957C2.42496 14.1915 2 13.6394 2 12C2 10.3606 2.42496 9.80853 3.27489 8.70433C4.97196 6.49956 7.81811 4 12 4C16.1819 4 19.028 6.49956 20.7251 8.70433C21.575 9.80853 22 10.3606 22 12C22 13.6394 21.575 14.1915 20.7251 15.2957C19.028 17.5004 16.1819 20 12 20C7.81811 20 4.97196 17.5004 3.27489 15.2957Z"
                                    stroke="currentColor" stroke-width="1.5"></path>
                                <path
                                    d="M15 12C15 13.6569 13.6569 15 12 15C10.3431 15 9 13.6569 9 12C9 10.3431 10.3431 9 12 9C13.6569 9 15 10.3431 15 12Z"
                                    stroke="currentColor" stroke-width="1.5"></path>
                            </svg>
                            </link>
                            <button type="button" class="hover:text-danger" @click="deleteRow(`${data.value.id}`)">
                                <svg width="24" height="24" viewBox="0 0 24 24" fill="none"
                                    xmlns="http://www.w3.org/2000/svg" class="w-5 h-5">
                                    <path d="M20.5001 6H3.5" stroke="currentColor" stroke-width="1.5"
                                        stroke-linecap="round"></path>
                                    <path
                                        d="M18.8334 8.5L18.3735 15.3991C18.1965 18.054 18.108 19.3815 17.243 20.1907C16.378 21 15.0476 21 12.3868 21H11.6134C8.9526 21 7.6222 21 6.75719 20.1907C5.89218 19.3815 5.80368 18.054 5.62669 15.3991L5.16675 8.5"
                                        stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></path>
                                    <path opacity="0.5" d="M9.5 11L10 16" stroke="currentColor" stroke-width="1.5"
                                        stroke-linecap="round"></path>
                                    <path opacity="0.5" d="M14.5 11L14 16" stroke="currentColor" stroke-width="1.5"
                                        stroke-linecap="round"></path>
                                    <path opacity="0.5"
                                        d="M6.5 6C6.55588 6 6.58382 6 6.60915 5.99936C7.43259 5.97849 8.15902 5.45491 8.43922 4.68032C8.44784 4.65649 8.45667 4.62999 8.47434 4.57697L8.57143 4.28571C8.65431 4.03708 8.69575 3.91276 8.75071 3.8072C8.97001 3.38607 9.37574 3.09364 9.84461 3.01877C9.96213 3 10.0932 3 10.3553 3H13.6447C13.9068 3 14.0379 3 14.1554 3.01877C14.6243 3.09364 15.03 3.38607 15.2493 3.8072C15.3043 3.91276 15.3457 4.03708 15.4286 4.28571L15.5257 4.57697C15.5433 4.62992 15.5522 4.65651 15.5608 4.68032C15.841 5.45491 16.5674 5.97849 17.3909 5.99936C17.4162 6 17.4441 6 17.5 6"
                                        stroke="currentColor" stroke-width="1.5"></path>
                                </svg>
                            </button>
                        </div>
                    </template>
                </vue3-datatable>
            </div>
        </div>
    </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import Vue3Datatable from '@bhplugin/vue3-datatable';
import { useMeta } from '@/composables/use-meta';
useMeta({ title: 'Invoice List' });

const datatable: any = ref(null);
const search = ref('');
const cols = ref([
    { field: 'invoice', title: 'Invoice' },
    { field: 'name', title: 'Name' },
    { field: 'email', title: 'Email' },
    { field: 'date', title: 'Date' },
    { field: 'amount', title: 'Amount', headerClass: 'justify-end' },
    { field: 'status', title: 'Status' },
    { field: 'actions', title: 'Actions', sort: false, headerClass: 'justify-center' },
]);
const items = ref([
    {
        id: 1,
        invoice: '081451',
        name: 'Laurie Fox',
        email: 'lauriefox@company.com',
        date: '15 Dec 2020',
        amount: '2275.45',
        status: 'Paid',
    },
    {
        id: 2,
        invoice: '081452',
        name: 'Alexander Gray',
        email: 'alexGray3188@gmail.com',
        date: '20 Dec 2020',
        amount: '1044.00',
        status: 'Paid',
    },
    {
        id: 3,
        invoice: '081681',
        name: 'James Taylor',
        email: 'jamestaylor468@gmail.com',
        date: '27 Dec 2020',
        amount: '20.00',
        status: 'Pending',
    },
    {
        id: 4,
        invoice: '082693',
        name: 'Grace Roberts',
        email: 'graceRoberts@company.com',
        date: '31 Dec 2020',
        amount: '344.00',
        status: 'Paid',
    },
    {
        id: 5,
        invoice: '084743',
        name: 'Donna Rogers',
        email: 'donnaRogers@hotmail.com',
        date: '03 Jan 2021',
        amount: '405.15',
        status: 'Paid',
    },
    {
        id: 6,
        invoice: '086643',
        name: 'Amy Diaz',
        email: 'amy968@gmail.com',
        date: '14 Jan 2020',
        amount: '100.00',
        status: 'Paid',
    },
    {
        id: 7,
        invoice: '086773',
        name: 'Nia Hillyer',
        email: 'niahillyer666@comapny.com',
        date: '20 Jan 2021',
        amount: '59.21',
        status: 'Pending',
    },
    {
        id: 8,
        invoice: '087916',
        name: 'Mary McDonald',
        email: 'maryDonald007@gamil.com',
        date: '25 Jan 2021',
        amount: '79.00',
        status: 'Pending',
    },
    {
        id: 9,
        invoice: '089472',
        name: 'Andy King',
        email: 'kingandy07@company.com',
        date: '28 Jan 2021',
        amount: '149.00',
        status: 'Paid',
    },
    {
        id: 10,
        invoice: '091768',
        name: 'Vincent Carpenter',
        email: 'vincentcarpenter@gmail.com',
        date: '30 Jan 2021',
        amount: '400',
        status: 'Paid',
    },
    {
        id: 11,
        invoice: '095841',
        name: 'Kelly Young',
        email: 'youngkelly@hotmail.com',
        date: '06 Feb 2021',
        amount: '49.00',
        status: 'Pending',
    },
    {
        id: 12,
        invoice: '098424',
        name: 'Alma Clarke',
        email: 'alma.clarke@gmail.com',
        date: '10 Feb 2021',
        amount: '234.40',
        status: 'Paid',
    },
]);
const searchText = ref('');
const columns = ref(['id', 'invoice', 'name', 'email', 'date', 'amount', 'status', 'actions']);
const tableOption = ref({
    headings: {
        id: (h: any, row: any, index: number) => {
            return '#';
        },
    },
    perPage: 10,
    perPageValues: [10, 20, 30, 50, 100],
    skin: 'table-hover',
    columnsClasses: { actions: 'actions !text-center w-1' },
    pagination: { show: true, nav: 'scroll', chunk: 10 },
    texts: {
        count: 'Showing {from} to {to} of {count} entries',
        filter: '',
        filterPlaceholder: 'Search...',
        limit: '',
    },
    resizableColumns: false,
    sortable: ['invoice', 'name', 'email', 'date', 'amount', 'status'],
    sortIcon: {
        base: 'sort-icon-none',
        up: 'sort-icon-asc',
        down: 'sort-icon-desc',
    },
});

const deleteRow = (item: any = null) => {
    if (confirm('Are you sure want to delete selected row ?')) {
        if (item) {
            items.value = items.value.filter((d) => d.id != item);
            datatable.value.clearSelectedRows();
        } else {
            let selectedRows = datatable.value.getSelectedRows();
            const ids = selectedRows.map((d) => {
                return d.id;
            });
            items.value = items.value.filter((d) => !ids.includes(d.id as never));
            datatable.value.clearSelectedRows();
        }
    }
};
</script>
