<template>
    <div class="MessagePoint">
        <el-button @click="add" type="primary" icon="el-icon-document-add">添加</el-button>
        <hr class="hr">
        <el-table :data="tableData" border style="width: 60%">
            <el-table-column prop="id" label="序号" width="120"></el-table-column>
            <el-table-column prop="code" label="消息点编码" width="120" ></el-table-column>
            <el-table-column label="消息点描述">
                <template slot-scope="scope">
                    <p>{{scope.row.description}}</p>
                </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" ></el-table-column>
            <el-table-column fixed="right" label="操作" width="150">
                <template slot-scope="scope">
                    <el-button @click="edit(scope.row)" type="text" size="small">编辑</el-button>
                    <el-popconfirm title="是否删除此设备的信息？" @onConfirm="del(scope.row)">
                        <el-button slot="reference" type="text" size="small">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog :title="isAdd?'添加消息点':'编辑消息点'" :visible.sync="showDialog">
            <el-row type="flex">
                <el-col :span="14">
                    <el-form
                        ref="dyForm"
                        :model="dyForm"
                        :rules="rules"
                        label-width="100px"
                        class="demo-dynamic"
                    >
                        <el-form-item label="消息点编码:" prop="code">
                            <el-input v-model="dyForm.code"></el-input>
                        </el-form-item>
                        <el-form-item label="消息点描述:" prop="description">
                            <el-input v-model="dyForm.description"></el-input>
                        </el-form-item>
                        <el-form-item label="备注:">
                            <el-input type="textarea" v-model="dyForm.remark"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button @click="showDialog=false">取 消</el-button>
                            <el-button type="primary" @click="submitForm('dyForm')">保 存</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
        </el-dialog>
    </div>
</template>
<script>
import { mapActions } from "vuex";
const rules = {
    code: [{ required: true, message: "请输入消息点编码", trigger: "blur" }],
    description: [
        { required: true, message: "请输入消息点描述", trigger: "blur" }
    ]
};

export default {
    name: "MessagePoint",
    data() {
        return {
            tableData: [],
            showDialog: false,
            dyForm: {
                code: null,
                description: "",
                remark: ""
            },
            rules,
            isAdd: true
        };
    },
    created() {},
    mounted() {
        this.getMessagePoint();
    },
    methods: {
        ...mapActions(["fetch"]),
        // 获取消息点
        getMessagePoint() {
            this.fetch({
                api: "/message/point/list",
                method: "GET",
                data: {},
                success: res => {
                    console.log(res);
                    this.tableData = res;
                    // this.subDeviceList = res;
                },
                fail: err => {
                    console.log(err);
                    if (err.errMsg) this.$message.error(err.errMsg);
                    else this.$message.error("服务器发生异常");
                }
            });
        },
        add() {
            this.isAdd = true;
            this.showDialog = true;
            this.dyForm = {
                code: null,
                description: "",
                remark: ""
            }
        },
        edit(row) {
            this.isAdd = false;
            this.showDialog = true;
            this.dyForm = row;
        },
        del(row) {
            this.fetch({
                api: "/message/point/delete",
                method: "POST",
                data: {id:row.id},
                success: res => {
                    console.log(res);
                    this.getMessagePoint();
                    this.$message,success("删除成功！")
                    // this.$message.success(res.msg);
                },
                fail: err => {
                    console.log(err);
                    if (err.errMsg) this.$message.error(err.errMsg);
                    else this.$message.error("服务器发生异常");
                }
            });
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    console.log(this.dyForm);
                    this.isAdd
                        ? this.reqSave("/message/point/add")
                        : this.reqSave("/message/point/update");
                } else {
                    return false;
                }
            });
        },
        // 请求 保存 （添加 和 更新）
        reqSave(api) {
            this.fetch({
                api,
                method: "POST",
                data: this.dyForm,
                success: res => {
                    console.log(res);
                    this.getMessagePoint();
                    this.$message.success('保存消息点成功！')
                    this.showDialog = false;
                },
                fail: err => {
                    console.log(err);
                    if (err.errMsg) this.$message.error(err.errMsg);
                    else this.$message.error("服务器发生异常");
                }
            });
        }
    }
};
</script>
<style lang="scss" scoped>
.MessagePoint{
    .hr{
        border-top: 2px solid #eee;
        margin: 10px 0;
    }
}
</style>

