<template>
    <div>
        <QueryListPage ref="queryListPage" :pager-config="pagerConfig" :query-form="queryForm" :query-list="queryList">
        </QueryListPage>
        <create-task ref='CreateTask' @reloadTable="reloadTable"></create-task>
    </div>
</template>

<script>
    import { listPublishMap, getProductList, putMapProductOnLine, putMapProductOffLine } from '@/api/jmap/mapdraft';
    import { getTaskList, postTask, postTaskCancel } from '@/api/management/task';
    import { launchFullscreen } from '@/utils/screen';
    import { getSkinStyleList } from '@/api/management/mapskin'
    import { UrlConfig } from '@/router/index';
    import localStore from 'storejs';
    import CreateTask from './createTask';

    export default {
        name: 'ManagementList',
        components: {
            CreateTask
        },
        data() {
            return {
                skinStyleList: [],
                taskList: [],
                taskStatusList: [],
                pagerConfig: {
                    pageSize: 'pageSize',
                    pageIndex: 'pageNum'
                },
                queryForm: {
                    labelWidth: '80px',
                    queryObject: {
                        status: {
                            type: 'select',
                            label: '状态',
                            config: {
                                data: []
                            }
                        }
                    }
                },
                queryList: {
                    query: getTaskList,
                    selectCheckShow: false,
                    indexShow: true,
                    columns: [
                        {
                            title: '皮肤类型',
                            prop: 'parameter',
                            type: 'tag',
                            columnValue: (row) => { return this.$convertField(row.parameter, this.skinStyleList, ['code', 'name']) },
                            tagType: (row) => { return '' }
                        },
                        {
                            title: '创建时间',
                            prop: 'createTime'
                        },
                        {
                            title: '状态',
                            prop: 'status',
                            type: 'tag',
                            columnValue: (row) => { return this.$convertField(row.status, this.taskStatusList, ['code', 'name']) },
                            tagType: (row) => { if (row.status != '03') { return 'warning' } else { return 'success' } }
                        },
                        {
                            title: '开始时间',
                            prop: 'startTime'
                        },
                        {
                            title: '完成时间',
                            prop: 'finishTime'
                        },
                        {
                            title: '创建结果',
                            prop: 'result',
                            width: '400px'
                        },
                        {
                            type: 'button',
                            title: '操作',
                            width: '250',
                            buttons: [
                                {
                                    name: '开始',
                                    handleClick: this.taskStart,
                                    type: '',
                                    showControl: (row) => { return row.status == '01' }
                                },
                                {
                                    name: '取消',
                                    handleClick: this.taskCancel,
                                    type: '',
                                    showControl: (row) => { return row.status == '04' }
                                },
                                {
                                    name: '重新执行',
                                    handleClick: this.taskStart,
                                    type: '',
                                    showControl: (row) => { return row.status == '03' || row.status == '05' }
                                },
                            ]
                        }
                    ],
                    actions: [
                        { text: '创建', btnCode: 'employee_insert', handler: this.createTask }
                    ]
                },

                currentModel: {}
            }
        },
        created() {
            this.loadInitData();
        },
        methods: {
            loadInitData() {
                this.skinStyleList = [];
                getSkinStyleList().then(response => {
                    this.skinStyleList = response.data;
                })

                this.taskStatusList = [];
                this.$Dictionary.taskStatus().then(list => {
                    this.taskStatusList = list;
                    list.forEach(elem => {
                        this.queryForm.queryObject.status.config.data.push({ value: elem.code, label: elem.name });
                    });
                })
            },
            taskStart(index, node) {
                this.$confirm('此操作将开始任务, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    postTask({ id: node.id }).then(res => {
                        this.$message.success('操作成功');
                        this.reloadTable();
                    }).catch(error => {
                        this.reloadTable();
                        this.$messageBox('操作失败');
                    })
                })
            },
            taskCancel(index, node) {
                this.$confirm('此操作将取消任务, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    postTaskCancel(node.id).then(res => {
                        this.$message.success('操作成功');
                        this.reloadTable();
                    }).catch(error => {
                        this.reloadTable();
                        this.$messageBox('操作失败');
                    })
                })
            },
            reloadTable() {
                this.queryList.reload()
            },
            createTask() {
                this.$refs.CreateTask.doShow();
            },
        }
    }
</script>