<template>
    <el-dialog :visible.sync="visible" v-if="visible" :title="dialogTitle" width="58%" class="dialog"
        @close="handleClose">
        <el-form :model="form" ref="formRef" :rules="rules" label-position="top">
            <el-row :gutter="20">
                <el-col :span="24">
                    <el-form-item label="商品名称" prop="name">
                        <el-input placeholder="请输入" v-model="form.name"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="兑换所需积分" prop="exchangePoints">
                        <el-input-number placeholder="请输入兑换所需积分" :min="0"
                            v-model="form.exchangePoints" style="width: 100%;"></el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="商品参考价" prop="salesPrice">
                        <el-input-number style="width: 100%;"  placeholder="请输入商品参考价" :min="0" v-model="form.salesPrice"></el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="商品总数量" prop="stockNum">
                        <el-input-number style="width: 100%;" placeholder="请输入商品总数量" :min="0" v-model="form.stockNum"></el-input-number>
                    </el-form-item>
                </el-col>
                  <el-col :span="12">
                    <el-form-item label="商品状态" prop="showStatus">
                        <el-radio-group v-model="form.showStatus">
                            <el-radio :label="1">上架</el-radio>
                            <el-radio :label="0">下架</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="商品图片" prop="imgsUrl">
                        <ImageUpload v-model="form.imgsUrl" :limit="1"></ImageUpload>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="handleSubmit">确认</el-button>
        </template>
    </el-dialog>
</template>
<script>
import { addData, detailData, editData } from '@/api/pointsMall';
export default {
    name: 'FormDialog',
    data() {
        return {
            visible: false,
            dialogTitle: '新增商品',
            form: {
                showStatus:1
            },
            rules: {
                name: [{ required: true, message: '请输入商品', trigger: 'blur' }],
                exchangePoints: [{ required: true, message: '请输入兑换所需积分', trigger: 'blur' }],
                salesPrice: [{ required: true, message: '请输入商品参考价', trigger: 'blur' }],
                stockNum: [{ required: true, message: '请输入商品总数量', trigger: 'blur' }],
                showStatus: [{ required: true, message: '请选择商品状态', trigger: 'change' }],
                imgsUrl:[{required: true, message: '请上传商品图片', trigger: 'change'}]
            }
        }
    },
    methods: {
        openModal(row) {
            this.visible = true;
            if (row) {
                this.dialogTitle = '修改商品';
                this.getDetail(row)
            } else {
                this.dialogTitle = '新增商品'
            }
        },
        // 查详情
        getDetail(row) {
            detailData(row.code).then(res => {
                console.log(res)
                this.form = { ...res.data }
            })
        },
        // 提交
        handleSubmit() {
            console.log(this.form)
            debugger
            this.$refs.formRef.validate((valid) => {
                if (valid) {
                    if (this.form.id) {
                        editData(this.form).then(res => {
                            if (res.code == 200) {

                                this.$modal.msgSuccess("修改成功");
                                this.handleClose()
                                this.$emit('handleOk')
                            } else {
                                this.$modal.msgError(res.msg || "修改失败");
                                this.handleClose()
                            }
                        })
                    } else {
                        addData(this.form).then(res => {
                            if (res.code == 200) {
                                this.$modal.msgSuccess("新增成功");
                                this.handleClose()
                                this.$emit('handleOk')
                            } else {
                                this.$modal.msgError(res.msg || "新增失败");
                                this.handleClose()
                            }
                        })
                    }
                }
            })
        },
        // 关闭
        handleClose() {
            this.form = {
                showStatus:1
            };
            this.visible = false
        }
    }
}
</script>

<style lang="scss">
.dialog {
    .el-dialog__body {
        padding: 0 20px !important;
    }
}
</style>