<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
            <el-form-item label="维保记录编号" prop="maintenanceRecordId">
                <el-input maxlength="100" v-model="queryParams.maintenanceRecordId" placeholder="请输入维保记录编号" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="维保项目" prop="maintenanceProjectName">
                <el-input maxlength="100" v-model="queryParams.maintenanceProjectName" placeholder="请输入维保项目" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="维保项目标准" prop="maintenanceProjectStandard">
                <el-input maxlength="300" v-model="queryParams.maintenanceProjectStandard" placeholder="请输入维保项目标准"
                    clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="维保项目责任人" prop="maintenanceProjectPersonLiable">
                <el-select v-model="queryParams.maintenanceProjectPersonLiable" filterable placeholder="请选择角色"
                    @change="handleQuery" clearable>
                    <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName"
                        :value="item.roleId" />
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="维保项目责任人" prop="maintenanceProjectPersonLiable">
                <el-input maxlength="100" v-model="queryParams.maintenanceProjectPersonLiable" placeholder="请输入维保项目责任人" clearable
                    @keyup.enter="handleQuery"  />
            </el-form-item> -->
            <el-form-item label="周期" prop="maintenanceProjectCycle">
                <el-select v-model="queryParams.maintenanceProjectCycle" filterable placeholder="请选择维保项目周期"
                    @change="handleQuery" clearable>
                    <el-option v-for="item in maintenance_project_cycle" :key="item.value" :label="item.label"
                        :value="item.value" />
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="周期" prop="maintenanceProjectCycle">
                <el-input maxlength="100" v-model="queryParams.maintenanceProjectCycle" 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" style="min-height:32px">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['maintenanceRecordDetails:maintenanceRecordDetails:add']">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['maintenanceRecordDetails:maintenanceRecordDetails:edit']">修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['maintenanceRecordDetails:maintenanceRecordDetails:remove']">删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['maintenanceRecordDetails:maintenanceRecordDetails:export']">导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="maintenanceRecordDetailsList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="维保记录详情编号" align="center" prop="maintenanceRecordDetailsId" />
            <el-table-column label="维保记录编号" align="center" prop="maintenanceRecordId" />
            <el-table-column label="维保项目" align="center" prop="maintenanceProjectName" />
            <el-table-column label="维保项目标准" align="center" prop="maintenanceProjectStandard" />
            <el-table-column label="维保项目责任人" align="center" prop="maintenanceProjectPersonLiable">
                <template #default="scope">
                    <TextTag :options="roleList" column="roleId" label="roleName"
                        :value="scope.row.maintenanceProjectPersonLiable" />
                </template>
            </el-table-column>
            <el-table-column label="周期" align="center" prop="maintenanceProjectCycle">
                <template #default="scope">
                    <dict-tag :options="maintenance_project_cycle" :value="scope.row.maintenanceProjectCycle" />
                </template>
            </el-table-column>
            <el-table-column label="维保项目图片" align="center" prop="maintenanceProjectFile" />
            <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 link type="primary" icon="View" @click="handleView(scope.row)"
                        v-hasPermi="['maintenanceRecordDetails:maintenanceRecordDetails:query']">详细
                    </el-button>
                    <el-button link type="success" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['maintenanceRecordDetails:maintenanceRecordDetails:edit']">修改
                    </el-button>
                    <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['maintenanceRecordDetails:maintenanceRecordDetails: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="maintenanceRecordDetailsRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="维保记录编号" prop="maintenanceRecordId">
                    <el-input maxlength="100" v-model="form.maintenanceRecordId" placeholder="请输入维保记录编号" />
                </el-form-item>
                <el-form-item label="维保项目" prop="maintenanceProjectName">
                    <el-input maxlength="100" v-model="form.maintenanceProjectName" placeholder="请输入维保项目" />
                </el-form-item>
                <el-form-item label="维保项目标准" prop="maintenanceProjectStandard">
                    <el-input maxlength="100" v-model="form.maintenanceProjectStandard" placeholder="请输入维保项目标准" />
                </el-form-item>
                <el-form-item label="维保项目责任人" prop="maintenanceProjectPersonLiable">
                    <el-input maxlength="100" v-model="form.maintenanceProjectPersonLiable" placeholder="请输入维保项目责任人" />
                </el-form-item>
                <el-form-item label="周期" prop="maintenanceProjectCycle">
                    <el-input maxlength="100" v-model="form.maintenanceProjectCycle" placeholder="请输入周期" />
                </el-form-item>
                <el-form-item label="维保项目图片">
                    <file-upload v-model="form.maintenanceProjectFile" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input maxlength="100" 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="MaintenanceRecordDetails">
import { listMaintenanceRecordDetails, getMaintenanceRecordDetails, delMaintenanceRecordDetails, addMaintenanceRecordDetails, updateMaintenanceRecordDetails } from "@/api/maintenanceRecordDetails/maintenanceRecordDetails";
import { listRole } from "@/api/system/role";

const { proxy } = getCurrentInstance();
const maintenanceRecordDetailsList = ref([]);
const roleList = ref([]);
const { maintenance_project_cycle } = proxy.useDict('maintenance_project_cycle');

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,
        maintenanceRecordId: null,
        maintenanceProjectName: null,
        maintenanceProjectStandard: null,
        maintenanceProjectPersonLiable: null,
        maintenanceProjectCycle: null,
        maintenanceProjectFile: null,
    },
    rules: {
    }
})
    ;

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

/** 查询维保记录详情列表 */
function getList() {
    loading.value = true;
    listMaintenanceRecordDetails(queryParams.value).then(response => {
        maintenanceRecordDetailsList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

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

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

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

// 表单重置
function reset() {
    form.value = {
        maintenanceRecordDetailsId: null,
        maintenanceRecordId: null,
        maintenanceProjectName: null,
        maintenanceProjectStandard: null,
        maintenanceProjectPersonLiable: null,
        maintenanceProjectCycle: null,
        maintenanceProjectFile: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
    }
        ;
    proxy.resetForm("maintenanceRecordDetailsRef");
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加维保记录详情";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const maintenanceRecordDetailsId =
        row.maintenanceRecordDetailsId || ids.value
    getMaintenanceRecordDetails(maintenanceRecordDetailsId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改维保记录详情";
    });
}

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

/** 删除按钮操作 */
function handleDelete(row) {
    const maintenanceRecordDetailsIds = row.maintenanceRecordDetailsId || ids.value;
    proxy.$modal.confirm('是否确认删除维保记录详情编号为"' + maintenanceRecordDetailsIds + '"的数据项？').then(function () {
        return delMaintenanceRecordDetails(maintenanceRecordDetailsIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

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

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


function initialData() {
    listRole(proxy.addDateRange({
        pageNum: 1,
        pageSize: 9999,
    }, undefined)).then(response => {
        roleList.value = response.rows;
    });
    //// console.log("onMounted")
    getList();
}

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



</script>

