<style>
.customerDialogBox.add-box .add-tit{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 20px;
}
.customerDialogBox.add-box .add-tit p{
    display: flex;
    align-items: center;
    font-weight: bold;
    color:rgba(17,17,17,1);
    font-size:14px;
}
.customerDialogBox.add-box .add-tit p span{
    display: inline-block;
    width:6px;
    height:6px;
    background:rgba(13,36,129,1);
    border-radius:50%;
    margin-right: 10px;
}
.customerDialogBox.add-box .el-dialog__header{
    padding: 15px 20px;
    background:rgba(255,255,255,1);
}
.customerDialogBox.add-box .dialog-footer{
    text-align: center;
    background-color: rgba(248,250,251,1);
    padding-bottom: 20px;
}
.customerDialogBox.add-box .el-dialog__footer{
    padding: 0;
}
.customerDialogBox.add-box .el-dialog__body{
    background-color: rgba(248,250,251,1);
    padding-top: 20px;
    max-height: 500px;
    overflow: auto;
}
.customerDialogBox .add-box-btn.el-button{
    border-radius: 0;
    background-color: rgba(13, 36, 129, .8);
    border-color: rgba(13, 36, 129, .8);
    color: white;
    padding: 12px 43px;
}
.customerDialogBox .add-box-btn.el-button:hover{
    background-color: rgba(13, 36, 129, 1);
    border-color: rgba(13, 36, 129, 1);
    color: white;
}
.customerDialogBox .add-box-btn.el-button.add-box-cancel{
    color: rgba(13, 36, 129, .8);
    background-color: white;
}
.customerDialogBox .add-box-btn.el-button.add-box-cancel:hover{
    color: rgba(13, 36, 129, 1);
    background-color: white;
}
.customerDialogBox .el-input .el-input-group__append {
    background-color: rgba(13, 36, 129, 1);
    border-color: rgba(13, 36, 129, 1);
    color: #fff;
}
.customerDialogBox .form-box .form-box-tit{
    display: flex;
    align-items: center;
    margin-bottom: 20px;
}
.customerDialogBox .form-box .radius{
    display: flex;
    width:4px;
    height:4px;
    background:rgba(255,164,117,1);
    border-radius:50%;
    margin-right: 10px;
}
.customerDialogBox .up-img-box .uib-item{
    margin-right: 30px;
    width: 120px;
    height: 120px;
    background-color: white;
    position: relative;
    margin-bottom: 30px;
}
.customerDialogBox .up-img-box .uib-item p{
    position: absolute;
    bottom: -30px;
    font-size: 12px;
    text-align: center;
    width: 100%;
}
.customerDialogBox .up-img-box .uib-item:hover .imgzhe-btn{
    opacity: 1
}
.customerDialogBox .imgzhe {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
}
.customerDialogBox .imgzhe .imgzhe-btn{
    background: rgba(0, 0, 0, 0.6);
    width: 100%;
    height: 100%;
    text-align: center;
    line-height: 120px;
    position: absolute;
    top: 0;
    opacity: 1;
    transition: all linear .5s
}
.customerDialogBox .imgzhe .imgzhe-btn i{
    font-size: 22px;
    margin-right: 5px;
    color: white;
    cursor: pointer;
}
.customerDialogBox .up-ctrl{
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
}
.customerDialogBox .up-ctrl>div{
    width: 100%;
    height: 100%;
    text-align: center;
    line-height: 140px;
}
</style>
<template>
    <el-dialog :visible.sync="dialogTableVisible" @closed="closedDialog" :close-on-click-modal="false" class="add-box add-box1 customerDialogBox">
        <div class="add-tit" slot="title">
            <p><span></span>{{name}}</p>
            <span icon="el-icon-close"></span>
        </div>
        <div class="form-box" v-if="show">
            <el-form :model="form" ref="form" class="MyEditForm" :rules="rules">
                <p class="form-box-tit"><span class="radius"></span> 基本信息</p>
                <el-row :gutter="30">
                    <el-col :span="8">
                        <div class="MyEditForm-item">
                            <el-form-item label="商机名称" prop="BusinessName">
                                <el-input placeholder="请输入商机名称" v-model="form.BusinessName"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="8">
                        <div class="MyEditForm-item">
                            <el-form-item label="客户名称" prop="CustomerId">
                                <el-select v-model="form.CustomerId" :disabled="type==='1' ? true : false" placeholder="请选择客户" v-if="!isGuest">
                                    <el-option 
                                        v-for="item in CustomerList"
                                        :key="item.CustomerId"
                                        :label="item.CustomerName"
                                        :value="item.CustomerId">
                                    </el-option>
                                </el-select>
                                <el-select v-model="form.CustomerId" :disabled="type==='1' ? true : false" placeholder="请选择客户" v-else>
                                    <el-option 
                                        v-for="item in CustomerList"
                                        :key="item.Id"
                                        :label="item.GuestName"
                                        :value="item.Id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="8">
                        <div class="MyEditForm-item">
                            <el-form-item label="预计人数" prop="PeopleNumber">
                                <el-input placeholder="请输入预计人数" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" v-model="form.PeopleNumber"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row>
                 <el-row :gutter="30">
                     <el-col :span="8">
                        <div class="MyEditForm-item">
                            <el-form-item label="预算价格" prop="BudgetPrice">
                                <el-input placeholder="请输入预算价格" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" v-model="form.BudgetPrice"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="8">
                        <div class="MyEditForm-item">
                            <el-form-item label="线路" prop="LineId">
                                <el-select v-model="form.LineId" filterable @change="getLineTeamList(form.LineId)" placeholder="请选择线路">
                                    <el-option
                                        v-for="item in LineList"
                                        :label='item.LineName' :value='item.LineID' :key='item.LineID'>
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="8">
                        <div class="MyEditForm-item">
                            <el-form-item label="系列" prop="LineteamId">
                                <el-select v-model="form.LineteamId" filterable placeholder="请选择系列">
                                    <el-option label='不限' value='0' key='0'></el-option>
                                    <el-option
                                        v-for="item in LineTeamList"
                                        :label='item.name' :value='item.id' :key='item.id'>
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row>
                <el-row  :gutter="30">
                    <el-col :span="8">
                        <div class="MyEditForm-item MyEditForm-item-time">
                            <el-form-item label="出发时间"  prop="DepartTime">
                                <el-date-picker
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    v-model="form.DepartTime"
                                    :clearable="false"
                                    type="datetime"
                                    placeholder="选择出发时间">
                                </el-date-picker>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="8">
                        <div class="MyEditForm-item MyEditForm-item-time">
                            <el-form-item label="商机状态" prop="BusinessStatus">
                                <el-select v-model="form.BusinessStatus" placeholder="请选择商机状态">
                                    <el-option
                                        v-for="item in BusinessStatusEnum"
                                        :label='item.Name' :value='item.ID' :key='item.ID'>
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <div class="MyEditForm-item label-pad-left">
                            <el-form-item label="备注">
                                <el-input type="textarea" placeholder="商机备注" v-model="form.Remark"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" class="add-box-btn" @click="submitForm('form')">确 定</el-button>
            <el-button class="add-box-btn add-box-cancel" @click="dialogTableVisible = false, resetForm('form')">取 消</el-button>
        </div>
    </el-dialog>
