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

<template>
    <div>
        <Row class="margin-top-10" v-show="proShow">
            <Col>
                <Card>
                    <p slot="title">
                        分类信息
                        <Button type="primary" @click="add">新增</Button>
                    </p>
                    <div class="edittable-table-height-con">
                        <Table :columns="columns" :data="prosHandle"></Table>
                        <Page :total="total" :page-size="pageSize" @on-change="getData"></Page>
                    </div>
                </Card>
            </Col>
        </Row>
        <edit v-show="editShow" @pub-cancel="pubCancel" @pub-success="pubSuccess" :parent-list="pList" :form-content="formContent"></edit>
    </div>
</template>

<script>
import edit from "./edit";
export default {
    name: 'product',
    components: {
        edit
    },
    computed: {
        editShow() {
            return !this.proShow;
        },
        prosHandle() {
            let products = [].concat(JSON.parse(JSON.stringify(this.products)));
            // console.log(products);
            for(let p of products) {
                if (p.location === 'top') {
                    p._location = '是'
                } else {
                    p._location = '否'
                }
            }
            return products;
        }
    },
    data () {
        return {
            total: 0,
            pageSize: 20,
            proShow: true,
            pList: [],
            formContent: {},
            columns: [
                {title: "分类名称", key: 'name', align: 'center'},
                {title: "描述信息", key: 'info', align: 'center'},
                // {title: "图片位置", key: 'photo', align: 'center',
                //     render: (h, params) => {
                //         console.log(params);
                //         return h('img', {
                //             attrs: {
                //                 src: params.row.photo
                //             }
                //         })
                //     }
                // },
                {title: "是否显示到轮播", key: '_location', 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) {
                                                if (product.location !== 'top') {
                                                    product.location = '';
                                                }
                                                this.formContent = product;
                                            }
                                        })
                                        for(let p of this.pList) {
                                            if (p.id === this.formContent.pid) {
                                                this.formContent.pname = p.name;
                                            }
                                        }
                                    }
                                }
                            }, '修改'),
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'default'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        let id = params.row.id;
                                        this.$Modal.confirm({
                                            title: '警告',
                                            content: '确定要删除当前分类？',
                                            onOk: () => {
                                                this.axios.delete(`/classify/${id}`)
                                                    .then((res) => {
                                                        this.products = this.products.filter((product) => {
                                                            if (product.id != id) {
                                                                return true;
                                                            }
                                                        });
                                                    });
                                            },
                                            onCancel: () => {
                                                
                                            }
                                        });
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                }
            ],
            products: []
        };
    },
    methods: {
        add () {
            this.proShow = false;
            this.formContent = {};
        },
        pubSuccess() {
            this.proShow = true;
            this.getData();
        },
        pubCancel() {
            this.proShow = true;
        },
        getData (num) {
            this.axios.get("/classifies", {
                params: {
                    page: num || 1
                }
            }).then((res) => {
                this.products = res.data.data;
                this.pageSize = res.data.per_page;
                this.total = res.data.total;
            });
            this.axios.get('classify/parent', {}).then((res) => {
                if (res.data.status == 0) {
                    this.pList = res.data.data;
                }
            });
        }
    },
    created () {
        this.getData();
    }
};
</script>
