<template>

    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
            <!-- 10入库 20出库 -->
            <el-form-item label="出入库编号" prop="sparePartAccessId">
                <el-input maxlength="100" v-model="queryParams.sparePartAccessId" placeholder="请输入备件编号" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="出入库类型" prop="sparePartAccessType">
                <el-select v-model="queryParams.sparePartAccessType" placeholder="请选择出入库类型" clearable
                    @change="handleQuery">
                    <el-option v-for="dict in com_access_type" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </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" style="min-height:32px">
            <!-- <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['sparePartAccess:sparePartAccess:add']">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['sparePartAccess:sparePartAccess:edit']">修改
                </el-button>
            </el-col> -->
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['sparePartAccess:sparePartAccess:remove']">删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['sparePartAccess:sparePartAccess:export']">导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-card shadow="never">
            <el-table ref="tableRef" v-loading="loading" :data="sparePartAccessList"
                @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
                <!-- <el-table-column type="selection" width="55" align="center"
                v-hasPermi="['sparePartAccess:sparePartAccess:edit', 'sparePartAccess:sparePartAccess:remove']" /> -->
                <el-table-column label="序号" align="center" type="index" width="50" />
                <el-table-column label="出入库编号" align="center" prop="sparePartAccessId" />
                <el-table-column label="出入库说明" align="center" prop="sparePartAccessExplain"
                    :show-overflow-tooltip="true" />
                <el-table-column label="出入库类型" align="center" prop="sparePartAccessType">
                    <template #default="scope">
                        <dict-tag :options="com_access_type" :value="scope.row.sparePartAccessType" />
                    </template>
                </el-table-column>
                <el-table-column label="创建者" align="center" prop="createBy" />
                <el-table-column label="创建时间" align="center" prop="createTime" width="160" sortable="custom"
                    :sort-orders="['descending', 'ascending']">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.createTime), "{y}-{m}-{d} {h}:{i}:{s}" }}</span>
                    </template>
                </el-table-column>

                <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200"
                    fixed="right"
                    v-hasPermi="['sparePartAccess:sparePartAccess:query', 'sparePartAccess:sparePartAccess:remove']">
                    <template #default="scope">
                        <el-button link type="primary" icon="View" @click="handleView(scope.row)"
                            v-hasPermi="['sparePartAccess:sparePartAccess:query']">详细
                        </el-button>
                        <!-- <el-button link icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['sparePartAccess:sparePartAccess:edit']">修改
                    </el-button> -->
                        <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"
                            v-hasPermi="['sparePartAccess:sparePartAccess:remove']">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <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="1000px" append-to-body>
            <el-form ref="sparePartAccessRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="出入库说明" prop="sparePartAccessExplain">
                    <el-input maxlength="300" v-model="form.sparePartAccessExplain" type="textarea"
                        placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="出入库类型" prop="sparePartAccessType">
                    <el-select v-model="form.sparePartAccessType" placeholder="请选择出入库类型">
                        <el-option v-for="dict in com_access_type" :key="dict.value" :label="dict.label"
                            :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-divider content-position="center">备件管理-出入库详情信息</el-divider>
                <el-row :gutter="10" class="mb8" style="min-height:32px">
                    <el-col :span="1.5">
                        <el-button type="primary" icon="Plus" @click="handleAddSparePartAccessDetails">添加</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" icon="Delete" @click="handleDeleteSparePartAccessDetails">删除
                        </el-button>
                    </el-col>
                </el-row>
                <el-table :data="sparePartAccessDetailsList" :row-class-name="rowSparePartAccessDetailsIndex"
                    @selection-change="handleSparePartAccessDetailsSelectionChange" ref="sparePartAccessDetails">
                    <el-table-column type="selection" width="50" align="center" />
                    <el-table-column label="序号" align="center" prop="index" width="50" />
                    <el-table-column label="备件名称" prop="sparePartName" width="150">
                        <template #default="scope">
                            <el-input maxlength="100" v-model="scope.row.sparePartName" placeholder="请输入备件名称" />
                        </template>
                    </el-table-column>
                    <el-table-column label="备件id" prop="sparePartId" width="150">
                        <template #default="scope">
                            <el-input maxlength="100" v-model="scope.row.sparePartId" placeholder="请输入备件id" />
                        </template>
                    </el-table-column>
                    <el-table-column label="备件编号" prop="sparePartNumber" width="150">
                        <template #default="scope">
                            <el-input maxlength="100" v-model="scope.row.sparePartNumber" placeholder="请输入备件编号" />
                        </template>
                    </el-table-column>
                    <el-table-column label="备件规格" prop="sparePartSpecifications" width="150">
                        <template #default="scope">
                            <el-input maxlength="100" v-model="scope.row.sparePartSpecifications"
                                placeholder="请输入备件规格" />
                        </template>
                    </el-table-column>
                    <el-table-column label="备件品牌" prop="sparePartBrand" width="150">
                        <template #default="scope">
                            <el-input maxlength="100" v-model="scope.row.sparePartBrand" placeholder="请输入备件品牌" />
                        </template>
                    </el-table-column>
                    <el-table-column label="备件出入库数量" prop="sparePartRetrievalQuantity" width="150">
                        <template #default="scope">
                            <el-input-number v-model="form.sparePartRetrievalQuantity" :precision="0" :step="1"
                                placeholder="请输入备件出入库数量" />
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" prop="remark" width="150">
                        <template #default="scope">
                            <el-input maxlength="300" v-model="scope.row.remark" placeholder="请输入备注" />
                        </template>
                    </el-table-column>
                </el-table>
            </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>
        <SparePartAccessDetailDrawer ref="sparePartAccessDetailDrawerRef" :sparePartAccessId="sparePartAccessId" />
    </div>




