<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="name">
                            <el-input v-model="queryParams.name" 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:productManufacturer:add']">新增生产厂商</el-button>
                    </el-col>
                    <!-- <el-col :span="1.5">
                        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['warehouse:productManufacturer:edit']">修改</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['warehouse:productManufacturer:remove']">删除</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['warehouse:productManufacturer: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="manufacturerList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="生产厂商" align="center" prop="name" />
                <el-table-column label="联系人名称" align="center" prop="contactName">
                    <template #default="scope">
                        <span>{{ scope.row.contactName||'--'  }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="联系人电话" align="center" prop="contactPhone">
                    <template #default="scope">
                        <span>{{ scope.row.contactPhone||'--'  }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="联系人地址" align="center" prop="address">
                    <template #default="scope">
                        <span>{{ scope.row.address||'--'  }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" 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:productManufacturer:edit']">编辑</el-button>
                        </el-tooltip>
                        <el-tooltip content="详情" placement="top">
                            <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['warehouse:productManufacturer:query']">详情</el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top">
                            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['warehouse:productManufacturer:remove']">删除</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="38%" append-to-body>
            <el-form ref="manufacturerFormRef" :model="form" :rules="rules" label-width="130px">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="生产厂商" prop="name">
                            <el-input v-model="form.name" placeholder="请输入" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系人" prop="contactName">
                            <el-input v-model="form.contactName" placeholder="请输入" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="联系电话" prop="contactPhone">
                            <el-input v-model="form.contactPhone" placeholder="请输入" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系地址" prop="address">
                            <el-input v-model="form.address" placeholder="请输入" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <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="38%" :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.name }}</div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">联系人</div>
                    <div class="detail-content">{{ detailForm.contactName }}</div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">联系电话</div>
                    <div class="detail-content">{{ detailForm.contactPhone }}</div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">联系地址</div>
                    <div class="detail-content">{{ detailForm.address }}</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="ProductManufacturer" lang="ts">
    import { listManufacturer, getManufacturer, delManufacturer, addManufacturer, updateManufacturer } from '@/api/warehouse/productManufacturer';
    import { ManufacturerVO, ManufacturerQuery, ManufacturerForm } from '@/api/warehouse/productManufacturer/types';
    import { ComponentInternalInstance, getCurrentInstance, ref, reactive, toRefs, onMounted } from 'vue';
    import type { FormInstance } from 'element-plus';

    interface DialogOption {
        visible: boolean;
        title ? : string;
    }

    interface PageData < T, Q > {
        form: T;
        queryParams: {
            pageNum: number;
            pageSize: number;
            params: Record < string,
            any > ;
        } & Q;
        rules ? : Record < string,
        any[] > ;
    }

    const { proxy } = getCurrentInstance() as ComponentInternalInstance;

    const manufacturerList = ref < ManufacturerVO[] > ([]);
    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 < FormInstance > ();
    const manufacturerFormRef = ref < FormInstance > ();

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

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

    const detailForm = reactive < ManufacturerForm > ({
        id: undefined,
        name: undefined,
        contactName: undefined,
        contactPhone: undefined,
        address: undefined,
        description: undefined
    });

    const initFormData: ManufacturerForm = {
        id: undefined,
        name: undefined,
        contactName: undefined,
        contactPhone: undefined,
        address: undefined,
        description: undefined,
    }
    const data = reactive < PageData < ManufacturerForm,
        ManufacturerQuery >> ({
            form: { ...initFormData },
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                name: undefined,
                params: {}
            },
            rules: {

                name: [
                    { required: true, message: "生产厂商不能为空", trigger: "blur" }
                ],
            }
        });

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

    /** 查询生产厂商列表 */
    const getList = async () => {
        loading.value = true;
        const res = await listManufacturer(queryParams.value);
        manufacturerList.value = res.rows;
        total.value = res.total;
        loading.value = false;
    }

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

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

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

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

    /** 多选框选中数据 */
    const handleSelectionChange = (selection: ManufacturerVO[]) => {
        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 ? : ManufacturerVO) => {
        reset();
        const _id = row ?.id || ids.value[0]
        const res = await getManufacturer(_id);
        Object.assign(form.value, res.data);
        dialog.visible = true;
        dialog.title = "编辑";
    }
    const handleDetail = async (row: ManufacturerVO) => {
        try {
            const res = await getManufacturer(row.id);
            Object.assign(detailForm, res.data);
            detailDialog.visible = true;
        } catch (error) {
            proxy ?.$modal.msgError("获取详情失败");
        }
    };

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

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

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

    onMounted(() => {
        getList();
    });
</script>

<style scoped>
    .dialog-footer {
        text-align: center;
        padding: 10px 0;
    }

    :deep(.el-dialog) {
        border-radius: 4px;
    }

    :deep(.el-dialog__header) {
        margin: 0;
        padding: 12px 20px;
        border-bottom: 1px solid #dcdfe6;
        background-color: #f5f7fa;
    }

    :deep(.el-dialog__title) {
        font-size: 14px;
        font-weight: 500;
    }

    :deep(.el-dialog__headerbtn) {
        top: 14px;
    }

    :deep(.el-dialog__body) {
        padding: 0;
    }

    .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>