<template>
    <ap-container>
        <ap-aside margin="1111" width="250px">
            <ap-aside-tree :options.sync="treeOptions" class="type-tree">
                <el-form slot="form" ref="tSysTaskType" :inline="true" label-width="80px"
                         :model="tSysTaskTypeForm" :rules="tSysTaskTypeRules">
                    <el-form-item label="父节点">
                        <el-select v-model="tSysTaskTypeForm.fdParentId" filterable placeholder="请选择父节点">
                            <el-option label="无" value=""></el-option>
                            <el-option
                                v-for="item in taskTypeList"
                                :key="item.fdId"
                                :label="item.fdName"
                                :value="item.fdId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label='名称' prop='fdName'>
                        <el-input v-model='tSysTaskTypeForm.fdName'></el-input>
                    </el-form-item>
                    <el-form-item label='排序' prop='fdSort'>
                        <el-input v-model='tSysTaskTypeForm.fdSort'></el-input>
                    </el-form-item>
                </el-form>
                <span slot="tree" slot-scope="scope" style="font-size: 14px">
                    {{ scope.data.fdName }}
                </span>
            </ap-aside-tree>
        </ap-aside>
        <ap-split-panel></ap-split-panel>
        <ap-container>
            <ap-header margin="1110">
                <el-form ref="tSysTaskLogSearchForm" :model="tSysTaskSearchForm"
                         :inline="true" class="layout-header-form" @submit.native.prevent="searchEvent">
                    <el-form-item label="搜索:" class="layout-header-form-item" prop="likeStr">
                        <el-input v-model="tSysTaskSearchForm.likeStr" placeholder="名称/路径/负责人/备注"></el-input>
                    </el-form-item>
                </el-form>
                <el-button type="primary" @click="searchEvent">查询</el-button>
            </ap-header>
            <ap-main margin="0110">
                <ap-table ref="tSysTaskTableRef" :options.sync="tSysTaskTable">
                    <template #fdPath="slotProps">
                        <template v-if="!slotProps.rowData.isEdit && !slotProps.rowData.isAdd">
                            {{ slotProps.rowData.fdPath }}
                        </template>
                        <template v-else-if="slotProps.rowData.fdType == 1">
                            <el-input v-model='slotProps.rowData.fdPath' @change="rowChange(slotProps.rowData)"></el-input>
                        </template>
                        <template v-else-if="slotProps.rowData.fdType == 2">
                            <el-input placeholder="请选择文件" v-model="slotProps.rowData.fdPath">
                                <template slot="append">
                                    <ap-upload ref="uploadRef" :options="uploadOpt">
                                        <el-button type="primary" @click="uploadClick(slotProps.rowData)">选择</el-button>
                                    </ap-upload>
                                </template>
                            </el-input>
                        </template>
                    </template>
                </ap-table>
            </ap-main>
        </ap-container>

        <el-dialog :title="taskParameterDialogTitle" :visible.sync="taskParameterDialogVisible" width="800px"
                   @opened="taskParameterDialogOpenedEvent">
            <ap-table :options.sync="taskParameterTableOpt" ref="taskParameterTableRef"
                      style="height: 400px"></ap-table>
        </el-dialog>
    </ap-container>
</template>

<script>
import {downloadFile} from 'ap-util/util/FileServiceUtil'

