<template>
    <div>
        <el-dialog :title="title" :visible.sync="visible" width="30%" :before-close="onClose">
            <el-form :model="queryParams" :rules="rules" ref="queryParamsModify" label-position="left" label-width="100px">
                <el-form-item label="小程序名称" prop="appName">
                    <el-input v-model="queryParams.appName" placeholder="请输入小程序名称" clearable size="small" />
                </el-form-item>
                <el-form-item label="AppId" prop="appId">
                    <el-input v-model="queryParams.appId" placeholder="请输入AppId" clearable size="small" />
                </el-form-item>
                <el-form-item label="小程序详情" prop="appDesc">
                    <el-input v-model="queryParams.appDesc" placeholder="请输入小程序详情" clearable size="small" />
                </el-form-item>
                <el-form-item label="小程序备注" prop="appRemark">
                    <el-input v-model="queryParams.appRemark" placeholder="请输入小程序备注" clearable size="small"
                        type="textarea" :rows="2" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="onClose()">取 消</el-button>
                <el-button type="primary" :loading="loading" @click="submit('queryParamsModify')">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { modifyMini } from "@/api/accounts/miniprogram";
export default {
    data() {
        return {
            queryParams: {
                appDesc: null,
                appId: null,
                appName: null,
                appRemark: null,
            },
            title: '新增小程序',
            rules: {
                appName: [
                    { required: true, message: '请输入小程序名称', trigger: 'blur' }
                ],
                appId: [
                    { required: true, message: '请输入AppId', trigger: 'blur' }
                ],
                appDesc: [
                    { required: true, message: '请输入小程序详情', trigger: 'blur' }
                ]
            },
            loading: false
        }
    },
    props: {
        visible: {
            type: Boolean,
            default: false,
        },
        modifyData: {
            type: Object,
            default: null
        }
    },
    watch: {
        modifyData: {
            handler(val) {
                if (val && Object.keys(val).length > 0) {
                    this.queryParams = val
                } else {
                    this.queryParams = {
                        appDesc: null,
                        appId: null,
                        appName: null,
                        appRemark: null,
                    }
                }
            },
            immediate: true,
        },
    },
    methods: {
        onClose() {
            this.$emit("onClose");
        },
        submit(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true
                    let data = await modifyMini(this.queryParams)
                    this.loading = false
                    if (data.data) {
                        this.$message({
                            message: this.queryParams.id ? '修改成功' : '新增成功',
                            type: 'success'
                        });
                        this.resetForm(formName);
                        this.$emit("onChange");
                    }
                }
            })
        },

    }
}
</script>