<template>
    <div class="page-container list-container">
        <!-- 功能按钮区 -->
        <div class="list-btns-container">
            <el-button type="success" size="small" @click="handleAgree">批量同意</el-button>
            <el-button type="danger" size="small" @click="handleRefuse">批量拒绝</el-button>
            <el-button type="warning" size="small" @click="handleExport"
                v-hasPermi="['business:examine:export']">导出</el-button>
        </div>
        <!-- 搜索区 -->
        <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
            <el-form-item label="状态：">
                <el-select size="small" v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 150px">
                    <el-option label="待确认" :value="0"></el-option>
                    <el-option label="已同意" :value="1"></el-option>
                    <el-option label="已拒绝" :value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="公司名称：">
                <el-input size="small" v-model="queryParams.companyName" placeholder="请输入公司名称" style="width: 150px"
                    clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="所属期：">
                <el-date-picker v-model="queryParams.currentPeriod" style="width: 150px" type="month" format="YYYY年MM月"
                    value-format="YYYY-MM" size="small" />
            </el-form-item>
            <el-form-item label="委托来源：" prop="entrustComeName">
                <el-input size="small" v-model="queryParams.entrustComeName" style="width: 150px" placeholder="请输入委托来源"
                    clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item prop="dateFilter" label="到账时间：">
                <el-date-picker v-model="queryParams.dateFilter" type="daterange" clearable size="small"
                    format="YYYY - MM - DD " value-format="YYYY-MM-DD" range-separator="至" start-placeholder="业务日期"
                    end-placeholder="业务日期" style="width: 100%;"></el-date-picker>
            </el-form-item>
            <el-form-item style="margin-left: 40px;">
                <el-button type="primary" icon="Search" @click="handleQuery" size="small">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery" size="small">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 列表区 -->
        <el-table v-loading="loading" :data="examineList" size="small" border height="100%"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="客户名称" align="center" prop="companyName" min-width="140px" />
            <el-table-column label="税号" align="center" prop="socialCreditCode" />
            <el-table-column label="项目" align="center" prop="taskName" />
            <el-table-column label="所属期" align="center" prop="currentPeriod">
                <template #default="scope">
                    {{ scope.row.currentPeriod.substr(0, 7) }}
                </template>
            </el-table-column>

            <el-table-column label="提交时间" align="center" prop="createTime" />
            <el-table-column label="委托来源" align="center" prop="fromTenant" />
            <el-table-column label="取消理由" align="center" prop="cancelReason" />
            <el-table-column label="状态" align="center" prop="status">
                <template #default="scope">
                    <div v-if="scope.row.status === 0" type="danger">待确认</div>
                    <div v-else-if="scope.row.status === 1" type="success">已同意</div>
                    <div v-else type="info">已拒绝</div>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="warning" size="small" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['business:examine:edit']" v-if="scope.row.status == 0">同意</el-button>
                    <el-button link type="danger" size="small" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['business:examine:remove']" v-if="scope.row.status == 0">拒绝</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" />

        <!-- 表单 -->
        <examine-form ref="examineRef" :get-list="getList"></examine-form>
        <el-dialog title="拒绝取消" v-model="cancelEntrust" width="720px" append-to-body size="small" draggable
            :close-on-click-modal="false">
            <el-form-item label="拒绝理由" prop="refuseReason" style="margin-top: 10px">
                <el-input v-model="form.refuseReason" type="textarea" placeholder="请输入取消理由" />
            </el-form-item>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" icon="Finished" size="small" @click="submitForm">确 定</el-button>
                    <el-button icon="close" size="small" @click="cancelEntrust = false">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Examine">
    import { listExamine, delExamine, refuse, agree, refuseIds } from "@/api/business/examine";
    import {
        okDelEntrust,
    } from "@/api/business/entrust/currentWorkOrder";
    import {
        okNextDelEntrust,
    } from "@/api/business/entrust/nextWorkOrder";
    const { proxy } = getCurrentInstance()
    /** 字典数组区 */
    /** 查询 对象 */

    const examineList = ref([]);
    const loading = ref(true);
    const ids = ref([])
    const single = ref(true);
    const cancelEntrust = ref(false);
    const multiple = ref(true);
    const total = ref(0);
    /** 查询对象 */
    const queryParams = ref({
        pageNum: 1,
        pageSize: 10,
        currentPeriod: null,
        entrustCome: null,
        cancelReason: null,
        status: null,
    })
    const form = ref({});
    /***********************  方法区  ****************************/

    /** 查询企业租户列表 */
    function getList() {
        loading.value = true;
        listExamine(queryParams.value).then(response => {
            examineList.value = response.rows;
            total.value = response.total;
            loading.value = false;
        });
    }

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

    /** 重置按钮操作 */
    function resetQuery() {
        proxy.resetForm("queryRef");
        handleQuery();
    }
    function handleAgree() {
        console.log(ids.value);
        if (ids.value.length == 0) {
            proxy.$modal.msgError("请选择要同意的数据");
            return;
        }
        proxy.$modal
            .confirm("确定同意么?")
            .then((_) => {
                agree(ids.value).then((res) => {
                    proxy.$modal.msgSuccess("同意成功");
                    getList();
                });
            })
            .catch((_) => { });
    }
    function handleRefuse() {
        if (ids.value.length == 0) {
            proxy.$modal.msgError("请选择要拒绝的数据");
            return;
        }

        cancelEntrust.value = true;

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

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

    /** 修改按钮操作 */
    function handleUpdate(row) {
        proxy.$modal
            .confirm("确定同意么?")
            .then((_) => {
                const entrust = {
                    workMonth: row.currentPeriod,
                    workOrderIds: [row.workOrderId],
                    examineId: row.id
                };
                if (row.nextFlag == 1) {
                    okNextDelEntrust(entrust).then((res) => {
                        proxy.$modal.msgSuccess("操作成功");
                        getList();
                    });
                } else {
                    okDelEntrust(entrust).then((res) => {
                        proxy.$modal.msgSuccess("操作成功");
                        getList();
                    });
                }

            })
            .catch((_) => { });
    }
    function submitForm() {
        if (form.value.refuseReason == null) {
            proxy.$modal.msgError("请填写拒绝理由");
            return;
        }
        if (form.value.id == undefined || form.value.id == null) {
            let data ={
                ids:ids.value,
                reason:form.value.refuseReason
            }
            refuseIds(data).then((res) => {
                proxy.$modal.msgSuccess("操作成功");
                cancelEntrust.value = false;
                getList();
            })
        } else {
            refuse(form.value).then((res) => {
                proxy.$modal.msgSuccess("操作成功");
                getList();
                cancelEntrust.value = false;
            });
        }

    }

    /** 删除按钮操作 */
    function handleDelete(row) {
        cancelEntrust.value = true;
        form.value.id = row.id
        console.log(form.value);

    }

    /** 导出按钮操作 */
    function handleExport() {
            console.log(queryParams.value);
            
        
        proxy.download('business/examine/export', {
            ...queryParams.value
        }, `取消委托列表导出_${new Date().getTime()}.xlsx`)
    }

    getList();
</script>