<template>
    <div class="list-view">
        <div class="filters-container">
            <el-input placeholder="输入关键字" v-model="search" clearable class="filter-item"></el-input>
            <el-button @click="initData" type="primary" icon="el-icon-search" class="filter-item">搜索 </el-button>
        </div>
        <el-table
            :data="tableData"
            row-key="id"
            ref="table"
            header-row-class-name="table-header-row"
            header-cell-class-name="table-header-cell"
            row-class-name="table-row"
            cell-class-name="table-cell"
            v-loading="fetchingData"
        >
            <el-table-column prop="jobDetailName" label="任务名称"> </el-table-column>
            <el-table-column prop="jobCronExpression" label="cron表达式"> </el-table-column>
            <el-table-column prop="groupName" label="分组名称" ></el-table-column>
            <el-table-column prop="description" label="描述" ></el-table-column>
            <el-table-column prop="status" label="状态" show-overflow-tooltip> 
                <template v-slot="{row}">
                    <el-tag v-if="row.status == 'PAUSED'"  type="danger">{{row.status}}</el-tag>
                    <el-tag v-else   type="">{{row.status}}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" width="340">
                <template v-slot="{ row }">
                    <el-button @click="operation(row, 'stop')" type="danger" size="mini" plain  :disabled="opLoading">停止</el-button>
                    <el-button @click="operation(row, 'start')" type="primary" size="mini" plain :disabled="opLoading">启动</el-button>
                    <el-button @click="operation(row, 'call')" type="warning" size="mini" plain :disabled="opLoading">调用</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-wrapper">
            <el-pagination
                background
                @size-change="onSizeChange"
                @current-change="onCurrentChange"
                :current-page="page"
                :page-sizes="[10, 20, 30, 40, 50]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalElements"
            >
            </el-pagination>
        </div>

        
    </div>
</template>
<script>
import pageableTable from '@/mixins/pageableTable';

export default {
    name: 'JobList',
    mixins: [pageableTable],
    data() {       

        return {
            multipleMode: false,
            search: '',
            url: '/quartz/allJobs',
            opLoading: false,
            sortStr: 'createdAt,desc',
        };
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        beforeGetData() {
            return { search: this.search, sort: 'createdAt,desc' };
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        operation(row, op){
            let url = null;
            if(op == "start"){
                url = "/quartz/resumeJob";
            }else if(op == "stop"){
                url = "/quartz/pauseJob";
            }else if(op == "call"){
                url = "/quartz/triggerJob";
            }else{
                this.$message.error("不支持的操作：" + op);
            }
            this.opLoading = true;
            this.$http.post(url, row, {body:"json"}).then(data => {
                this.$message.success("操作成功");
                this.initData();
            }).catch(e => {
                this.$message.error(e.error || e.message);
            }).finally(() => {
                this.opLoading = false;
            });

        }
    }
};
</script>

