<style lang="less">
    @import '../../styles/common.less';
    .ivu-card-head {
        p {
            height: 32px;
            line-height: 32px;
        }
        button {
            float: right;
        }
    }
</style>

<template>
    <div>
        <Row class="margin-top-10" v-show="proShow">
            <Col>
                <Card>
                    <p slot="title">
                        <Icon type="android-remove"></Icon>
                        订单评论
                    </p>
                    <div class="edittable-table-height-con">
                        <Table :columns="columns" :data="prosHandle"></Table>
                    </div>
                </Card>
            </Col>
        </Row>
    </div>
</template>

<script>
export default {
    name: 'comment',
    computed: {
        editShow() {
            return !this.proShow;
        },
        prosHandle() {
            let products = [].concat(JSON.parse(JSON.stringify(this.products)));
            for (let product of products) {
                if (product.status == 0) {
                    product.status = "进行中";
                } else {
                    product.status = "结束";
                }
            }
            return products;
        }
    },
    data () {
        return {
            proShow: true,
            formContent: {},
            columns: [
                {title: "id", key: 'id'},
                {title: "标题", key: 'title'},
                {title: "状态", key: 'status', align: 'center'},
                {
                    title: "操作",
                    key: "action",
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'default'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        let id = params.row.id;
                                        this.proShow = false;
                                        this.products.forEach((product) => {
                                            if (product.id == id) {
                                                this.formContent = product;
                                            }
                                        })
                                    }
                                }
                            }, '修改'),
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'default'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        let id = params.row.id;
                                        this.axios.delete(`/campaign/${id}`)
                                            .then((res) => {
                                                this.products = this.products.filter((product) => {
                                                    if (product.id != id) {
                                                        return true;
                                                    }
                                                });
                                            });
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                }
            ],
            products: []
        };
    },
    methods: {
        add () {
            this.proShow = false;
            this.formContent = {};
        },
        pubSuccess() {
            this.proShow = true;
            this.getData();
        },
        getData () {
            this.axios.get("campaigns")
                .then((res) => {
                    this.products = res.data.data;
                });
        }
    },
    created () {
        this.getData();
    }
};
</script>
