<template>
    <div>
        <ul class="flex space-x-2 rtl:space-x-reverse">
            <li>
                <a href="javascript:;" class="hover:underline" @click="goHome">首页</a>
            </li>
            <li class="before:content-['/'] ltr:before:mr-2 rtl:before:ml-2 text-primary">
                <span>调拨列表</span>
            </li>
        </ul>

        <div class="panel pb-0 mt-6">

            <div class="flex md:items-center md:flex-row flex-col mb-5 gap-5">

                <div class="flex items-center gap-5 ltr:ml-auto rtl:mr-auto">
                    <div class="dropdown">
                        <Popper :placement="store.rtlClass === 'rtl' ? 'bottom-end' : 'bottom-start'" offsetDistance="0"
                            class="align-middle">
                            <button type="button"
                                class="whitespace-nowrap flex items-center border font-semibold border-[#e0e6ed] dark:border-[#253b5c] rounded-md px-4 py-2 text-sm dark:bg-[#1b2e4b] dark:text-white-dark">
                                <span class="ltr:mr-1 rtl:ml-1">选择</span>
                                <icon-caret-down class="w-5 h-5" />
                            </button>
                            <template #content>
                                <ul class="whitespace-nowrap">
                                    <template v-for="(col, i) in cols" :key="i">
                                        <li>
                                            <div class="flex items-center px-4 py-1">
                                                <label class="cursor-pointer mb-0">
                                                    <input type="checkbox" class="form-checkbox" :id="`chk-${i}`"
                                                        :value="col.field" @change="col.hide = !$event.target.checked"
                                                        :checked="!col.hide" />
                                                    <span :for="`chk-${i}`" class="ltr:ml-2 rtl:mr-2">{{ col.title
                                                        }}</span>
                                                </label>
                                            </div>
                                        </li>
                                    </template>
                                </ul>
                            </template>
                        </Popper>
                    </div>
                    <div>
                        <input v-model="search" type="text" class="form-input" placeholder="Search..." />
                    </div>
                </div>
            </div>

            <div class="datatable">
                <vue3-datatable :rows="rows" :columns="cols" :totalRows="rows?.length" :sortable="true" :search="search" style="min-height: 500px;"
                    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 #transfer_time="data">
                        {{ formatDate(data.value.transfer_time) }}
                    </template> -->
                    <template #in_warehouse_id="data">
                        {{ warehouseName(data.value.in_warehouse_id) }}
                    </template>
                    <template #out_warehouse_id="data">
                        {{ warehouseName(data.value.out_warehouse_id) }}
                    </template>
                    <template #send_type="data">
                        {{ deliveryName(data.value.send_type) }}
                    </template>
                    <!-- <template #order_vo.pay_type="data">
                        {{ collectingAccountName(data.value.order_vo.pay_type) }}
                    </template>-->

                    <template #status="data">
                        {{ typeName(data.value.status) }}
                    </template>
                    <!-- 定义操作栏的模板 -->
                    <template #actions="data">
                        <button @click="deliveryOrder(data)" class="btn btn-primary">查看</button>
                    </template>
                    <template #isActive="data">
                        <!--  <span class="capitalize" :class="data.value.isActive ? 'text-success' : 'text-danger'">{{
                            data._rawValue }}</span> -->
                    </template>
                    <template #pagination>
                        <!-- 这里什么也不放，或者放一个空的div -->
                    </template>
                </vue3-datatable>
                <el-pagination class="p-2 flex justify-end dark:bg-[#0e1726]" v-model:current-page="params.page_no" :page-size="params.page_size" :small="true"
                    layout="total, prev, pager, next" :total="totalRows" @size-change="handleSizeChange"
                    @current-change="handleCurrentChange" />
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import Vue3Datatable from '@bhplugin/vue3-datatable';
import { useAppStore } from '@/stores/index';
import { useRouter } from 'vue-router';
import { useMeta } from '@/composables/use-meta';
import { allocationAPI, infoApi, dictionaryAPI } from '@/api/api'
import IconBell from '@/components/icon/icon-bell.vue';
import IconCaretDown from '@/components/icon/icon-caret-down.vue';


