<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="环节id" prop="linkId">
                <el-input v-model="queryParams.linkId" placeholder="请输入环节id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="申请单id" prop="applyId">
                <el-input v-model="queryParams.applyId" placeholder="请输入申请单id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="操作意见" prop="jobOpinion">
                <el-input v-model="queryParams.jobOpinion" placeholder="请输入操作意见" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="操作时间" prop="jobTime">
                <el-date-picker clearable v-model="queryParams.jobTime" type="date" value-format="YYYY-MM-DD"
                    placeholder="请选择操作时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="环节名称" prop="jobName">
                <el-input v-model="queryParams.jobName" 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">
                <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['wfJob:wfJob:add']">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['wfJob:wfJob:edit']">修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['wfJob:wfJob:remove']">删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['wfJob:wfJob:export']">导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="wfJobList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="审批流程编号" align="center" prop="jobId" />
            <el-table-column label="环节id" align="center" prop="linkId" />
            <el-table-column label="申请单id" align="center" prop="applyId" />
            <el-table-column label="操作人" align="center" prop="jobUser" />
            <el-table-column label="操作意见" align="center" prop="jobOpinion" />
            <el-table-column label="操作时间" align="center" prop="jobTime" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.jobTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="完成状态" align="center" prop="jobState">
                <template #default="scope">
                    <dict-tag :options="com_apply_state" :value="scope.row.jobState" />
                </template>
            </el-table-column>
            <el-table-column label="是否撤回" align="center" prop="jobWithdraw" />
            <el-table-column label="环节操作人类型" align="center" prop="type" />
            <el-table-column label="部门编号" align="center" prop="deptId" />
            <el-table-column label="是否只阅不批" align="center" prop="isZybp" />
            <el-table-column label="环节名称" align="center" prop="jobName" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
                <template #default="scope">
                    <el-button text type="primary" icon="View" @click="handleView(scope.row)"
                        v-hasPermi="['wfJob:wfJob:query']">详细
                    </el-button>
                    <el-button text type="success" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['wfJob:wfJob:edit']">修改
                    </el-button>
                    <el-button text type="danger" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['wfJob:wfJob: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="wfJobRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="环节id" prop="linkId">
                    <el-input v-model="form.linkId" placeholder="请输入环节id" />
                </el-form-item>
                <el-form-item label="申请单id" prop="applyId">
                    <el-input v-model="form.applyId" placeholder="请输入申请单id" />
                </el-form-item>
                <el-form-item label="操作意见" prop="jobOpinion">
                    <el-input v-model="form.jobOpinion" placeholder="请输入操作意见" />
                </el-form-item>
                <el-form-item label="操作时间" prop="jobTime">
                    <el-date-picker clearable v-model="form.jobTime" type="date" value-format="YYYY-MM-DD"
                        placeholder="请选择操作时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="环节名称" prop="jobName">
                    <el-input v-model="form.jobName" 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="WfJob">
import { listWfJob, getWfJob, delWfJob, addWfJob, updateWfJob } from "@/api/wfJob/wfJob";

const { proxy } = getCurrentInstance();
const { com_apply_state } = proxy.useDict('com_apply_state');

const wfJobList = 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,
        linkId: null,
        applyId: null,
        jobUser: null,
        jobOpinion: null,
        jobTime: null,
        jobState: null,
        jobWithdraw: null,
        type: null,
        deptId: null,
        isZybp: null,
        jobName: null
    },
    rules: {
        linkId: [
            {
                required: true, message: "环节id不能为空", trigger: "blur"
            }
        ],
        applyId: [
            {
                required: true, message: "申请单id不能为空", trigger: "blur"
            }
        ],
    }
})
    ;

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

/** 查询流程待办列表 */
function getList() {
    loading.value = true;
    listWfJob(queryParams.value).then(response => {
        wfJobList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

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

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

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

// 表单重置
function reset() {
    form.value = {
        jobId: null,
        linkId: null,
        applyId: null,
        jobUser: null,
        jobOpinion: null,
        jobTime: null,
        jobState: null,
        jobWithdraw: null,
        type: null,
        deptId: null,
        isZybp: null,
        jobName: null
    }
        ;
    proxy.resetForm("wfJobRef");
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加流程待办";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const jobId =
        row.jobId || ids.value
    getWfJob(jobId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改流程待办";
    });
}

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

/** 删除按钮操作 */
function handleDelete(row) {
    const jobIds = row.jobId || ids.value;
    proxy.$modal.confirm('是否确认删除流程待办编号为"' + jobIds + '"的数据项？').then(function () {
        return delWfJob(jobIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

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

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

getList();
</script>

