<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="备件名称" prop="sparePartName">
                <el-input v-model="queryParams.sparePartName" placeholder="请输入备件名称" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="备件id" prop="sparePartId">
                <el-input v-model="queryParams.sparePartId" placeholder="请输入备件id" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="出入库id" prop="sparePartAccessId">
                <el-input v-model="queryParams.sparePartAccessId" placeholder="请输入出入库id" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item> -->
            <el-form-item label="备件编号" prop="sparePartNumber">
                <el-input v-model="queryParams.sparePartNumber" placeholder="请输入备件编号" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="备件规格" prop="sparePartSpecifications">
                <el-input v-model="queryParams.sparePartSpecifications" placeholder="请输入备件规格" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="备件品牌" prop="sparePartBrand">
                <el-input v-model="queryParams.sparePartBrand" placeholder="请输入备件品牌" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="备件出入库数量" prop="sparePartRetrievalQuantity">
                <el-input v-model="queryParams.sparePartRetrievalQuantity" 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-row :gutter="10" class="mb8">
            <!-- <el-col :span="1.5" v-hasPermi="['business:sparePartAccessDetails:add']">
                <el-button type="primary" plain icon="Plus" @click="handleAdd">新增
                </el-button>
            </el-col>
            <el-col :span="1.5" v-hasPermi="['business:sparePartAccessDetails:edit']">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">修改
                </el-button>
            </el-col>
            <el-col :span="1.5" v-hasPermi="['business:sparePartAccessDetails:remove']">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除
                </el-button>
            </el-col> -->
            <el-col :span="1.5" v-hasPermi="['business:sparePartAccessDetails:export']">
                <el-button type="warning" plain icon="Download" @click="handleExport">导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="sparePartAccessDetailsList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="备件id" align="center" prop="sparePartAccessDetailsId" />
            <el-table-column label="备件名称" align="center" prop="sparePartName" />
            <el-table-column label="备件id" align="center" prop="sparePartId" />
            <el-table-column label="出入库id" align="center" prop="sparePartAccessId" />
            <el-table-column label="备件编号" align="center" prop="sparePartNumber" />
            <el-table-column label="备件规格" align="center" prop="sparePartSpecifications" />
            <el-table-column label="备件品牌" align="center" prop="sparePartBrand" />
            <el-table-column label="备件出入库数量" align="center" prop="sparePartRetrievalQuantity" />
            <!--  <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
                <template #default="scope">
                    <el-button text type="primary" icon="View" @click="handleView(scope.row)"
                        v-hasPermi="['business:sparePartAccessDetails:query']">详细
                    </el-button>
                    <el-button text type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['business:sparePartAccessDetails:edit']">修改
                    </el-button>
                    <el-button text type="danger" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['business:sparePartAccessDetails:remove']">删除
                    </el-button>
                </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-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="sparePartAccessDetailsRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="备件名称" prop="sparePartName">
                    <el-input v-model="form.sparePartName" placeholder="请输入备件名称" />
                </el-form-item>
                <el-form-item label="备件id" prop="sparePartId">
                    <el-input v-model="form.sparePartId" placeholder="请输入备件id" />
                </el-form-item>
                <el-form-item label="出入库id" prop="sparePartAccessId">
                    <el-input v-model="form.sparePartAccessId" placeholder="请输入出入库id" />
                </el-form-item>
                <el-form-item label="备件编号" prop="sparePartNumber">
                    <el-input v-model="form.sparePartNumber" placeholder="请输入备件编号" />
                </el-form-item>
                <el-form-item label="备件规格" prop="sparePartSpecifications">
                    <el-input v-model="form.sparePartSpecifications" placeholder="请输入备件规格" />
                </el-form-item>
                <el-form-item label="备件品牌" prop="sparePartBrand">
                    <el-input v-model="form.sparePartBrand" placeholder="请输入备件品牌" />
                </el-form-item>
                <el-form-item label="备件出入库数量" prop="sparePartRetrievalQuantity">
                    <el-input v-model="form.sparePartRetrievalQuantity" placeholder="请输入备件出入库数量" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>