export default {
    name: 'SysTask',
    data() {
        let tSysTaskCols = [
            {
                prop: 'fdName',
                label: '名称',
                minWidth: "150",
                type: 'input'
            }, {
                prop: 'fdTypeId',
                label: '任务类型',
                type: 'select',
                width: "130",
                selectOptionsRemote: 'typeList',
                selectOptionsKey: {label: "fdName", value: "fdId"},
                addDefaultValue: () => {
                    return this.selectedId;
                }
            }, {
                prop: 'fdType',
                label: '接口类型',
                type: 'select',
                width: "80px",
                addDefaultValue: 1,
                selectOptions: [
                    {label: "Java接口", value: 1},
                    {label: "kettle", value: 2}
                ],
            }, {
                prop: 'fdPath',
                label: '路径',
                minWidth: "300px",
                type: 'slot',
                slot: "fdPath",
                addDefaultValue: "",
            }, {
                prop: 'fdPersonName',
                label: '负责人',
                align: 'center',
                width: "90",
                type: "searchTable",
                searchTableTitle: "负责人",
                searchTableParamPlaceholder: "输入姓名，回车搜索",
                searchTableShowParam: true,
                searchTableWidth: "800px",
                searchTableOpt: {
                    columns: [{prop: "fdDeptName", label: "部门", type: "input"},
                        {prop: "fdNo", label: "工号", type: "input"},
                        {prop: "fdName", label: "姓名", type: "input"}],
                    dataUrl: "/apm/sys/person/getSimplifyPersonListByName",
                    method: "POST",
                    multiSelect: false,
                    initData: false,
                },
                searchTableConfirm: (row, selectedRow) => {
                    if (selectedRow.length < 1) {
                        row.fdPersonId = "";
                        row.fdPersonName = "";
                    } else {
                        row.fdPersonId = selectedRow[0].fdId;
                        row.fdPersonName = selectedRow[0].fdName;
                    }
                    return true;
                },
                searchTableClear: (row) => {
                    row.fdPersonId = "";
                    row.fdPersonName = "";
                    return true;
                },
            }, {
                prop: 'fdComment',
                label: '备注',
                minWidth: "150px",
                type: 'input'
            }, {
                prop: 'fdSort',
                label: '排序',
                width: "50px",
                type: 'input',
                align: 'center',
            }, {
                prop: 'fdDisabled',
                label: '是否有效',
                type: 'select',
                align: "center",
                width: "80px",
                addDefaultValue: 0,
                selectOptions: [
                    {label: "有效", value: 0},
                    {label: "无效", value: 1}
                ],
                html: function (value, row) {
                    if (value == 0) {
                        return '<div class="ap-tag-success">有效</div>'
                    } else if (value == 1) {
                        return '<div class="ap-tag-danger">无效</el-tag>';
                    }
                }
            }
        ];
        let tSysTaskTable = {
            title: "调度任务",
            columns: tSysTaskCols,
            dataUrl: "/apt/sys/TSysTask/getTSysTaskGridList",
            saveUrl: "/apt/sys/TSysTask/saveTSysTaskGridData",
            editPk: "fdId", //默认fdId
            deletePk: "fdId", // 默认fdId
            deleteUrl: "/apt/sys/TSysTask/deleteTSysTaskGridData",
            sortUrl: "/apt/sys/TSysTask/updateTSysTaskSortList",
            initData: true,
            toolbarBtn: [
                {
                    btnType: "primary",
                    text: "下载ktr",
                    icon: "el-icon-download",
                    onClick: () => {
                        this.downloadKtr();
                    }
                }, "separator",
                {
                    btnType: "primary",
                    text: "任务参数",
                    icon: "el-icon-menu",
                    onClick: () => {
                        this.openTaskParameterDialog();
                    }
                }, "separator", "sortAuto", "sortDown", "sortUp", "separator",
                {
                    btnType: "primary",
                    text: "复制",
                    icon: "el-icon-copy",
                    onClick: this.copyTask
                },
                "separator", "add", "edit", "del", "refresh", "save"
            ],
            params: () => {
                return {
                    typeIds: this.treeSelectedValues,
                    likeStr: this.tSysTaskSearchForm.likeStr
                }
            }
        };

        let taskParameterCols = [
            {
                prop: 'fdKey',
                label: '参数名',
                type: 'input'
            }, {
                prop: 'fdValue',
                label: '参数值',
                type: 'input'
            }, {
                prop: 'fdComment',
                label: '备注',
                type: 'input'
            },
        ];
        let taskParameterTableOpt = {
            columns: taskParameterCols,
            editPk: "fdId", //默认fdId
            deletePk: "fdId", // 默认fdId
            dataUrl: "/apt/sys/TSysTaskParameter/getTSysTaskParameterGridList",
            saveUrl: "/apt/sys/TSysTaskParameter/saveTSysTaskParameterGridData",
            deleteUrl: "/apt/sys/TSysTaskParameter/deleteTSysTaskParameterGridData",
            toolbarBtn: ["add", "edit", "del", "cancel", "refresh", "save"],
            initData: false, // 默认false
            addDefaultObj: () => {
                return {
                    fdTaskId: this.getSelectedRow().fdId
                }
            },
            params: () => {
                return {
                    taskId: this.getSelectedRow().fdId
                }
            }
        };
        return {
            // ----- 左侧树 -----
            treeOptions: this.getTreeOption(),
            tSysTaskTypeForm: {},
            tSysTaskTypeRules: {},
            treeSelectedValues: "",
            selectedId: "",
            taskTypeList: [],
            // ----- 右侧 -----
            tSysTaskSearchForm: {
                likeStr: ""
            },
            tSysTaskTable: tSysTaskTable,
            // 功能权限dialog
            taskParameterDialogTitle: "",
            taskParameterDialogVisible: false,
            taskParameterTableOpt: taskParameterTableOpt,
            dialogInfo: {
                title: "",
                isAdd: false,
                isVisible: false,
                saveBtnIsLoading: false,
                formIsLoading: false
            },
            currentRow: {},
            uploadOpt: {
                data: () => {
                    return {
                        filePath: '/apm/kettle/',      // 服务器保存的路径
                    }
                },
                onSuccess: (response, file, fileList) => {
                    console.log("上传文件地址：" + response.data)
                    this.currentRow.fdPath = response.data;
                    this.currentRow.changeArr.push("fdPath")
                },
                onError: (response, file, fileList) => {
                    this.$message.success('上传失败！')
                }
            }
        }
    },
    methods: {
        // 左侧树：配置
        getTreeOption() {
            let treeOptions = {
                title: "任务类型",
                toolbarBtn: ["add", "edit", "del", "sort"],
                dialogTitle: "任务类型",
                showCheckbox: true,
                sortByDrag: true,
                onCheck: (data, node, comp, halfCheckedKeys, checkedKeys) => {
                    this.selectedId = data.fdId;
                    this.treeSelectedValues = halfCheckedKeys.concat(checkedKeys).join(",");
                    if (this.treeSelectedValues == null || this.treeSelectedValues == "") {
                        return;
                    }
                    this.$refs.tSysTaskTableRef.refresh();
                },
                loadOptions: {
                    url: "/apt/sys/TSysTaskType/getTSysTaskTypeList",
                    treeKey: {
                        idKey: "fdId",
                        // parentKey: "fdParentId",
                        // sortKey: "fdSort",
                        childrenKey: "children",
                        label: "fdName"
                    },
                    success: (response) => {
                        this.taskTypeList = response.data;
                    },
                },
                editOptions: {
                    url: "/apt/sys/TSysTaskType/getTSysTaskTypeByFdId",
                    data: (node) => {
                        return {
                            fdId: node.fdId
                        }
                    },
                    success: (response) => {
                        this.tSysTaskTypeForm = response.data;
                    }
                },
                insertUrl: "/apt/sys/TSysTaskType/insertTSysTaskType",
                insertData: () => {
                    return this.tSysTaskTypeForm
                },
                updateUrl: "/apt/sys/TSysTaskType/updateTSysTaskType",
                updateData: () => {
                    return this.tSysTaskTypeForm
                },
                deleteUrl: "/apt/sys/TSysTaskType/deleteTSysTaskTypeByFdId",
                deleteData: (node) => {
                    return {
                        fdId: node.fdId
                    }
                },
                sortByTree: true,
                updateSortUrl: '/apt/sys/TSysTaskType/updateTSysTaskTypeSort',
                updateSortListUrl: '/apt/sys/TSysTaskType/updateTSysTaskTypeSortList',
                resetForm: () => {
                    this.tSysTaskTypeForm = {
                        fdParentId: '',
                        fdName: '',
                        fdSort: '',
                    };
                },
                afterOpenAddDialog: (node) => {
                    this.tSysTaskTypeForm.fdParentId = node.fdId;
                },
            };

            return treeOptions;
        },
        // 右侧
        getSelectedRow() {
            let selectedRow = this.$refs.tSysTaskTableRef.getSelection();
            if (selectedRow.length == 0) {
                this.$message.error(`请选择需要操作的行！`);
                return null;
            } else if (selectedRow.length > 1) {
                this.$message.error(`只能选择一行！`);
                return null;
            }
            return selectedRow[0];
        },
        searchEvent() {
            this.$refs.tSysTaskTableRef.refresh();
        },
        openTaskParameterDialog() {
            let selectedRow = this.getSelectedRow();
            if (selectedRow == null) {
                return
            }
            this.taskParameterDialogTitle = "调度任务参数 -【" + selectedRow.fdName + "】";
            this.taskParameterDialogVisible = true;
        },
        downloadKtr() {
            let selectedRows = this.$refs.tSysTaskTableRef.getSelection();
            if (selectedRows.length < 1) {
                this.$message.error("请选择需要下载的任务！");
                return;
            }
            if (selectedRows.length > 1) {
                this.$message.error("只能选择单个任务下载！");
                return;
            }

            let row = selectedRows[0];
            if (row.fdType != 2) {
                this.$message.error("只能kettle任务文件！");
                return;
            }
            downloadFile(row.fdPath);
        },
        taskParameterDialogOpenedEvent() {
            this.$refs.taskParameterTableRef.refresh();
        },
        copyTask() {
            let selectedRows = this.$refs.tSysTaskTableRef.getSelection();
            if (selectedRows.length < 1) {
                this.$message.error("请选择需要编辑的行！");
                return;
            }
            if (selectedRows.length > 1) {
                this.$message.error("只能选择单行数据进行编辑！");
                return;
            }
            this.$request({
                url: '/apt/sys/TSysTask/copyTask',
                method: 'post',
                data: {
                    fdId: selectedRows[0].fdId
                }
            }).then(response => {
                this.$message.success(`复制成功！`)
                this.$refs.tSysTaskTableRef.refresh();
            })
        },
        rowChange(row) {
            row.changeArr.push("fdPath")
        },
        uploadClick(row) {
            this.currentRow = row;
        }
    }
}
</script>

<style scoped>
.type-tree /deep/ .el-tree-node__content {
    height: 22px;
}
</style>
