<template>
<div>
  <el-button type="primary" size="small" @click="dialogVisible = true">新 建</el-button>
  <el-drawer append-to-body title="新建" :visible.sync="dialogVisible" :before-close="handleClose" v-if="tool.checkAuth($route.name,'read')" size="800px">
    <div class="drawer__panel">
      <el-row :gutter="20">
        <el-form ref="formInfo" :model="param.content" :rules="rules" label-width="90px" label-position="right">
          <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="电子邮箱:">
              <el-input v-model="param.content.email" placeholder="请输入" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色配置:" prop="roleids">
              <el-select style="width:100%" 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-row>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="dialogVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="submitTeam" class="normal-btn-width">确 定</el-button>
      </div>
    </div>
  </el-drawer>
</div>
</template>

<script>
export default {
  name: 'AddUser',
  data () {
    return {
      param: {
          "classname": "sale.team.team",
          "method": "insertormodify_team",
          "content": {
              "sys_enterprise_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.sys_enterprise_hrid = val.sys_enterprise_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": {
                "sys_enterprise_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": {
          "sys_enterprise_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.$emit('onSuccess')
          })
        }
      })
    }
  },
};
</script>

<style scoped>
</style>
