<template>
    <div>
          <ul class="flex space-x-2 rtl:space-x-reverse">
            <li>
                <a href="javascript:;" class=" hover:underline" @click="goIndex">首页</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"><!-- :totalRows="rows?.length" -->
                <vue3-datatable :rows="rows" :columns="cols" :sortable="true" :search="search"
                    style="min-height: 500px; " :show-pagination="false" 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 #type="data">
                        {{ deliveryName(data.value.type) }}
                    </template>
                    <template #warehouse_id="data">
                        {{ warehouseName(data.value.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 v-if="data.value.status !== 'HAS_HANDLE'" @click="deliveryOrder(data)"
                            class="btn btn-primary m-0">编辑</button>
                    </template>
                    <template #isActive="">
                     
                    </template>-->
                    <template #pagination>
                      
                    </template>
                </vue3-datatable>
                <el-pagination 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 { pageListAPI, 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: '仓库编号', hide: true },
    { field: 'name', title: '仓库名称', hide: false },
    { field: 'phone', title: '负责人手机号', hide: false },
   /*  { field: 'type', title: '仓库地址', hide: true }, */
    { field: 'description', title: '仓库说明', hide: true },
    /* { field: 'actual_stock', title: '实际库存', hide: false },
    { field: 'usable_stock', title: '可用库存', hide: true },
    { field: 'transit_stock', title: '在途库存', hide: true },
    { field: 'locked_stock', title: '冻结库存', hide: true },
    { field: 'warning_stock', title: '预警库存', hide: true } */
]);
const params: any = ref([
    { page_no: 1, page_size: 10 }
])
const totalRows: any = ref(0);

const goIndex=()=>{
    router.replace({
        name: 'home',
    });
}
const handleSizeChange = (val: number) => {

    console.log(`${val} items per page`)
}
const handleCurrentChange = (val: number) => {
    params.page_no = val
    init()
    console.log(`current page: ${val}`)
}
/* 处理订单时间格式 */
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 ) {
        const type = deliveryList.value.data.find(type => type.code === value);
        return type ? type.name : '';
    }
}
/* 状态*/
const typeName = (value) => {
    if (value) {
        return value == 'HAS_HANDLE' ? '已处理' : '未处理';
    }
}
onMounted(() => {
    init()
    getInforList()
});
/* 获取库存列表 */
const init = async () => {
    console.log(params);
    pageListAPI.warehouseList({page_no:params.page_no,page_size:params.page_size}).then((res: any) => {
        params.page_no = res.data.page_no
        params.page_size = res.data.page_size
        totalRows.value = res.data.data_total
        rows.value = res.data.data; // 更新rows的值
    });
};
/* 获取仓库,字典,客户信息 */
const getInforList = async () => {
    warehouseList.value = await infoApi.warehouseList({})//获取仓库列表
    deliveryList.value = await dictionaryAPI.listAreaType(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: 'addInventory',
        params: { id: row.value.id }
    });
}
</script>
<style>
.bh-pagination {
    display: none !important;
}
</style>


