<template>
    <div class="edit-view">
        <page-title>
            <el-button @click="$router.go(-1)" :disabled="saving">取消</el-button>
            <!-- <el-button @click="onDelete" :disabled="saving" type="danger" v-if="formData.id"> 删除 </el-button>
            <el-button @click="onSave" :loading="saving" type="primary">保存</el-button> -->
        </page-title>
        <div class="edit-view__content-wrapper">
            <div class="edit-view__content-section">
                <el-form
                    :model="formData"
                    :rules="rules"
                    ref="form"
                    label-width="94px"
                    label-position="right"
                    size="small"
                    style="max-width: 700px"
                >
                    <el-form-item prop="userId" label="用户信息">
                        <el-input-number type="number" v-model="formData.userId" disabled></el-input-number>
                        <el-input
                            v-model="formData.nickname"
                            disabled
                            style="margin-left: 6px; width: 200px"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="auctionId" label="拍卖信息">
                        <el-input-number type="number" v-model="formData.auctionId" disabled></el-input-number>
                        <el-input v-model="formData.name" disabled style="margin-left: 6px; width: 200px"></el-input>
                    </el-form-item>
                    <el-form-item prop="pic" label="">
                        <object-upload :value="pic" disabled></object-upload>
                    </el-form-item>
                    <!-- <el-form-item prop="properties" label="特性">
                        <el-input v-model="formData.properties"></el-input>
                    </el-form-item>
                    <el-form-item prop="privileges" label="特权">
                        <el-input v-model="formData.privileges"></el-input>
                    </el-form-item> -->
                    <div class="inline-wrapper">
                        <el-form-item prop="royalties" label="版税(%)">
                            <el-input-number v-model="formData.royalties" :min="0" :max="99" disabled>
                            </el-input-number>
                        </el-form-item>
                        <el-form-item prop="serviceCharge" label="手续费(%)">
                            <el-input-number v-model="formData.serviceCharge" :min="0" :max="99" disabled>
                            </el-input-number>
                        </el-form-item>
                    </div>
                    <div class="inline-wrapper">
                        <el-form-item prop="price" label="价格">
                            <el-input-number type="number" v-model="formData.price" disabled></el-input-number>
                        </el-form-item>
                        <el-form-item prop="totalPrice" label="总价">
                            <el-input-number type="number" v-model="formData.totalPrice" disabled></el-input-number>
                        </el-form-item>
                    </div>
                    <el-form-item prop="source" label="来源">
                        <el-select v-model="formData.source" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in sourceOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="paymentType" label="支付类型">
                        <el-select v-model="formData.paymentType" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in paymentTypeOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="status" label="状态">
                        <el-select v-model="formData.status" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in statusOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <div v-if="formData.status != 'CANCELLED' && formData.status != 'NOT_PAID'">
                        <el-form-item prop="transactionId" label="交易订单号">
                            <el-input v-model="formData.transactionId" disabled></el-input>
                        </el-form-item>
                        <el-form-item prop="payMethod" label="支付信息">
                            <el-select
                                v-model="formData.payMethod"
                                clearable
                                filterable
                                placeholder="支付方式"
                                disabled
                            >
                                <el-option
                                    v-for="item in payMethodOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                </el-option>
                            </el-select>
                            <el-date-picker
                                v-model="formData.payAt"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="支付时间"
                                style="margin-left: 6px"
                                disabled
                            >
                            </el-date-picker>
                        </el-form-item>
                    </div>
                    <el-form-item prop="payTime" label="支付时间">
                        <el-date-picker
                            v-model="formData.payTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择日期时间"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="cancelTime" label="取消时间" v-if="formData.status == 'CANCELLED'">
                        <el-date-picker
                            v-model="formData.cancelTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择日期时间"
                            disabled
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="收货信息">
                        <el-input v-model="formData.contactName" style="width: 180px; margin-right: 6px"></el-input>
                        <el-input v-model="formData.contactPhone" style="width: 240px"></el-input>
                        <el-input
                            type="textarea"
                            size="2"
                            v-model="formData.address"
                            style="margin-top: 6px"
                        ></el-input>
                    </el-form-item>
                    <el-form-item class="form-submit">
                        <!-- <el-button @click="onSave" :loading="saving" type="primary"> 保存 </el-button>
                        <el-button @click="onDelete" :disabled="saving" type="danger" v-if="formData.id">
                            删除
                        </el-button> -->
                        <el-button @click="$router.go(-1)" :disabled="saving">取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'AuctionOrderEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('auctionOrder/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {},
            sourceOptions: [
                { label: '官方拍卖', value: 'OFFICIAL' },
                { label: '转让拍卖', value: 'TRANSFER' }
            ],
            paymentTypeOptions: [
                { label: '保证金', value: 'DEPOSIT' },
                { label: '成交金', value: 'PURCHASE_PRICE' },
                { label: '一口价', value: 'FIXED_PRICE' }
            ],
            statusOptions: [
                { label: '未支付', value: 'NOT_PAID' },
                { label: '已完成', value: 'FINISH' },
                { label: '已取消', value: 'CANCELLED' },
                { label: '退款中', value: 'REFUNDING' },
                { label: '已退款', value: 'REFUNDED' },
                { label: '待发货', value: 'DELIVERY' },
                { label: '待收货', value: 'RECEIVE' }
            ],
            payMethodOptions: [
                { label: '微信', value: 'WEIXIN' },
                { label: '支付宝', value: 'ALIPAY' },
                { label: '无GAS费', value: 'FREE' },
                { label: '衫德支付', value: 'SANDPAY' },
                { label: '河马支付', value: 'HMPAY' },
                { label: '首信易', value: 'PAYEASE' },
                { label: '余额支付', value: 'BALANCE' }
            ]
        };
    },
    computed: {
        pic() {
            return (this.formData.pic || [])[0] || {};
        }
    },
    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('/auctionOrder/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.$confirm('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/auctionOrder/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 || '删除失败');
                    }
                });
        }
    }
};
</script>
<style lang="less" scoped>
.inline-wrapper {
    .el-form-item {
        display: inline-block;
    }
}
</style>
