<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="warehouseAccessExplain">
                <el-input v-model="queryParams.warehouseAccessExplain" placeholder="请输入出入库说明" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="出入库类型" prop="warehouseAccessType">
                <el-select v-model="queryParams.warehouseAccessType" 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 label="创建者" prop="createBy">
                <el-input v-model="queryParams.createBy" 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="['warehouseAccess:warehouseAccess:add']">
                <el-button type="primary" plain icon="Plus" @click="handleAdd"
                   >新增
                </el-button>
            </el-col>
            <el-col :span="1.5" v-hasPermi="['warehouseAccess:warehouseAccess:edit']">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                   >修改
                </el-button>
            </el-col>
            <el-col :span="1.5" v-hasPermi="['warehouseAccess:warehouseAccess:remove']">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除
                </el-button>
            </el-col> -->
            <el-col :span="1.5" v-hasPermi="['warehouseAccess:warehouseAccess: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-card shadow="never">
            <el-table ref="tableRef" v-loading="loading" :data="warehouseAccessList"
                @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
                <el-table-column type="selection" width="55" align="center"
                    v-hasPermi="['warehouseAccess:warehouseAccess:remove']" />
                <el-table-column label="序号" align="center" type="index" width="50" />
                <el-table-column label="出入库编号" align="center" prop="warehouseAccessId" />
                <el-table-column label="出入库说明" align="center" prop="warehouseAccessExplain" />
                <el-table-column label="出入库类型" align="center" prop="warehouseAccessType">
                    <template #default="scope">
                        <dict-tag :options="com_access_type" :value="scope.row.warehouseAccessType" />
                    </template>
                </el-table-column>
                <el-table-column label="创建者" align="center" prop="createBy" />
                <el-table-column label="创建时间" align="center" prop="createTime" width="180" 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="['warehouseAccess:warehouseAccess:query', 'warehouseAccess:warehouseAccess:edit', 'warehouseAccess:warehouseAccess:remove']">
                    <template #default="scope">
                        <el-button link type="primary" icon="View" @click="handleView(scope.row)"
                            v-hasPermi="['warehouseAccess:warehouseAccess:query']">详细
                        </el-button>
                        <!-- <el-button link type="success" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['warehouseAccess:warehouseAccess:edit']">修改
                    </el-button>
                    <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['warehouseAccess:warehouseAccess: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="500px" append-to-body>
            <el-form ref="warehouseAccessRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="出入库说明" prop="warehouseAccessExplain">
                    <el-input v-model="form.warehouseAccessExplain" placeholder="请输入出入库说明" />
                </el-form-item>
                <el-form-item label="出入库类型" prop="warehouseAccessType">
                    <el-select v-model="form.warehouseAccessType" placeholder="请选择出入库类型 10入库 20出库">
                        <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-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                </el-form-item>
                <el-divider content-position="center">备件管理-出入库详情信息</el-divider>
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" icon="Plus" @click="handleAddWarehouseAccessDetails">添加</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" icon="Delete" @click="handleDeleteWarehouseAccessDetails">删除</el-button>
                    </el-col>
                </el-row>
                <el-table :data="warehouseAccessDetailsList" :row-class-name="rowWarehouseAccessDetailsIndex"
                    @selection-change="handleWarehouseAccessDetailsSelectionChange" ref="warehouseAccessDetails">
                    <el-table-column type="selection" width="50" align="center" />
                    <el-table-column label="序号" align="center" prop="index" width="50" />
                    <el-table-column label="物料名称" prop="warehouseName" width="150">
                        <template #default="scope">
                            <el-input v-model="scope.row.warehouseName" placeholder="请输入物料名称" />
                        </template>
                    </el-table-column>
                    <el-table-column label="出入库id" prop="warehouseAccessId" width="150">
                        <template #default="scope">
                            <el-input v-model="scope.row.warehouseAccessId" placeholder="请输入出入库id" />
                        </template>
                    </el-table-column>
                    <el-table-column label="物料编号" prop="warehouseNumber" width="150">
                        <template #default="scope">
                            <el-input v-model="scope.row.warehouseNumber" placeholder="请输入物料编号" />
                        </template>
                    </el-table-column>
                    <el-table-column label="物料规格" prop="warehouseSpecifications" width="150">
                        <template #default="scope">
                            <el-input v-model="scope.row.warehouseSpecifications" placeholder="请输入物料规格" />
                        </template>
                    </el-table-column>
                    <el-table-column label="物料出入库数量" prop="warehouseRetrievalQuantity" width="150">
                        <template #default="scope">
                            <el-input v-model="scope.row.warehouseRetrievalQuantity" placeholder="请输入物料出入库数量" />
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" prop="remark" width="150">
                        <template #default="scope">
                            <el-input v-model="scope.row.remark" placeholder="请输入备注" />
                        </template>
                    </el-table-column>
                    <el-table-column label="物料出入库前数量" prop="warehouseQuantity" width="150">
                        <template #default="scope">
                            <el-input v-model="scope.row.warehouseQuantity" 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>


        <WarehouseAccessDetailDrawer ref="warehouseAccessDetailDrawerRef" :warehouseAccessId="warehouseAccessId" />


    </div>
</template>

<script setup name="WarehouseAccess">
import WarehouseAccessDetailDrawer from "@/components/WarehouseAccess/detail/drawer";
import { listWarehouseAccess, getWarehouseAccess, delWarehouseAccess, addWarehouseAccess, updateWarehouseAccess, downloadWarehouseAccess } from "@/api/warehouseAccess/warehouseAccess";

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

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseAccessExplain: null,
        warehouseAccessType: null,
        createBy: null,
    },
    rules: {
        warehouseAccessExplain: [
            {
                required: true, message: "出入库说明不能为空", trigger: "blur"
            }
        ],
        warehouseAccessType: [
            {
                required: true, message: "出入库类型 10入库 20出库不能为空", 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;
    listWarehouseAccess(queryParams.value).then(response => {
        warehouseAccessList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

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

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

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

// 表单重置
function reset() {
    form.value = {
        warehouseAccessId: null,
        warehouseAccessExplain: null,
        warehouseAccessType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
    }
        ;
    warehouseAccessDetailsList.value = [];
    proxy.resetForm("warehouseAccessRef");
}

/** 搜索按钮操作 */
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.warehouseAccessId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

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

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

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

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

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

/** 备件管理-出入库详情添加按钮操作 */
function handleAddWarehouseAccessDetails() {
    let obj = {};
    obj.warehouseName = "";
    obj.warehouseAccessId = "";
    obj.warehouseNumber = "";
    obj.warehouseSpecifications = "";
    obj.warehouseRetrievalQuantity = "";
    obj.remark = "";
    obj.warehouseQuantity = "";
    warehouseAccessDetailsList.value.push(obj);
}

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

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

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

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


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

getList();
</script>

