<template>
    <div class="app-container">

        <el-row :gutter="10" class="mb8">
            <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
                <el-form-item label="期数名称" prop="period">
                    <el-input v-model="queryParams.period" placeholder="请输入期数名称" clearable size="small" style="width: 140px"
                        @keyup.enter.native="getList()" />
                </el-form-item>
                <el-form-item>
                    <el-button type="cyan" icon="el-icon-search" size="mini" @click="getList()">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">添加</el-button>
            </el-col>

        </el-row>

        <el-table :data="periodList">
            <el-table-column label="ID" prop="id" align="center" width="120" />
            <el-table-column label="期数" prop="period" align="center" width="350" />
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
                    <div style="display: inline-block;width: 10px"></div>
                    <el-popconfirm title="确定删除吗？" @confirm="handleDelete(scope.row)">
                        <el-button slot="reference" size="mini" type="text" icon="el-icon-delete">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

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

        <!-- 添加弹窗 -->
        <el-dialog :title="isAdd ? '添加期数' : '编辑期数'" :visible.sync="openEdit" width="400px" @close="dialogClose">
            <el-form :model="queryPlatform" :rules="rules" ref="platForm">
                <el-form-item label="期数名称" prop="period">
                    <el-input clearable size="small" v-model="queryPlatform.period" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="openEdit = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('platForm')">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { addPeriod, getPeriod, editPeriod, delPeriod } from "@/api/accounts/period";
export default {
    data() {
        return {
            queryParams: {
                pageNum: 1,
                pageSize: 50
            },
            periodList: [],
            total: 0,
            openEdit: false,
            queryPlatform: {
                period: ''
            },
            rules: {
                period: [
                    { required: true, message: '请输入期数', trigger: 'blur' }
                ],
            },
            isAdd: true
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.getList();
        },
        /** 期数新增 */
        handleAdd() {
            this.isAdd = true
            this.openEdit = true
        },
        /** 获取列表数据 */
        getList() {
            getPeriod(this.queryParams).then(response => {
                this.periodList = response.data.records
                this.total = response.data.total
            });
        },
        /** 列表编辑 */
        handleEdit(value) {
            this.openEdit = true
            this.isAdd = false
            this.queryPlatform = { ...value }
        },
        /** 列表删除 */
        handleDelete(value) {
            delPeriod(value.id).then(response => {
                if (response.data) {
                    this.msgSuccess("删除成功");
                    this.getList()
                }
            });
        },
        /** 保存 编辑 */
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.isAdd) {

                        addPeriod(this.queryPlatform).then(response => {
                            this.openEdit = false
                            if (response.data) {
                                this.msgSuccess("添加成功");
                                this.getList()
                                this.queryPlatform = {
                                    period: ''
                                }
                            }
                        }).catch(err => {
                            this.openEdit = false
                        });

                    } else {
                        console.log(this.queryPlatform)
                        editPeriod(this.queryPlatform).then(response => {
                            this.openEdit = false
                            if (response.data) {
                                this.msgSuccess("修改成功");
                                this.getList()
                            }
                        }).catch(err => {
                            this.openEdit = false
                        });
                    }


                } else {
                    return false;
                }
            });
        },
        /** 弹窗关闭触发 */
        dialogClose() {
            if (!this.isAdd) {
                this.queryPlatform = {
                    period: ''
                }
            }
        }
    }
}
</script>