<template>
  <el-dialog :title="editTarget ? '编辑' : '新建'" :visible.sync="dialogVisible" :before-close="handleClose" v-if="tool.checkAuth($route.name,'read')">
    <el-row :gutter="30">
      <el-form ref="formInfo" :model="param.content" :rules="rules" label-width="102px" label-position="left">
        <el-col :span="12">
          <el-form-item label="姓名:" prop="name">
            <el-input v-model="param.content.name" placeholder="请输入" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="职位:" prop="position">
            <el-input v-model="param.content.position" placeholder="请输入" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号:" prop="phonenumber">
            <el-input v-model="param.content.phonenumber" placeholder="请输入" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电子邮箱:" prop="email">
            <el-input v-model="param.content.email" placeholder="请输入" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="角色配置:" label-width="102px" prop="roleids">
            <el-select v-model="param.content.roleids" multiple placeholder="请选择" size="small">
              <el-option
                v-for="(item,index) in checkList"
                :key="index"
                :label="item.rolename"
                :value="item.roleid">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form>
      <el-col :span="24">
        <div class="footer" style="margin-left:50%;transform: translateX(-40%);">
          <el-button @click="dialogVisible=false" size="small">取消</el-button>
          <el-button type="primary" @click="submitTeam()" size="small" v-if="tool.checkAuth($route.name,'update')">提交</el-button>
        </div>
      </el-col>

    </el-row>

  </el-dialog>
</template>

<script>
export default {
  name: 'AddUser',
  data () {
    return {
      param: {
          "classname": "sale.team.team",
          "method": "insertormodify_team",
          "content": {
              "sa_agent_hrid": 0,
              "name": "",
              "phonenumber": "",
              "position": "",
              "email":"",
              "remarks": "",
              "roleids":[]
          }
      },
      dialogVisible: false,
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
        ],
        phonenumber: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern:/^1[3-9]\d{9}$/, message: '请输入正确手机号码',trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入电子邮箱', trigger: 'blur' },
          { pattern: /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/, message: '请输入正确电子邮箱',trigger: 'blur' }
        ],
        roleids: [
          { required: true, message: '请选择你的权限', trigger: 'blur' }
        ]
      },
      checkList: []
    };
  },
  props:['editTarget'],
  components: {
  },
  computed: {
  },
  watch: {
    editTarget: {
      handler(val) {
        this.$nextTick(() => {
          this.$refs.formInfo.clearValidate()
          this.param.content.name = val.name
          this.param.content.position = val.position
          this.param.content.phonenumber = val.phonenumber
          this.param.content.roleids = val.roleids ? val.roleids.map((item) => item) : []
          this.param.content.sa_agent_hrid = val.sa_agent_hrid
          this.param.content.email = val.email
        })
        console.log(this.param);
        
      }
    },
    dialogVisible: {
      handler(val) {
        if(!val) {
          this.param = {
            "classname": "sale.team.team",
            "method": "insertormodify_team",
            "content": {
                "sa_agent_hrid": 0,
                "name": "",
                "phonenumber": "",
                "position": "",
                "email":"",
                "remarks": "",
                "roleids":[]
            }
          }
          console.log(this.param);
          
        }
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    handleClose () {
      this.dialogVisible = false
    },
    async getRoleList() {
      let res= await this.$api.requested({
        "classname": "sale.team.team",
        "method": "queryRole",
        "content": {
            "sa_agent_hrid": 0
        }
      })
      this.checkList = res.data
    },
    //提交
    submitTeam() {
      this.$refs.formInfo.validate( async val => {
        if(val) {
          let res = await this.$api.requested(this.param)
          this.tool.showMessage(res,() => {
            this.$refs.formInfo.resetFields()
            this.dialogVisible = false
            this.$parent.getTeamData()
          })
        }
      })
    }
  },
};
</script>

<style scoped>
* {
  box-sizing: border-box;
}
/deep/ .el-dialog__title {
  font-size: 16px;
  font-weight: bold;
  color: #333333;
}
/deep/.el-dialog__body {
  padding: 30px 50px 50px 30px;
}
/* .el-form {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  } */
/* .el-form-item {
    margin-bottom: 30px;
  } */
/deep/.el-dialog {
  width: 864px;
  background: #ffffff;
}
.el-input {
  width: 240px;
}
.footer {
}
.footer .el-button {
  width: 120px;
}
.el-select {
  width: 100%;
}
</style>
