<template>
  <!--
      	作者：luoyiming
      	时间：2020-06-01
      	描述：插件中心-分销-分销商用户-编辑
      -->
  <el-dialog title="编辑" :visible.sync="dialogVisible" @close="cancelFunc" :close-on-click-modal="false" :close-on-press-escape="false" width="30%">
    <el-form :model="formData">
      <el-form-item label="微信昵称" :label-width="formLabelWidth">
        <el-input type="text" v-model="formData.nickName" autocomplete="off" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="微信头像" :label-width="formLabelWidth">
        <img v-img-url="formData.avatarUrl" width="50" height="50" />
      </el-form-item>
      <el-form-item label="姓名" :label-width="formLabelWidth">
        <el-input type="text" v-model="formData.real_name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号" :label-width="formLabelWidth">
        <el-input type="text" v-model="formData.mobile" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="推荐人ID" :label-width="formLabelWidth">
        <el-input type="text" v-model="formData.referee_id" autocomplete="off"></el-input>
        <div class="tips">如果没有上级则设置为0</div>
      </el-form-item>
      <el-form-item label="推荐人昵称" :label-width="formLabelWidth">
          <span v-if="formData.referee">{{ formData.referee.nickName }}</span>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelFunc">取 消</el-button>
      <el-button type="primary" @click="confirmFunc">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import PlusApi from '@/api/plus.js';
export default {
  data() {
    return {
      /*左边长度*/
      formLabelWidth: '100px',
      /*是否显示*/
      dialogVisible: false,
      /*表单数据模型*/
      formData: {},
      old_referee_id: 0
    };
  },
  props: {
    open_edit: Boolean,
    userModel: Object
  },
  watch: {
    open_edit: function(n, o) {
      if (n != o) {
        this.dialogVisible = this.open_edit;
        if (n) {
          this.formData = this.userModel;
          this.old_referee_id = this.formData.referee_id;
        }
      }
    }
  },
  created() {},
  methods: {
    /*修改用户*/
    confirmFunc() {
      let self = this;
      let params ={};
      params.user_id= this.formData.user_id;
      params.real_name=this.formData.real_name;
      params.mobile=this.formData.mobile;
      if(self.formData.referee_id > 0 && self.formData.referee_id != self.old_referee_id){
        PlusApi.toAgentUserEdit({
          user_id: self.formData.referee_id
        }, true)
          .then(res => {
            if(!res.data.model){
              self.$message({
                message: '上级用户不存在',
                type: 'error'
              });
              return;
            }
            if(this.formData.user_id == self.formData.referee_id){
              self.$message({
                message: '不能绑定自己',
                type: 'error'
              });
              return;
            }
            self.$confirm('确定要修改上级到'+res.data.model.real_name+'?', '提示', {
              type: 'warning'
            }).then(() => {
               params.referee_id=self.formData.referee_id;
               self.editUser(params);
            });
          })
          .catch(error => {});
      }else{
        self.editUser(params);
      }
    },
    editUser(params){
      let self = this;
      PlusApi.agentUserEdit(params, true)
        .then(data => {
          self.$message({
            message: '恭喜你，修改成功',
            type: 'success'
          });
          self.cancelFunc(true);
        })
        .catch(error => {});
    },
    /*关闭弹窗*/
    cancelFunc(e) {
      let type='cancel';
      if(e){
        type='success';
      }
      this.$emit('close', {
        type:type
      });
    }
  }
};
</script>

<style></style>
