<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="维保项目" prop="maintenanceProjectId">
                <el-select v-model="queryParams.maintenanceProjectId" filterable placeholder="请选择维保项目"
                    @change="handleQuery" clearable>
                    <el-option v-for="item in maintenanceProjectList" :key="item.maintenanceProjectId"
                        :label="item.maintenanceProjectName" :value="item.maintenanceProjectId" />
                </el-select>
            </el-form-item>
            <el-form-item label="部门" prop="deptId">
                <el-tree-select v-model="queryParams.deptId" :data="deptOptions"
                    :props="{ value: 'deptId', label: 'deptName', children: 'children' }" value-key="deptId"
                    placeholder="选择部门" check-strictly @change="handleQuery" clearable />
                <!-- <el-input maxlength="100" v-model="queryParams.deptId" 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="['maintenanceProjectDept:maintenanceProjectDept:add']">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['maintenanceProjectDept:maintenanceProjectDept:edit']">修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['maintenanceProjectDept:maintenanceProjectDept:remove']">
                    删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['maintenanceProjectDept:maintenanceProjectDept:export']">
                    导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="maintenanceProjectDeptList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="维保项目部门关系id" align="center" prop="maintenanceProjectDeptId" />
            <el-table-column label="维保项目" align="center" prop="maintenanceProjectId">
                <template #default="scope">
                    <TextTag :options="maintenanceProjectList" column="maintenanceProjectId"
                        label="maintenanceProjectName" :value="scope.row.maintenanceProjectId" />
                </template>

            </el-table-column>
            <el-table-column label="部门" align="center" prop="deptId">
                <template #default="scope">
                    <TextTag :options="deptList" column="deptId" label="deptName" :value="scope.row.deptId" />
                </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 type="primary" link icon="View" @click="handleView(scope.row)"
                        v-hasPermi="['maintenanceProjectDept:maintenanceProjectDept:query']">详细
                    </el-button> -->
                    <el-button type="success" link icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['maintenanceProjectDept:maintenanceProjectDept:edit']">修改
                    </el-button>
                    <el-button type="danger" link icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['maintenanceProjectDept:maintenanceProjectDept: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="maintenanceProjectDeptRef" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="维保项目" prop="maintenanceProjectId">
                    <el-select v-model="form.maintenanceProjectId" filterable placeholder="请选择维保项目"
                        @change="handleQuery" clearable>
                        <el-option v-for="item in maintenanceProjectList" :key="item.maintenanceProjectId"
                            :label="item.maintenanceProjectName" :value="item.maintenanceProjectId" />
                    </el-select>
                    <!-- <el-input maxlength="100" v-model="form.maintenanceProjectId" placeholder="请输入维保项目" /> -->
                </el-form-item>
                <el-form-item label="部门" prop="deptId">
                    <el-tree-select v-model="form.deptId" :data="deptOptions"
                        :props="{ value: 'deptId', label: 'deptName', children: 'children' }" value-key="deptId"
                        placeholder="选择部门" check-strictly />
                </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="MaintenanceProjectDept">
import { listMaintenanceProjectDept, getMaintenanceProjectDept, delMaintenanceProjectDept, addMaintenanceProjectDept, updateMaintenanceProjectDept } from "@/api/maintenanceProjectDept/maintenanceProjectDept";
import { listMaintenanceProject } from "@/api/maintenanceProject/maintenanceProject";
import { listDept } from "@/api/system/dept";

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

const deptOptions = ref([]);


const deptList = 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,
        maintenanceProjectId: null,
        deptId: null
    },
    rules: {
        maintenanceProjectId: [
            {
                required: true, message: "维保项目不能为空", trigger: "blur"
            }
        ],
        deptId: [
            {
                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;
    listMaintenanceProjectDept(queryParams.value).then(response => {
        maintenanceProjectDeptList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

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

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

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

// 表单重置
function reset() {
    form.value = {
        maintenanceProjectDeptId: null,
        maintenanceProjectId: null,
        deptId: null
    }
        ;
    proxy.resetForm("maintenanceProjectDeptRef");
}

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

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

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

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

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

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

/** 删除按钮操作 */
function handleDelete(row) {
    const maintenanceProjectDeptIds = row.maintenanceProjectDeptId || ids.value;
    proxy.$modal.confirm('是否确认删除维保项目部门关系编号为"' + maintenanceProjectDeptIds + '"的数据项？').then(function () {
        return delMaintenanceProjectDept(maintenanceProjectDeptIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

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

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

function initialData() {
    listDept({
        pageNum: 1,
        pageSize: 9999,
    }).then(response => {
        deptList.value = response.data;
        deptOptions.value = proxy.handleTree(response.data, "deptId");
    });
    listMaintenanceProject({
        pageNum: 1,
        pageSize: 9999,
    }).then(response => {
        maintenanceProjectList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });

    getList();
}

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

</script>

