<template>
    <div>
        <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small" style="max-width: 500px;">
            <el-form-item prop="userId" label="用户">
                <el-input v-model="formData.userId" :disabled="'userId'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="orderCode" label="订单编码">
                <el-input v-model="formData.orderCode" :disabled="'orderCode'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="storeId" label="店铺">
                <el-input v-model="formData.storeId" :disabled="'storeId'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="productType" label="商品类型">
                <el-input v-model="formData.productType" :disabled="'productType'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="productId" label="商品ID">
                <el-input v-model="formData.productId" :disabled="'productId'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="productName" label="商品名称">
                <el-input v-model="formData.productName" :disabled="'productName'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="specification" label="规格">
                <el-input v-model="formData.specification" :disabled="'specification'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="unitPrice" label="单价">
                <el-input v-model="formData.unitPrice" :disabled="'unitPrice'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="quantity" label="数量">
                <el-input v-model="formData.quantity" :disabled="'quantity'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="userCouponId" label="优惠券">
                <el-input v-model="formData.userCouponId" :disabled="'userCouponId'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="offPrice" label="优惠金额">
                <el-input v-model="formData.offPrice" :disabled="'offPrice'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="totlePrice" label="总价">
                <el-input v-model="formData.totlePrice" :disabled="'totlePrice'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="dealPrice" label="成交价">
                <el-input v-model="formData.dealPrice" :disabled="'dealPrice'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="payMode" label="支付方式">
                <el-input v-model="formData.payMode" :disabled="'payMode'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="payTime" label="支付时间">
                <template>
                    <div class="block">
                        <el-date-picker v-model="formData.payTime" type="datetime" value-format="timestamp" placeholder="选择日期" :disabled="'payTime'==subColumn">
                        </el-date-picker>
                    </div>
                </template>
            </el-form-item>
            <el-form-item prop="remark" label="备注">
                <el-input v-model="formData.remark" :disabled="'remark'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="showFlag" label="显示标识">
                <el-input v-model="formData.showFlag" :disabled="'showFlag'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="statusFlag" label="状态">
                <el-input v-model="formData.statusFlag" :disabled="'statusFlag'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="fee" label="手续费">
                <el-input v-model="formData.fee" :disabled="'fee'==subColumn"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="onSave" :loading="$store.state.fetchingData" type="primary">保存</el-button>
                <el-button @click="onDelete" v-if="formData.id" type="danger">删除</el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import formValidator from '../formValidator'

export default {
    created() {
        if (this.$route.query.column) {
            this.subColumn = this.$route.query.column.split(',')[1];
            this.subValue = this.$route.query.column.split(',')[0];
        }

        if (this.$route.query.id) {
            this.$http.get({
                url: '/userOrder/getOne',
                data: {
                    id: this.$route.query.id
                }
            }).then(res => {
                if (res.success) {


                    this.formData = res.data;

                    if (this.$route.query.column) {
                        this.formData[this.subColumn] = this.subValue;
                    }
                }
            })
        } else {
            if (this.$route.query.column) {
                this.formData[this.subColumn] = this.subValue;
            }
        }

    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {
            },
            subColumn: '',
            subValue: '',
        }
    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            var data = JSON.parse(JSON.stringify(this.formData));
            this.$http.post({
                url: this.formData.id ? '/userOrder/update' : '/userOrder/save',
                data: data
            }).then(res => {
                if (res.success) {
                    this.$message.success('成功');
                    this.$router.go(-1);
                } else {
                    this.$message.warning('失败')
                }
            });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/userOrder/del',
                    data: { id: this.formData.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
                this.$router.go(-1);
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
    }
}
</script>
<style lang="less" scoped>
</style>