</template>

<script setup name="SparePartAccessDetails">
import { listSparePartAccessDetails, getSparePartAccessDetails, delSparePartAccessDetails, addSparePartAccessDetails, updateSparePartAccessDetails } from "@/api/business/sparePartAccessDetails";

const { proxy } = getCurrentInstance();
const sparePartAccessDetailsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        sparePartName: null,
        sparePartId: null,
        sparePartAccessId: null,
        sparePartNumber: null,
        sparePartSpecifications: null,
        sparePartBrand: null,
        sparePartRetrievalQuantity: null,
    },
    rules: {
        sparePartName: [
            {
                required: true, message: "备件名称不能为空", trigger: "blur"
            }
        ],
        sparePartId: [
            {
                required: true, message: "备件id不能为空", trigger: "blur"
            }
        ],
        sparePartAccessId: [
            {
                required: true, message: "出入库id不能为空", trigger: "blur"
            }
        ],
        sparePartNumber: [
            {
                required: true, message: "备件编号不能为空", trigger: "blur"
            }
        ],
        sparePartSpecifications: [
            {
                required: true, message: "备件规格不能为空", trigger: "blur"
            }
        ],
        sparePartBrand: [
            {
                required: true, message: "备件品牌不能为空", trigger: "blur"
            }
        ],
        sparePartRetrievalQuantity: [
            {
                required: true, message: "备件出入库数量不能为空", trigger: "blur"
            }
        ],
    }
})
    ;

const { queryParams, form, rules } = toRefs(data);
const baseUrl = import.meta.env.VITE_APP_BASE_API;

/** 查询备件管理-出入库详情列表 */
function getList() {
    loading.value = true;
    listSparePartAccessDetails(queryParams.value).then(response => {
        sparePartAccessDetailsList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

function addList() {
    loading.value = true;
    listSparePartAccessDetails(queryParams.value)
        .then(response => {
            sparePartAccessDetailsList.value.push(...response.rows);
            total.value = response.total;
            if (sparePartAccessDetailsList.value.length >= total.value) finished.value = true;
            loading.value = false;
        })
        .catch(response => {
            loading.value = false;
        });
}

// 下拉刷新
function handlePullDownRefresh() {
    queryParams.value.pageNum = 1;
    sparePartAccessDetailsList.value = [];
    addList();
}

// 滚动到底部的事件
function handleReachBottom() {
    queryParams.value.pageNum++;
    addList();
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        sparePartAccessDetailsId: null,
        sparePartName: null,
        sparePartId: null,
        sparePartAccessId: null,
        sparePartNumber: null,
        sparePartSpecifications: null,
        sparePartBrand: null,
        sparePartRetrievalQuantity: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
    }
        ;
    proxy.resetForm("sparePartAccessDetailsRef");
}

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

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.sparePartAccessDetailsId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加备件管理-出入库详情";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const sparePartAccessDetailsId =
        row.sparePartAccessDetailsId || ids.value
    getSparePartAccessDetails(sparePartAccessDetailsId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改备件管理-出入库详情";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["sparePartAccessDetailsRef"].validate(valid => {
        if (valid) {
            if (form.value.sparePartAccessDetailsId != null) {
                updateSparePartAccessDetails(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addSparePartAccessDetails(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const sparePartAccessDetailsIds = row.sparePartAccessDetailsId || ids.value;
    proxy.$modal.confirm('是否确认删除备件管理-出入库详情编号为"' + sparePartAccessDetailsIds + '"的数据项？').then(function () {
        return delSparePartAccessDetails(sparePartAccessDetailsIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
    downloadSparePartAccessDetails(queryParams.value)
    // proxy.download('business/sparePartAccessDetails/export', {
    //   ...queryParams.value
    // }, `sparePartAccessDetails_${new Date().getTime()}.xlsx`)
}

/** 详细按钮操作 */
function handleView(row) {
}

getList();
</script>

