<template>
    <div class="existingPage" :class="!theme ? 'themeDark' : 'themeLight'">
        <p class="tableTit">已有脚本</p>
        <el-table :data="tableData2" highlight-current-row :height="height" @current-change="handleCurrentChange"
            ref="singleTableRef">
            <el-table-column label="脚本名称">
                <template #default="scope">
                    <el-button type="warning" round>{{ scope.row.scriptName}}</el-button>
                </template>
            </el-table-column>
            <el-table-column align="right">
                <template #default="scope">
                    <el-button type="primary" @click="openDetil(scope.row)">编辑</el-button>
                    <el-button style="margin-right:10px" type="danger" @click="deleteRow(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
    <el-dialog title="详情" custom-class="windLifeDialog" v-model="dialogVisible" width="1000px"
        :close-on-click-modal="false">
        <div class="periodFrom">
            <div style="margin-bottom: 12px">
                <el-input v-model="scriptname" style="width: 500px">
                    <template #prepend>输入脚本名称：</template>
                </el-input>
                <el-button type="primary" @click="saveJson" style="margin-left: 10px">保存脚本</el-button>
            </div>
            <el-table :data="editTable" :height="listHeight">
                <el-table-column type="index" width="30" />
                <el-table-column label="脚本步骤">
                    <template #default="scope">
                        <el-input v-model="scope.row.beforeText" v-if="scope.row.before && scope.row.before !==''"
                            style="width: 419px">
                            <template #prepend>{{ scope.row.before }}</template>
                        </el-input>
                        <el-input v-model="scope.row.behindText" v-if="scope.row.behind && scope.row.behind !==''"
                            style="width: 250px">
                            <template #prepend>{{ scope.row.behind }}</template>
                        </el-input>
                        <el-button v-if="scope.row.scriptName !==''" type="warning" round>{{ scope.row.scriptName}}
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column align="right" width="80">
                    <template #default="scope">
                        <el-button type="danger" @click="handleDelete(scope.$index)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </el-dialog>
</template>

<script>
    import {
        read,
        save,
        updateScript,
        deleteScript
    } from "@/api/report";
    export default {
        props: {
            height: {
                type: String,
                default: () => {
                    return '';
                },
            },
            theme: {
                type: String,
                default: () => {
                    return '';
                },
            },
        },
        data() {
            return {
                listHeight: document.documentElement.clientHeight - 220,
                tableData2: [],
                childCurrentRow: {},
                dialogVisible: false,
                scriptname: '',
                editTable: []
            }
        },
        mounted() {
            this.getData()
        },
        methods: {
            getData() {
                read().then(res => {
                    console.log("res:", res.data)
                    this.$emit("getExistingData", res.data)
                    this.tableData2 = res.data
                });
            },
            handleCurrentChange(val) {
                this.$emit("getExistingData", val)
                this.childCurrentRow = val;
                //清除父组件的行选择
                this.tableRow()
                this.setTableRow(val)
            },
            setTableRow(row) {
                this.$refs.singleTableRef.setCurrentRow(row)
            },
            tableRow() {

            },
            handleDelete(index) {
                this.editTable.splice(index, 1)
            },
            openDetil(row) {
                this.scriptObj = row
                this.dialogVisible = true
                this.scriptname = row.scriptName
                this.editTable = row.scripts
            },
            deleteRow(row) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let that = this
                    let params = {
                        id: row.id
                    }
                    deleteScript(params).then(res => {
                        if (res.code === 0) {
                            that.$message({
                                message: res.msg,
                                type: 'success'
                            });
                            that.getData()
                        }
                    });
                })
            },
            saveJson() {
                let that = this
                let params = {
                    id: that.scriptObj.id,
                    scriptName: that.scriptname,
                    scripts: that.editTable
                }
                updateScript(params).then(res => {
                    if (res.code === 0) {
                        that.$message({
                            message: res.msg,
                            type: 'success'
                        });
                        that.dialogVisible = false
                        that.getData()
                    }
                });
            },
        }
    }
</script>



<style scoped lang="less">
    .existingPage {

        .tableTit {
            line-height: 40px;
            margin-left: 10px;
        }
    }

    .themeDark {
        .tableTit {
            font-size: 16px;
            color: #f8f9ff;
        }
    }

    .themeLight {
        background: #f8f9ff;

        .tableTit {
            font-size: 16px;
            color: #9093a6;
        }
    }

    .el-overlay {
        .startToDia {
            .el-dialog__body {
                padding: 30px 60px 30px 20px !important;



                .periodFrom {

                    .el-select,
                    .el-input {
                        width: 100%;
                    }

                    .el-input {
                        height: 30px;
                    }
                }

                .flowSty {
                    width: 100%;
                    height: 70vh;
                }
            }
        }
    }
</style>