<style lang="less">
    @import '../../styles/common.less';
</style>

<template>
    <div>
        <Row class="margin-top-10" v-show="proShow">
            <Col>
                <Card>
                    <p slot="title">
                        <Icon type="android-remove"></Icon>
                        月嫂订单
                        <span class="search">
                            <Input v-model="keyword" @on-enter="search" />
                            <Button type="primary" @click="search">搜索</Button>
                        </span>
                    </p>
                    <div class="edittable-table-height-con">
                        <Table :columns="columns" :data="prosHandle"></Table>
                        <Page :total="total" :page-size="pageSize" @on-change="changePage"></Page>
                    </div>
                </Card>
            </Col>
        </Row>
        <edit :editShow="editShow" :row="row" @close="closeModal"></edit>
        <remark :remarkShow="remarkShow" :row="row" @close="closeRemark"></remark>
        <detail v-show="!proShow" :form-content="formContent" @back="back"></detail>
    </div>
</template>

<script>
import edit from "./edit";
import remark from "./remark";
import detail from "./detail";
export default {
    name: 'product',
    components: {
        edit,
        remark,
        detail
    },
    computed: {
        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 if (product.status == 3) {
                    product.status = "关闭";
                } else {
                    product.status = "支付失败";
                }
            }
            return products;
        }
    },
    data () {
        return {
            row: {},
            total: 0,
            pageSize: 20,
            proShow: true,
            editShow: false,
            remarkShow: false,
            keyword: '',
            formContent: {},
            columns: [
                {title: "月嫂等级", key: 'level'},
                // {title: "预产期", key: 'expected_date'},
                // {title: "特殊需求", key: 'special_demand'},
                // {title: "服务时间", key: 'server_time'},
                // {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: 'remark', align: 'center'},
                // {title: "评论", key: 'comment', align: 'center'},
                {
                    title: "操作",
                    key: "action",
                    align: 'center',
                    width: 300,
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'default'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.formContent = params.row;
                                        this.proShow = false;
                                    }
                                }
                            }, '详情'),
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'default'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.row = params.row;
                                        this.editShow = true;
                                    }
                                }
                            }, '修改'),
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'default'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.row = params.row;
                                        this.remarkShow = true;
                                    }
                                }
                            }, '备注'),
                            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(`/maternity_matron_order/${id}`)
                                                    .then((res) => {
                                                        this.products = this.products.filter((product) => {
                                                            if (product.id != id) {
                                                                return true;
                                                            }
                                                        });
                                                        this.$Modal.remove();
                                                    });
                                            },
                                            onCancel: () => {
                                                this.$Modal.remove();
                                            }
                                        });
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                }
            ],
            products: []
        };
    },
    methods: {
        getData (num) {
            this.axios.get("/maternity_matron_orders", {
                params: {
                    page: num || 1
                }
            }).then((res) => {
                this.products = res.data.data;
                this.pageSize = res.data.per_page;
                this.total = res.data.total;
            });
        },
        closeModal(row) {
            this.editShow = false;
            if (row && row.id) {
                this.products.forEach((product, i) => {
                    if (product.id == row.id) {
                        product.price = row.price;
                        this.$set(this.products, i, product);
                    }
                });
            }
        },
        closeRemark(row) {
            this.remarkShow = false;
            if (row && row.id) {
                this.products.forEach((product, i) => {
                    if (product.id == row.id) {
                        product.remark = row.remark;
                        this.$set(this.products, i, product);
                    }
                });
            }
        },
        back() {
            this.proShow = true;
        },
        changePage(num) {
            if (this.keyword) {
                this.search(num);
            } else {
                this.getData(num);
            }
        },
        search(num) {
            this.axios.get("/maternity_matron_orders", {
                params: {
                    keyword: this.keyword,
                    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>
