<template>
    <div>
        <el-dialog :title="exData.id ? '修改' : '新增'" :visible.sync="dialog" center width="600px" top="50px">
            <div>
                <el-form ref="form" :model="form" :rules="formRules" label-width="120px">
                    <el-form-item label="所属项目：" prop="project_id" :rules="formRules.InterNum">
                        <el-select v-model="form.project_id" clearable placeholder="请选择项目">
                            <el-option v-for="(item, index) in 	exData.projectList" :key="index" :label="item.project_name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="推广来源：" prop="sources_name" :rules="formRules.required">
                        <el-input v-model="form.sources_name" placeholder="请输入推广来源 如：baidu、sohu"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer text-center">
                <el-button @click="dialog = false">取 消</el-button>
                <el-button type="primary" @click="handleSubmit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    props: {
        value: {
            type: Boolean,
            default: true,
        },
        exData: {
            type: Object,
            default: {},
        },
    },
    data() {
        return {
            form: {
                sources_name: undefined,
                project_id: undefined
            },
            dialog: !!this.value,
        }
    },
    methods: {
        async handleSubmit() {
            this.$refs.form.validate(async valid => {
                if (valid) {
                    const data = await this.$fetch('/addPromoteSource', {...this.form})
                    if (data.code == 200) {
                        this.$message.success('成功')
                        this.dialog = false
                        this.$emit('success')
                    }
                }
            })
        },
    },
    mounted() {
        if (this.exData.id) {
            this.form.id = this.exData.id
            for (const key in this.form) {
                this.form[key] = this.exData[key]
            }
        } else {
            this.form.project_id = this.exData.projectList[0].id
        }
    },
    watch: {
        dialog(val) {
            if (!val) this.$emit('input', val)
        },
    },
}
</script>
<style lang="scss">

</style>