</template>

<script setup name="SparePartAccess">
import SparePartAccessDetailDrawer from "@/components/SparePartAccess/detail/drawer";

import { listSparePartAccess, getSparePartAccess, delSparePartAccess, addSparePartAccess, updateSparePartAccess, downloadSparePartAccess } from "@/api/sparePartAccess/sparePartAccess";

const { proxy } = getCurrentInstance();
const { com_access_type } = proxy.useDict('com_access_type');
const sparePartAccessList = ref([]);
const sparePartAccessDetailsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedSparePartAccessDetails = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const sparePartAccessId = ref(null)
const defaultSort = ref({ prop: "createTime", order: "descending" });

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        sparePartAccessExplain: null,
        sparePartAccessType: null,
    },
    rules: {
        sparePartAccessExplain: [
            {
                required: true, message: "出入库说明不能为空", trigger: "blur"
            }
        ],
        sparePartAccessType: [
            {
                required: true, message: "出入库类型不能为空", trigger: "change"
            }
        ],
    }
})
    ;

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

/** 查询备件管理-出入库记录列表 */
function getList() {
    loading.value = true;
    queryParams.value.orderByColumn = queryParams.value.orderByColumn || defaultSort.value.prop;
    queryParams.value.isAsc = queryParams.value.isAsc || defaultSort.value.order;
    listSparePartAccess(queryParams.value).then(response => {
        sparePartAccessList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

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

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

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

// 表单重置
function reset() {
    form.value = {
        sparePartAccessId: null,
        sparePartAccessExplain: null,
        sparePartAccessType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
    }
        ;
    sparePartAccessDetailsList.value = [];
    proxy.resetForm("sparePartAccessRef");
}

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

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    queryParams.value.pageNum = 1;
    proxy.$refs["tableRef"].sort(defaultSort.value.prop, defaultSort.value.order);
    // handleQuery();
}

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

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

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

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

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

/** 备件管理-出入库详情序号 */
function rowSparePartAccessDetailsIndex({ row, rowIndex }) {
    row.index = rowIndex + 1;
}

/** 备件管理-出入库详情添加按钮操作 */
function handleAddSparePartAccessDetails() {
    let obj = {};
    obj.sparePartName = "";
    obj.sparePartId = "";
    obj.sparePartNumber = "";
    obj.sparePartSpecifications = "";
    obj.sparePartBrand = "";
    obj.sparePartRetrievalQuantity = "";
    obj.remark = "";
    sparePartAccessDetailsList.value.push(obj);
}

/** 备件管理-出入库详情删除按钮操作 */
function handleDeleteSparePartAccessDetails() {
    if (checkedSparePartAccessDetails.value.length == 0) {
        proxy.$modal.msgError("请先选择要删除的备件管理-出入库详情数据");
    } else {
        const sparePartAccessDetailss = sparePartAccessDetailsList.value;
        const checkedSparePartAccessDetailss = checkedSparePartAccessDetails.value;
        sparePartAccessDetailsList.value = sparePartAccessDetailss.filter(function (item) {
            return checkedSparePartAccessDetailss.indexOf(item.index) == -1
        });
    }
}

/** 复选框选中数据 */
function handleSparePartAccessDetailsSelectionChange(selection) {
    checkedSparePartAccessDetails.value = selection.map(item => item.index)
}

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

/** 详细按钮操作 */
function handleView(row) {
    //// console.log(row)
    //// console.log(row.sparePartAccessId)
    sparePartAccessId.value = row.sparePartAccessId
    proxy.$refs["sparePartAccessDetailDrawerRef"].show()
}


/** 排序触发事件 */
function handleSortChange(column, prop, order) {
    queryParams.value.orderByColumn = column.prop;
    queryParams.value.isAsc = column.order;
    getList();
}

function initialData() {
    getList();
}

onActivated(() => {
    //// console.log("onActivated")
    initialData();
})
onMounted(() => {
    //// console.log("onMounted")
    initialData();
})

</script>

