<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
            <!-- <el-form-item label="维保编号" prop="maintenanceId">
                <el-input v-model="queryParams.maintenanceId" placeholder="请输入维保编号" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item> -->
            <el-form-item label="维保" prop="maintenanceId">
                <el-tree-select v-model="queryParams.maintenanceId" :data="maintenanceOptions"
                    :props="{ value: 'maintenanceId', label: 'maintenanceName', children: 'children' }"
                    value-key="maintenanceId" placeholder="请选择维保" check-strictly filterable clearable
                    @change="handleQuery" @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>
                <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="['maintenanceProject:maintenanceProject:add']">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['maintenanceProject:maintenanceProject:edit']">修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['maintenanceProject:maintenanceProject:remove']">删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['maintenanceProject:maintenanceProject:export']">导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="maintenanceProjectList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column label="维保计划详情id" align="center" prop="maintenanceProjectId" />
            <el-table-column label="维保编号" align="center" prop="maintenanceId" /> -->
            <el-table-column type="expand" width="30">
                <template #default="scope">
                    <div class="padding">
                        <el-descriptions :column="1">
                            <el-descriptions-item label="维保">
                                <TextTag :options="maintenanceList" column="maintenanceId" label="maintenanceName"
                                    :value="scope.row.maintenanceId" style="display:inline-block" />
                            </el-descriptions-item>
                            <el-descriptions-item label="维保项目">
                                {{ scope.row.maintenanceProjectName }}
                            </el-descriptions-item>
                            <el-descriptions-item label="维保项目标准">
                                {{ scope.row.maintenanceProjectStandard }}
                            </el-descriptions-item>
                            <el-descriptions-item label="维保项目责任人">
                                <TextTag :options="roleList" column="roleId" label="roleName"
                                    :value="scope.row.maintenanceProjectPersonLiable" style="display:inline-block" />
                            </el-descriptions-item>
                        </el-descriptions>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="维保" align="center" prop="maintenanceId">
                <template #default="scope">
                    <TextTag :options="maintenanceList" column="maintenanceId" label="maintenanceName"
                        :value="scope.row.maintenanceId" />
                </template>
            </el-table-column>
            <el-table-column label="维保项目" align="center" prop="maintenanceProjectName" :show-overflow-tooltip="true" />
            <el-table-column label="维保项目标准" align="center" prop="maintenanceProjectStandard"
                :show-overflow-tooltip="true" />
            <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" 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="['maintenanceProject:maintenanceProject:query']">详细
                    </el-button> -->
                    <el-button link type="success" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['maintenanceProject:maintenanceProject:edit']">修改
                    </el-button>
                    <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['maintenanceProject:maintenanceProject: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="maintenanceProjectRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="维保" prop="maintenanceId">
                    <el-tree-select v-model="form.maintenanceId" :data="maintenanceOptions"
                        :props="{ value: 'maintenanceId', label: 'maintenanceName', children: 'children' }"
                        value-key="maintenanceId" placeholder="请选择维保" check-strictly />
                    <!-- <el-input maxlength="100" v-model="form.maintenanceId" 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="300" v-model="form.maintenanceProjectStandard" placeholder="请输入维保项目标准"
                        type="textarea" :rows="4" />
                </el-form-item>
                <el-form-item label="维保项目责任人" prop="maintenanceProjectPersonLiable">
                    <el-select v-model="form.maintenanceProjectPersonLiable" filterable placeholder="请选择维保项目责任人"
                        @change="handleQuery">
                        <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName"
                            :value="item.roleId" />
                    </el-select>
                </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="MaintenanceProject">
import { listMaintenanceProject, getMaintenanceProject, delMaintenanceProject, addMaintenanceProject, updateMaintenanceProject } from "@/api/maintenanceProject/maintenanceProject";
import { addRole, changeRoleStatus, dataScope, delRole, getRole, listRole, updateRole } from "@/api/system/role";
import { listMaintenance, getMaintenance, delMaintenance, addMaintenance, updateMaintenance, downloadMaintenance } from "@/api/maintenance/maintenance";

const { proxy } = getCurrentInstance();
const maintenanceList = ref([]);
const maintenanceProjectList = ref([]);
const roleList = ref([]);

const maintenanceOptions = 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,
        maintenanceId: null,
        maintenanceProjectName: null,
        maintenanceProjectStandard: null,
        maintenanceProjectPersonLiable: null,
    },
    rules: {
        maintenanceId: [
            {
                required: true, message: "维保编号不能为空", trigger: "blur"
            }
        ],
        maintenanceProjectName: [
            {
                required: true, message: "维保项目不能为空", trigger: "blur"
            }
        ],
        maintenanceProjectStandard: [
            {
                required: true, message: "维保项目标准不能为空", trigger: "blur"
            }
        ],
        maintenanceProjectPersonLiable: [
            {
                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;
    getTreeselect();
    listMaintenanceProject(queryParams.value).then(response => {
        maintenanceProjectList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

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

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

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

// 表单重置
function reset() {
    form.value = {
        maintenanceProjectId: null,
        maintenanceId: null,
        maintenanceProjectName: null,
        maintenanceProjectStandard: null,
        maintenanceProjectPersonLiable: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
    }
        ;
    proxy.resetForm("maintenanceProjectRef");
}

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

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

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

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

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const maintenanceProjectId =
        row.maintenanceProjectId || ids.value
    getMaintenanceProject(maintenanceProjectId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改维保项目";
    });
}

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

/** 删除按钮操作 */
function handleDelete(row) {
    const maintenanceProjectIds = row.maintenanceProjectId || ids.value;
    const maintenanceProjectListFilter = Array.isArray(maintenanceProjectIds)
        ? maintenanceProjectList.value.filter((item) => maintenanceProjectIds.includes(item.maintenanceProjectId))
        : maintenanceProjectList.value.filter((item) => [row.maintenanceProjectId].includes(item.maintenanceProjectId));
    const maintenanceProjectDeleteList = maintenanceProjectListFilter.map((item) => item.maintenanceProjectName);
    proxy.$modal.confirm('是否确认删除维保项目为"' + maintenanceProjectDeleteList + '"的数据项？').then(function () {
        return delMaintenanceProject(maintenanceProjectIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

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

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

/** 查询维保下拉树结构 */
function getTreeselect() {
    listMaintenance().then(response => {
        maintenanceList.value = response.data;
        maintenanceOptions.value = [];
        maintenanceOptions.value.push(...proxy.handleTree(response.data, "maintenanceId", "parentId"));
    });
}


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

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