<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>
                        保姆订单
                        <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-success="pubSuccess" :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)));
            for (let product of products) {
                if (product.status == 0) {
                    product.status = "待处理";
                } else if (product.status == 1) {
                    product.status = "商家受理";
                } else if (product.status == 2) {
                    product.status = "待支付";
                } else {
                    product.status = "已支付";
                }
            }
            return products;
        }
    },
    data () {
        return {
            total: 0,
            pageSize: 20,
            proShow: true,
            formContent: {},
            columns: [
                {title: "id", key: 'id'},
                {title: "项目", key: 'contents'},
                {title: "人数", key: 'people_num'},
                {title: "服务时间", key: 'server_time'},
                {title: "休息时间", key: 'rest'},
                {title: "房屋面积", key: 'area'},
                {title: "服务地址", key: 'address'},
                {title: "联系人", key: 'contact', align: 'center'},
                {title: "联系电话", key: 'phone', align: 'center'},
                {title: "订单价格", key: 'price', align: 'center'},
                {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(`/housekeeper_order/${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 (num) {
            this.axios.get("/housekeeper_orders", {
                params: {
                    page: num || 1
                }
            }).then((res) => {
                this.products = res.data.data;
                this.pageSize = res.data.per_page;
                this.total = res.data.total;
            });
        }
    },
    created () {
        this.getData();
    }
};
</script>
