<template>
    <div class="p-2">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div v-show="showSearch" class="mb-[10px]">
                <el-card shadow="hover">
                    <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="120px">
                        <el-form-item label="库位名称" prop="storageName">
                            <el-input v-model="queryParams.storageName" placeholder="请输入" clearable @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>
        </transition>

        <el-card shadow="never">
            <template #header>
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['warehouse:storageLocation:add']">新增库位</el-button>
                    </el-col>
                    <!-- <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['warehouse:storageLocation:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['warehouse:storageLocation:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['warehouse:storageLocation:export']">导出</el-button>
          </el-col> -->
                    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
            </template>

            <el-table v-loading="loading" border :data="locationList" @selection-change="handleSelectionChange">
                <!-- <el-table-column type="selection" width="55" align="center" /> -->
                <el-table-column label="库位名称" align="left" prop="storageName" />
                <el-table-column label="库位描述" align="left" prop="description">
                    <template #defult="scope">
                        <span>{{ scope.row.description||'--'  }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="350px" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-tooltip content="编辑" placement="top">
                            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['warehouse:storageLocation:edit']">编辑</el-button>
                        </el-tooltip>
                        <el-tooltip content="详情" placement="top">
                            <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['warehouse:storageLocation:edit']">详情</el-button>
                        </el-tooltip>

                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>
        <!-- 添加或修改库位管理对话框 -->
        <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
            <el-form ref="locationFormRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="库位名称" prop="storageName">
                    <el-input v-model="form.storageName" placeholder="请输入库位名称" />
                </el-form-item>
                <el-form-item label="库位描述" prop="description">
                    <el-input type="textarea" :rows="3" v-model="form.description" placeholder="请输入库位描述" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
        <!-- 详情对话框 -->
        <el-dialog title="详情" v-model="detailDialog.visible" width="23%" :close-on-click-modal="false" append-to-body>
            <div class="detail-container">
                <div class="detail-row">
                    <div class="detail-label">库位名称</div>
                    <div class="detail-content">{{ detailForm.storageName }}</div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">描述</div>
                    <div class="detail-content">{{ detailForm.description }}</div>
                </div>
            </div>
            <template #footer>
                <div class="dialog-footer" style="text-align: center">
                    <el-button size="large" @click="detailDialog.visible = false">关闭</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="StorageLocation" lang="ts">
    import { listLocation, getLocation, delLocation, addLocation, updateLocation } from '@/api/warehouse/storageLocation';
    import { StorageLocationVO, StorageLocationQuery, StorageLocationForm } from '@/api/warehouse/storageLocation/types';

    const { proxy } = getCurrentInstance() as ComponentInternalInstance;

    const locationList = ref < StorageLocationVO[] > ([]);
    const buttonLoading = ref(false);
    const loading = ref(true);
    const showSearch = ref(true);
    const ids = ref < Array < string | number >> ([]);
    const single = ref(true);
    const multiple = ref(true);
    const total = ref(0);

    const queryFormRef = ref < ElFormInstance > ();
    const locationFormRef = ref < ElFormInstance > ();

    const dialog = reactive < DialogOption > ({
        visible: false,
        title: ''
    });

    const detailDialog = reactive < DialogOption > ({
        visible: false
    });

    const detailForm = reactive < StorageLocationForm > ({
        id: undefined,
        storageName: undefined,
        description: undefined
    });

    const initFormData: StorageLocationForm = {
        id: undefined,
        storageName: undefined,
        description: undefined,
    }
    const data = reactive < PageData < StorageLocationForm,
        StorageLocationQuery >> ({
            form: { ...initFormData },
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                storageName: undefined,
                params: {}
            },
            rules: {
                id: [
                    { required: true, message: "不能为空", trigger: "blur" }
                ],
                storageName: [
                    { required: true, message: "库位名称不能为空", trigger: "blur" }
                ],
            }
        });

    const { queryParams, form, rules } = toRefs(data);

    /** 查询库位管理列表 */
    const getList = async () => {
        loading.value = true;
        const res = await listLocation(queryParams.value);
        locationList.value = res.rows;
        total.value = res.total;
        loading.value = false;
    }

    /** 取消按钮 */
    const cancel = () => {
        reset();
        dialog.visible = false;
    }

    /** 表单重置 */
    const reset = () => {
        form.value = { ...initFormData };
        locationFormRef.value ?.resetFields();
    }

    /** 搜索按钮操作 */
    const handleQuery = () => {
        queryParams.value.pageNum = 1;
        getList();
    }

    /** 重置按钮操作 */
    const resetQuery = () => {
        queryFormRef.value ?.resetFields();
        handleQuery();
    }

    /** 多选框选中数据 */
    const handleSelectionChange = (selection: StorageLocationVO[]) => {
        ids.value = selection.map(item => item.id);
        single.value = selection.length != 1;
        multiple.value = !selection.length;
    }

    /** 新增按钮操作 */
    const handleAdd = () => {
        reset();
        dialog.visible = true;
        dialog.title = "新增";
    }

    /** 修改按钮操作 */
    const handleUpdate = async (row ? : StorageLocationVO) => {
        reset();
        const _id = row ?.id || ids.value[0]
        const res = await getLocation(_id);
        Object.assign(form.value, res.data);
        dialog.visible = true;
        dialog.title = "编辑";
    }

    const handleDetail = async (row ? : StorageLocationVO) => {
        try {
            const res = await getLocation(row.id);
            Object.assign(detailForm, res.data);
            detailDialog.visible = true;
        } catch (error) {
            proxy ?.$modal.msgError("获取详情失败");
        }
    }

    /** 提交按钮 */
    const submitForm = () => {
        locationFormRef.value ?.validate(async (valid: boolean) => {
            if (valid) {
                buttonLoading.value = true;
                if (form.value.id) {
                    await updateLocation(form.value).finally(() => buttonLoading.value = false);
                } else {
                    await addLocation(form.value).finally(() => buttonLoading.value = false);
                }
                proxy ?.$modal.msgSuccess("操作成功");
                dialog.visible = false;
                await getList();
            }
        });
    }

    /** 删除按钮操作 */
    const handleDelete = async (row ? : StorageLocationVO) => {
        const _ids = row ?.id || ids.value;
        await proxy ?.$modal.confirm('是否确认删除库位管理编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
        await delLocation(_ids);
        proxy ?.$modal.msgSuccess("删除成功");
        await getList();
    }

    /** 导出按钮操作 */
    const handleExport = () => {
        proxy ?.download('warehouse/storageLocation/export', {
            ...queryParams.value
        }, `location_${new Date().getTime()}.xlsx`)
    }

    onMounted(() => {
        getList();
    });
</script>
<style scoped>
    .detail-container {
        background: #fff;
    }

    .detail-row {
        display: flex;
        border-bottom: 1px solid #dcdfe6;
    }

    .detail-row:last-child {
        border-bottom: none;
    }

    .detail-label {
        width: 50%;
        padding: 12px 20px;
        background: #f5f7fa;
        border-right: 1px solid #dcdfe6;
        box-sizing: border-box;
    }

    .detail-content {
        width: 50%;
        padding: 12px 20px;
        box-sizing: border-box;
    }

    :deep(.el-dialog__footer) {
        margin: 0;
        border-top: 1px solid #dcdfe6;
        padding: 12px 20px;
    }
</style>>