</template>
<script>
export default {
  props:['ID', 'name', 'CustomerId', 'ContactId', 'type', 'isGuest'],
  data() {
    return {
        CustomerList: [],
        LineList: [],
        LineTeamList: [],
        dialogTableVisible: false,
        form: {
            ID: 0,
            CustomerId: '',
            BudgetPrice: '',
            LineId: '',
            BusinessName: '',
            BusinessStatus: '',
            LineteamId: '',
            DepartTime: '',
            PeopleNumber: 1,
            Remark: '',
        },
        BusinessStatusEnum: [],
        rules:{
            CustomerId: [
                { required: true, message: '请选择客户', trigger: 'change' }
            ],
            BusinessName: [
                { required: true, message: '请输入商机名称', trigger: 'blur' }
            ],
            BudgetPrice: [
                { required: true, message: '请输入预算价格', trigger: 'blur' }
            ],
            BusinessStatus: [
                { required: true, message: '请选择商机状态', trigger: 'change' }
            ],
            LineId: [
                { required: true, message: '请选择线路', trigger: 'change' }
            ],
            LineteamId: [
                { required: true, message: '请选择系列', trigger: 'change' }
            ],
            DepartTime: [
                { required: true, message: '请选择时间', trigger: 'change' }
            ],
            PeopleNumber: [
                { required: true, message: '请输入预计人数', trigger: 'blur' }
            ],
            Remark: [
                { required: true, message: '请输入备注', trigger: 'blur'}
            ],
        },
        show: true,
    };
  },watch:{
        ID: {
            handler(val, oldVal){
                console.log(val)
            },
            deep:true
        },
        CustomerId: {
            handler(val, oldVal){
                console.log(val)
            },
            deep:true
        }
        ,
        isGuest: {
            handler(val, oldVal){
                if (val) {
                    this.form.CustomerType = 1
                    this.getGuestList()
                } else {
                    this.getCustomerList()
                }
            },
            deep:true
        }
    },
  mounted() {
    console.log(this.isGuest)
    if (this.isGuest) {
        this.form.CustomerType = 1
        this.getGuestList()
    } else {
        this.getCustomerList()
    }
    this.getLineList()
    this.GetBusinessStatusEnum()
    let $this = this
    if (this.CustomerId!==0) {
        this.form.CustomerId = this.CustomerId
    }
    setTimeout(()=>{
        $this.dialogTableVisible = true
    },50)
    if (this.ID!==0) {
        let msg = {
            BusinessId: this.ID
        }
        if (this.isGuest) {
           msg.CustomerType = 1
        }
        this.apipost('/api/Customer/GetAllBusinessModel', msg, res=>{
            if (res.data.resultCode == 1) {
                let data = res.data.data
                if (data.LineId) {
                    this.getLineTeamList(data.LineId, 1)
                }
                this.form = data
            } 
        })
    }
  },
  methods: {
    //获取系列列表
    getLineTeamList(lineId, type) {
        if (!type) {
            this.form.LineteamId = ''
        }
        this.apipost2("b2b_get_GetLineTeam", {
            lineId: lineId
        }, res => {
            if (res.data.resultCode == 1) {
                this.LineTeamList = res.data.data;
            }
        });
    },
    getLineList() {
        this.apipost2("line_post_GetAllList", {}, res => {
            if (res.data.resultCode == 1) {
                this.LineList = res.data.data;
            }
        });
    },
    GetBusinessStatusEnum(){
        this.apipost('/api/Customer/GetBusinessStatusEnum', {}, res=>{
            if (res.data.resultCode == 1) {
                this.BusinessStatusEnum = res.data.data
            }
        })
    },
    getGuestList(){
        this.apipost('/api/Customer/GetGuesstCustomerByCreateList', {}, res=>{
            if (res.data.resultCode == 1) {
                this.CustomerList = res.data.data
            }
        })
    },
    getCustomerList(){
        this.apipost('/api/Customer/GetCustomerListByCreateInfo', {}, res=>{
            if (res.data.resultCode == 1) {
                this.CustomerList = res.data.data
            }
        })
    },
    closedDialog(){
        this.MsgBus.$emit('closeBusinessDialogBox')
    },
    addData(){
        this.apipost('/api/Customer/CreateCustomerBusiness', this.form, res=>{
            if (res.data.resultCode == 1) {
                this.dialogTableVisible = false;
                // 有id 发送更新抽屉消息
                if(this.CustomerId !== 0) {
                    this.$message.success('操作成功');
                    this.MsgBus.$emit('RefreshInfoBox')
                } else {
                    this.$message.success('操作成功');
                }
                this.$emit('getList')
                this.MsgBus.$emit('RefreshbusSelect')
                this.resetForm("form");
            } else {
                this.$message.error(res.data.message);
            }
        })
    },
    submitForm(addMsg) {
      //提交创建、修改表单
      this.$refs[addMsg].validate(valid => {
        if (valid) {
          this.addData();
        } else {
          return false;
        }
      });
    },
    resetForm(formName) { // 重置表单
        this.form =  {
            ID: 0,
            CustomerId: 0,
            BudgetPrice: '',
            LineId: '',
            BusinessName: '',
            BusinessStatus: '',
            LineteamId: '',
            DepartTime: '',
            PeopleNumber: 0,
            Remark: '',
        }
        this.$refs[formName].resetFields();
    },
  }
}
</script>