<template>
    <div>
        <el-dialog title="新增" :visible.sync="dialog" center width="600px" top="50px">
            <div v-if="show">
                <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="promotion_sources_id" :rules="formRules.InterNum">
                        <el-select v-model="form.promotion_sources_id" clearable placeholder="请选择来源">
                            <el-option v-for="(item, index) in 	exData.sourcesList" :key="index" :label="item.sources_name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="推广形式：" prop="promotion_type_id" :rules="formRules.InterNum">
                        <el-select v-model="form.promotion_type_id" clearable placeholder="请选择推广形式">
                            <el-option v-for="(item, index) in 	exData.typeList" :key="index" :label="item.type_name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="负责人：" prop="promotion_admin_id" :rules="formRules.InterNum">
                        <el-select v-model="form.promotion_admin_id" clearable placeholder="请选择负责人">
                            <el-option v-for="(item, index) in 	exData.adminList" :key="index" :label="item.admin_name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="渠道备注：" prop="remark" :rules="formRules.required">
                        <el-input v-model="form.remark" clearable placeholder="请填写备注"> </el-input>
                    </el-form-item>
                    <div class="form-title">设置推广</div>
                    <el-form-item label="是否使用相同的目标URL：" label-width="160">
                        <el-radio-group v-model="isSameUrl" clearable placeholder="请填写备注">
                            <el-radio :label="true">是</el-radio>
                            <el-radio :label="false">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item v-if="isSameUrl" label="目标URL：">
                        <el-input v-model="form.promote_list[0].properties" clearable placeholder="推广时所指向您网站的URL"> </el-input>
                    </el-form-item>
                    <div class="properties-group">
                        <div v-for="(item, index) in form.promote_list" :key="index" class="flex properties-group-item">
                            <div style="flex: 7">
                                <el-form-item v-if="!isSameUrl" :label="`目标URL${index + 1}：`">
                                    <el-input v-model="form.promote_list[index].properties" clearable placeholder="推广时所指向您网站的URL"> </el-input>
                                </el-form-item>
                                <el-form-item :label="`推广名称${index + 1}：`">
                                    <el-input v-model="form.promote_list[index].promotion_name" clearable placeholder="推广的名称，如：春季福利"> </el-input>
                                </el-form-item>
                            </div>
                            <div class="marginL-10" style="flex: 2">
                                <el-button type="text" @click="del(index)">删除</el-button>
                                <el-button type="text" @click="add">新增</el-button>
                            </div>
                        </div>
                    </div>
                    <el-form-item class="marginT-20" label="推广备注：">
                        <el-input type="textarea" :rows="3" v-model="properties_remark" > </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 {
            isSameUrl: true,
            properties_remark: '',
            show: false,
            form: {
                promote_list: [
                    {
                        properties: '',
                        promotion_name: '',
                    }
                ]
            },
            promote_list_remark: '',
            dialog: !!this.value,
        }
    },
    methods: {
        async handleSubmit() {
            this.$refs.form.validate(async valid => {
                if (valid) {
                    let promote_list = this.form.promote_list
                    let new_promote = []
                    for (const key in promote_list) {
                        new_promote.push({
                            promotion_url: promote_list[this.isSameUrl ? 0 : key].properties,
                            promotion_name: promote_list[key].promotion_name,
                            remark: this.properties_remark
                        })
                    }

                    const data = await this.$fetch('/addPromoteChannel', {...this.form, promote_list: new_promote})
                    if (data.code == 200) {
                        this.$message.success('成功')
                        this.dialog = false
                        this.$emit('success')
                    }
                }
            })
        },
        add() {
            this.form.promote_list.push({
                properties: '',
                promotion_name: '',
            })
        },
        del(index) {
            if (this.form.promote_list.length == 1) {
                this.$message.error('无法继续删除')
                return
            }
            this.form.promote_list.splice(index, 1)
        }
    },
    mounted() {
        // this.form.project_id = this.exData.projectList[0].id
        // this.form.promotion_sources_id = this.exData.sourcesList[0].id
        // this.form.promotion_type_id = this.exData.typeList[0].id
        // this.form.promotion_admin_id = this.exData.adminList[0].id
        // setTimeout(() => {
            this.show = true
        // }, 500);
        
    },
    watch: {
        dialog(val) {
            if (!val) this.$emit('input', val)
        },
    },
}
</script>
<style lang="scss">
.form-title {
    height: 50px;
    line-height: 50px;
    font-size: 16px;
    font-weight: bold;
    border-left: 2px solid #409EFF;
    background: #409EFF33;
    padding-left: 30px;
}
.properties-group {
    background: #409EFF33;
    padding: 4px 10px;
    .el-form-item {
        margin-bottom: 0;
    }
    .properties-group-item {
        & + .properties-group-item {
            margin-top: 10px;
            padding-top: 10px;
            border-top: 1px solid #ddd;
        }
    }
}
</style>