useMeta({ title: 'Column Chooser Table' });
const router = useRouter();
const store = useAppStore();
const search = ref('');
const rows: any = ref([]); // 初始化rows为响应式引用
const customerList: any = ref([])//初始化客户列表
const warehouseList: any = ref([])//初始化客户列表
const collectingAccountList: any = ref([])//初始化客户列表
const deliveryList: any = ref([])//初始化客户列表
const orderTypeList: any = ref([])//初始化客户列表
const cols: any = ref([
    { field: 'sn', title: '调拨单编号', isUnique: true, hide: false },
    { field: 'transfer_time', title: '调拨时间', hide: false },
    { field: 'out_warehouse_id', title: '调出仓库', hide: false },
    { field: 'in_warehouse_id', title: '调入仓库', hide: false },
    { field: 'send_type', title: '配送方式', hide: false },
    { field: 'status', title: '状态', hide: false },
    { field: 'actions', title: '操作', hide: true }
]);
const params: any = ref([
    { page_no: 1, page_size: 10 }
])
/* 处理订单时间格式 */
const formatDate = (date) => {
    if (date) {
        const dt = new Date(date * 1000);
        const month = dt.getMonth() + 1 < 10 ? '0' + (dt.getMonth() + 1) : dt.getMonth() + 1;
        const day = dt.getDate() < 10 ? '0' + dt.getDate() : dt.getDate();
        return dt.getFullYear() + '/' + month + '/' + day;
    }
    return '';
};
/* 处理客户名称 */
const customerName = (value) => {
    if (value && customerList.value && customerList.value.data && customerList.value.data.data) {
        const type = customerList.value.data.data.find(type => type.id === value);
        return type ? type.name : '';
    }
}
/* 仓库名称 */
const warehouseName = (value) => {
    if (value && warehouseList.value && warehouseList.value.data) {
        const type = warehouseList.value.data.find(type => type.id === value);
        return type ? type.name : '';
    }
}
/* 配送方式*/
const deliveryName = (value) => {
    if (value && deliveryList.value && deliveryList.value.data && deliveryList.value.data.data) {
        const type = deliveryList.value.data.data.find(type => type.id === value);
        return type ? type.name : '';
    }
}
/* 状态*/
const typeName = (value) => {
    if (value && orderTypeList.value && orderTypeList.value.data && orderTypeList.value.data.data) {
        const type = orderTypeList.value.data.data.find(type => type.value === value);
        return type ? type.label : '';
    }
}
const totalRows: any = ref(0);
const handleSizeChange = (val: number) => {

    console.log(`${val} items per page`)
}
const handleCurrentChange = (val: number) => {
    params.page_no = val
    getOrderList()
    console.log(`current page: ${val}`)
}
onMounted(() => {
    getOrderList()
    getInforList()
});
/* 获取调拨列表 */
const getOrderList = async () => {
    allocationAPI.stockTransfer(params).then((res: any) => {
        params.page_no = res.data.page_no
        params.page_size = res.data.page_size
        totalRows.value = res.data.data_total
        console.log(params);
        rows.value = res.data.data; // 更新rows的值
    });
};
/* 获取仓库,字典,客户信息 */
const getInforList = async () => {
    warehouseList.value = await infoApi.warehouseList({})//获取仓库列表
    deliveryList.value = await dictionaryAPI.getExpressList(params)//获取配送方式
    /*  customerList.value = await infoApi.customer({})//获取客户列表
     collectingAccountList.value = await infoApi.collectingAccount({})//获取支付方式列表*/
    orderTypeList.value = await dictionaryAPI.getPage({ dictType: "transfer_status" })//获取状态 
}
const deliveryOrder = async (row) => {
    router.replace({
        name: 'check',
        params: { id: row.value.id }
    });
}
const goHome = () => {
    router.replace({
        name: 'home'
    });
}
</script>
<style>
.bh-pagination {
    display: none !important;
}
</style>