<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="工单编号" prop="applyNo">
                <el-input v-model="queryParams.applyNo" placeholder="请输入流程名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="类型" prop="applyType">
                <el-select v-model="queryParams.applyType" placeholder="请选择流程状态" clearable @change="handleQuery">
                    <el-option v-for="dict in apply_type" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="申请人" prop="applyEstablishUser">
                <el-select v-model="queryParams.applyEstablishUser" placeholder="请选择申请人" clearable filterable
                    @change="handleQuery">
                    <el-option :label="`${$store.state.user.user.nickName}(我)`"
                        :value="$store.state.user.user.userId" />
                    <template v-for="dict in userList" :key="dict.userId">
                        <el-option :label="dict.nickName" :value="dict.userId"
                            v-if="$store.state.user.user.userId != dict.userId" />
                    </template>
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="流程状态" prop="processState">
                <el-select v-model="queryParams.processState" placeholder="请选择流程状态" clearable @change="handleQuery">
                    <el-option v-for="dict in apply_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-card shadow="never">
            <el-table ref="tableRef" v-loading="loading" :data="applyList" :default-sort="defaultSort"
                @sort-change="handleSortChange">
                <el-table-column label="序号" align="center" type="index" width="50" />
                <el-table-column label="工单编号" align="center" prop="applyNo" width="300px" />
                <el-table-column label="类型" align="center" prop="applyType">
                    <template #default="scope">
                        <dict-tag :options="apply_type" :value="scope.row.applyType" />
                    </template>
                </el-table-column>
                <el-table-column label="当前环节" align="center" prop="applyLink" />
                <el-table-column label="申请人" align="center" prop="userNickName" />
                <el-table-column label="开始时间" align="center" prop="applyEstablishTime" width="180" sortable="custom"
                    :sort-orders="['descending', 'ascending']">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.applyEstablishTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="状态" align="center" prop="applyState">
                <template #default="scope">
                    <dict-tag :options="com_apply_state" :value="scope.row.applyState" />
                </template>
            </el-table-column> -->
                <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-button text type="primary" icon="View" @click="handleView(scope.row)"
                            v-hasPermi="['apply:apply:query']">详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <ApplyDetailDrawer ref="applyDetailDrawerRef" :applyId="applyId" @update="getList" />

    </div>

</template>

<script setup>
import {
    listApply,
} from "@/api/workbench/apply";
import { listUser } from "@/api/system/user";

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


const applyList = ref([]);
const userList = ref([]);

const loading = ref(true);
const showSearch = ref(true);
const applyId = ref(null);
const total = ref(0);
const defaultSort = ref({ prop: "applyEstablishTime", order: "descending" });

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        applyState: null,
        applyLink: null,
        applyType: null,
        applyEstablishUser: null,
        applyEstablishTime: null,
        applyCompleteTime: null
    },
    rules: {},
});
const { queryParams, form, rules } = toRefs(data);

function getList() {
    loading.value = true;
    queryParams.value.applyType == '' ? queryParams.value.applyType = null : null
    listApply(queryParams.value).then((response) => {
        applyList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

function handleView(row) {
    applyId.value = row.applyId;
    proxy.$refs["applyDetailDrawerRef"].show();

}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryForm");
    queryParams.value.pageNum = 1;
    proxy.$refs["tableRef"].sort(defaultSort.value.prop, defaultSort.value.order);
    // handleQuery();
}

/** 排序触发事件 */
function handleSortChange(column, prop, order) {
    queryParams.value.orderByColumn = column.prop;
    queryParams.value.isAsc = column.order;
    getList();
}

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

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

</script>
