<template>
    <div class="page-container list-container">
        <!-- 功能按钮区 -->
        <div class="list-btns-container">
            <el-button type="primary" size="small" icon="Plus" @click="handleAdd"
                v-hasPermi="['business:project:add']">新增</el-button>
            <el-button type="success" size="small" icon="Edit" :disabled="single" @click="handleUpdate"
                v-hasPermi="['business:project:edit']">修改</el-button>
            <el-button type="danger" size="small" icon="Delete" :disabled="multiple" @click="handleDelete"
                v-hasPermi="['business:project:remove']">删除</el-button>
            <el-button type="warning" size="small" icon="Download" @click="handleExport"
                v-hasPermi="['business:project:export']">导出</el-button>
        </div>

        <!-- 搜索区 -->
        <el-form class="list-search-container" size="small" :model="queryParams" ref="queryRef" :inline="true">
            <el-form-item label="项目名称：" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入项目名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="开发模式：" prop="developType">
                <el-select v-model="queryParams.developType" placeholder="请选择开发模式" size="small" clearable
                    @change="handleQuery">
                    <el-option v-for="dict in develop_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-table v-loading="loading" :data="list" size="small" border height="100%"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="项目名称" align="center" prop="name" />
            <el-table-column label="项目金额(元)" align="center" prop="amount" />
            <el-table-column label="开发模式" align="center" prop="developType">
                <template #default="scope">
                    <dict-tag :options="develop_type" :value="scope.row.developType" />
                </template>
            </el-table-column>
            <el-table-column label="项目负责人" align="center" prop="leader" />
            <el-table-column label="开始日期" align="center" prop="startTime" />
            <el-table-column label="结束日期" align="center" prop="endTime" />
            <el-table-column label="纳期" align="center" prop="deadline" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['business:project:edit']">修改</el-button>
                    <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['business:project:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </div>
        <!-- 表单 -->
        <project-form ref="projectRef" :get-list="getList"></project-form>
    </div>
</template>

<script setup name="Project">
import projectForm from './form'
import { listProject, delProject } from "@/api/business/project";
const { proxy } = getCurrentInstance()
/** 字典数组区 */
const { develop_type } = proxy.useDict('develop_type')
/** 主页面 变量 */
const list = ref([])
const loading = ref(false)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
/** 查询 对象 */
const queryParams = ref({
    pageNum: 1,
    pageSize: 20,
    name: null,
    standard: null,
    num: null,
})

/***********************  列表页方法 ****************************/

/** 查询项目信息列表 */
function getList() {
    loading.value = true;
    listProject(queryParams.value).then(response => {
        list.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
    proxy.$refs.projectRef.open()
}

/** 修改按钮操作 */
function handleUpdate(row) {
    const id = row.id || ids.value
    console.log(id)
    proxy.$refs.projectRef.open(id)
}


/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除选中的数据项？').then(function () {
        return delProject(_ids);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功！");
    }).catch(() => { });
}

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

getList()

</script>
<style scope></style>
