<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="115px"
                    label-position="right"
                    size="small"
                    style="max-width: 550px"
                >
                    <el-form-item prop="id" label="订单ID">
                        <el-input-number type="number" v-model="formData.id" disabled></el-input-number>
                    </el-form-item>
                    <el-form-item prop="userId" label="用户信息">
                        <el-input-number type="number" v-model="formData.userId" disabled></el-input-number>
                        <!-- </el-form-item>
                    <el-form-item prop="phone" label="手机号"> -->
                        <el-input v-model="formData.phone" disabled style="margin-left: 6px; width: 180px"></el-input>
                    </el-form-item>
                    <el-form-item prop="mintActivity" label="铸造活动">
                        <el-input v-model="formData.mintActivity" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="铸造材料">
                        <div v-for="item in formData.material" :key="item.collectionId" style="margin-bottom: 6px">
                            <el-input
                                v-model="item.collectionId"
                                style="width: 110px; margin-right: 6px"
                                disabled
                            ></el-input>
                            <el-input v-model="item.name" style="width: 315px" disabled></el-input>
                        </div>
                    </el-form-item>
                    <el-form-item prop="consume" label="是否消耗藏品">
                        <el-radio-group v-model="formData.consume" disabled>
                            <el-radio :label="true">是</el-radio>
                            <el-radio :label="false">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="gasPrice" label="gas费">
                        <el-input-number type="number" v-model="formData.gasPrice" disabled></el-input-number>
                    </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 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 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 prop="status" label="状态">
                        <el-select
                            v-model="formData.status"
                            filterable
                            placeholder="请选择状态"
                            :disabled="formData.status == 'CANCELLED' || formData.status == 'NOT_PAID'"
                        >
                            <el-option
                                v-for="item in statusOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                                :disabled="item.disabled"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="airDrop" label="类型">
                        <el-radio v-model="formData.airDrop" :label="true" disabled>空投铸造</el-radio>
                        <el-radio v-model="formData.airDrop" :label="false" disabled>实物铸造</el-radio>
                    </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: 'MintOrderEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('mintOrder/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: {
                phone: [
                    {
                        required: true,
                        message: '请输入phone',
                        trigger: 'blur'
                    },
                    {
                        pattern: /^1[3-9]\d{9}$/,
                        message: '请输入正确的手机号',
                        trigger: 'blur'
                    }
                ],
                contactPhone: [
                    {
                        pattern: /^1[3-9]\d{9}$/,
                        message: '请输入正确的手机号',
                        trigger: 'blur'
                    }
                ]
            },
            statusOptions: [
                { label: '未支付', value: 'NOT_PAID', disabled: true },
                { label: '待发货', value: 'DELIVERY' },
                { label: '待收货', value: 'RECEIVE' },
                { label: '待空投', value: 'AIR_DROP' },
                { label: '已完成', value: 'FINISH' },
                { label: '已取消', value: 'CANCELLED', disabled: true }
            ],
            payMethodOptions: [
                { label: '微信', value: 'WEIXIN' },
                { label: '支付宝', value: 'ALIPAY' }
            ]
        };
    },
    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('/mintOrder/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(`/mintOrder/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>
/deep/.el-input.is-disabled .el-input__inner {
    color: #7c7e7e;
}
/deep/.el-radio__input.is-disabled + span.el-radio__label {
    color: #7c7e7e;
}
</style>
