<template>
    <div class="edit-view">
        <el-form :model="formData" :rules="rules" ref="form" label-width="108px" label-position="right" size="small"
                 style="max-width: 500px;">
            <el-form-item prop="userId" label="用户ID">
                <el-input-number type="number" v-model="formData.userId"></el-input-number>
            </el-form-item>
            <el-form-item prop="userAddress" label="配送地址">
                <el-input v-model="formData.userAddress"></el-input>
            </el-form-item>
            <el-form-item prop="merchantId" label="商户ID">
                <el-input-number type="number" v-model="formData.merchantId"></el-input-number>
            </el-form-item>
            <el-form-item prop="merchantStatus" label="商家状态">
                <el-input v-model="formData.merchantStatus"></el-input>
            </el-form-item>
            <!--            <el-form-item prop="merchantAddress" label="商家地址">-->
            <!--                <el-input v-model="formData.merchantAddress"></el-input>-->
            <!--            </el-form-item>-->
            <el-form-item prop="jobNumber" label="骑手工号">
                <el-input v-model="formData.riderId"></el-input>
                <el-button type="primary" @click="changeStatus">{{formData.riderStatus}}</el-button>
            </el-form-item>

            <!--      <el-form-item prop="riderStatus" label="骑手状态">
                      <el-input v-model="formData.riderStatus"></el-input>
                  </el-form-item>-->
            <el-form-item prop="riderStatus" label="骑手状态">
                <el-select v-model="formData.riderStatus" clearable filterable placeholder="请选择">
                    <el-option
                            v-for="item in riderStatusOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="totalAmount" label="总价">
                <el-input-number type="number" v-model="formData.totalAmount"></el-input-number>
            </el-form-item>
            <!--            <el-form-item prop="goodsAmount" label="商品总价">-->
            <!--                <el-input-number type="number" v-model="formData.goodsAmount"></el-input-number>-->
            <!--            </el-form-item>-->
            <el-form-item prop="deliveryAmount" label="配送费">
                <el-input-number type="number" v-model="formData.deliveryAmount"></el-input-number>
            </el-form-item>
            <el-form-item prop="realAmount" label="实付金额">
                <el-input-number type="number" v-model="formData.realAmount"></el-input-number>
            </el-form-item>
            <el-form-item prop="payMethod" label="支付方式">
                <el-select v-model="formData.payMethod" clearable filterable placeholder="请选择">
                    <el-option
                            v-for="item in payMethodOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="cancel" label="取消订单">
                <el-switch v-model="formData.cancel"></el-switch>
            </el-form-item>
            <el-form-item prop="rated" label="已评价">
                <el-switch v-model="formData.rated"></el-switch>
            </el-form-item>
            <el-form-item prop="orderTime" label="下单时间">
                <el-date-picker
                        v-model="formData.orderTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
            <!--            <el-form-item prop="merchantOrderTime" label="商家接单时间">-->
            <!--                <el-date-picker-->
            <!--                        v-model="formData.merchantOrderTime"-->
            <!--                        type="datetime"-->
            <!--                        value-format="yyyy-MM-dd HH:mm:ss"-->
            <!--                        placeholder="选择日期时间">-->
            <!--                </el-date-picker>-->
            <!--            </el-form-item>-->
            <!--            <el-form-item prop="riderOrderTime" label="骑手接单时间">-->
            <!--                <el-date-picker-->
            <!--                        v-model="formData.riderOrderTime"-->
            <!--                        type="datetime"-->
            <!--                        value-format="yyyy-MM-dd HH:mm:ss"-->
            <!--                        placeholder="选择日期时间">-->
            <!--                </el-date-picker>-->
            <!--            </el-form-item>-->
            <el-form-item prop="userReceivedTime" label="用户收到时间">
                <el-date-picker
                        v-model="formData.userReceivedTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
            <!--            <el-form-item prop="isCoupon" label="使用优惠券">-->
            <!--                <el-switch v-model="formData.isCoupon"></el-switch>-->
            <!--            </el-form-item>-->
            <el-form-item prop="goods" label="商品详情">
                <template v-if="goods">
                    <el-table :data="goods">
                        <el-table-column prop="goodsId" label="商品ID">
                        </el-table-column>
                        <el-table-column prop="goods.name" label="商品名称"></el-table-column>
                        <el-table-column prop="num" label="数量">
                        </el-table-column>
                        <el-table-column prop="specification" label="规格">
                        </el-table-column>
                        <el-table-column prop="goodsPrice" label="价格">
                        </el-table-column>

                    </el-table>
                </template>

            </el-form-item>

            <el-form-item>
                <el-button @click="onSave" :loading="saving"
                           type="primary">保存
                </el-button>
                <el-button @click="onDelete" :loading="saving"
                           type="danger" v-if="formData.id">删除
                </el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>


        </el-form>
    </div>
</template>
<script>
    export default {
        name: 'OrderInfoEdit',
        created() {
            if (this.$route.query.id) {
                this.$http
                    .get('orderInfo/get/' + this.$route.query.id)
                    .then(res => {
                        this.formData = res;
                    })
                    .catch(e => {
                        console.log(e);
                        this.$message.error(e.error);
                    });

                this.$http
                    .get('/orderGoodsSpec/byOrder',
                        {
                            orderId: this.$route.query.id
                        }
                    )
                    .then(res => {
                        this.goods = res;
                    })
                    .catch(e => {
                        console.log(e);
                    });
            }
        },
        data() {
            return {
                saving: false,
                formData: {},
                rules: {},
                riderStatusOptions: [
                    {"label": "接单中", "value": "RECEIVED"},
                    {"label": "取餐中", "value": "TAKE_MEAL"},
                    {"label": "送餐中", "value": "MEAL_DELIVERY"},
                    {"label": "完成", "value": "CARRY_OUT"}],
                payMethodOptions: [
                    {"label": "支付宝", "value": "ALI_PAY"},
                    {"label": "货到付款", "value": "CASH_DELIVERY"},
                    {"label": "信用卡", "value": "CREDIT_CARD"}],
                goods: [],
            }
        },
        methods: {
            onSave() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.submit();
                    } else {
                        return false;
                    }
                });
            },
            submit() {
                let data = {...this.formData};

                this.saving = true;
                this.$http
                    .post('/orderInfo/save', data, {body: 'json'})
                    .then(res => {
                        this.saving = false;
                        this.$message.success('成功');
                        this.$router.go(-1);
                    })
                    .catch(e => {
                        console.log(e);
                        this.saving = false;
                        this.$message.error(e.error);
                    });
            },
            onDelete() {
                this.$alert('删除将无法恢复，确认要删除么？', '警告', {type: 'error'}).then(() => {
                    return this.$http.post(`/orderInfo/del/${this.formData.id}`)
                }).then(() => {
                    this.$message.success('删除成功');
                    this.$router.go(-1);
                }).catch(e => {
                    if (e !== 'cancel') {
                        console.log(e);
                        this.$message.error(e.error);
                    }
                })
            },
            changeStatus() {
                this.$alert('确认此状态已完成？', '提示').then(() => {
                    return this.$http.post("/orderInfo/riderStatus", {
                        orderId: this.formData.id,
                        pass: true
                    })
                }).then(() => {
                    this.$message.success('状态已改');
                    this.$router.go(0);
                    //this.$router.go(-1);
                }).catch(e => {
                    if (e !== 'cancel') {
                        console.log(e);
                        this.$message.error(e.error);
                    }
                })
            }
        }
    }
</script>
<style lang="less" scoped>
</style>
