<template>
  <div>
    <el-button size="small" type="primary" icon="el-icon-plus" @click="onShow">新 建</el-button>
    <el-drawer
      title="创建用户"
      :visible.sync="drawer"
      size="664px"
      direction="rtl"
      append-to-body
      :show-close="false"
      @close="onClose">
      <div class="drawer__panel">
        <el-row>
          <el-form :model="form" :rules="rules" ref="form"  size="mini" label-position="right">
            <el-col :span="24">
              <el-form-item label="手机号码" label-width="80px" prop="phonenumber">
                <el-popover
                  placement="bottom"
                  trigger="manual"
                  v-model="visible1"
                  width="500">
                  <searchInput ref="searchInput" :param="pepoleParam" :radio="true" @onSelect="setLeader" @onCancel="visible1 = false"></searchInput>
                  <el-input type="text" v-model.number="form.phonenumber" size="small" placeholder="请输入手机号" slot="reference" @input="phoneChange"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="账号" label-width="80px" prop="accountno">
                <el-input v-model="form.accountno" :disabled="!!isSelectPhone" autocomplete="off" placeholder="编辑账号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="用户名称" label-width="80px" prop="name">
                <el-input  v-model="form.name" :disabled="!!isSelectPhone" autocomplete="off" placeholder="输入用户名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="isSelectPhone">
              <el-form-item label="用户类型" label-width="80px" prop="usertype">
                <el-select v-model="form.usertype" style="width:100%" placeholder="请选择">
                  <el-option
                    v-for="item in selectType"
                    :label="item.remarks"
                    :value="item.value"
                    :key="item.index"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="授权角色" label-width="80px" prop="roleids">
                <!-- <el-input v-model="form.sex" autocomplete="off" placeholder="输入性别"></el-input> -->
                <el-select v-model="form.roleids" style="width:100%" placeholder="请选择" multiple>
                  <el-option
                    v-for="item in options"
                    :key="item.index"
                    :label="item.rolename"
                    :value="item.roleid">
                    <span style="float: left">{{ item.rolename }}</span>
                    <span style="float: right; color: #8492a6; font-size: 12px">{{ item.remarks?item.remarks:'暂无描述' }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="drawer = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import searchInput from '@/template/searchInput/index.vue'
export default {
  components:{searchInput},
  data () {
    return {
      drawer:false,
      visible1:false,
      timer:null,
      isDisabled:false,
      isSelectPhone:'',
      selectType:[],
      rules:{
        phonenumber: [
          { required: true, message: '请输入手机号码', trigger: 'change' },
          { pattern:/^1[3-9]\d{9}$/, message: '请输入正确手机号码',trigger: 'change' }
        ],
         name: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
        ],
        roleids: [
          { required: true, message: '请选择授权角色', trigger: 'change' }
        ],
        usertype: [
          { required: true, message: '请选择用户类型', trigger: 'change' }
        ],
      },
      form:{
        "userid": 0,
        "name": "",
        "phonenumber": "",
        "status": 1,
        "remarks":"",
        "roleids":[],
        "accountno":'',
      },
      options:[],
      pepoleParam: {
        "classname": "webmanage.users.users",
        "method": "query_notinmysiteuserList",
        "content": {
            "pageNumber": 1,
            "pageSize": 99999,
            "where": {
                "condition": ""
            }
        }
      }
    }
  },
  watch: {
    drawer (val) {
      if (!val) {
        this.$refs.form.resetFields()
        this.isSelectPhone = ''
      }
    }
  },
  methods:{
    onShow () {
      this.drawer = true
      this.rolelist()
      this.accountTypeList()
    },
    async accountTypeList() {
      let param = {
        classname: "sysmanage.develop.optiontype.optiontype",
        method: "optiontypeselect",
        content: {
          pageNumber: 1,
          pageSize: 20,
          typename: "usertype",
          parameter: {},
        },
      };
      const res = await this.$api.requested(param);
      res.data.pop()
      this.selectType = res.data;
      console.log(this.selectType);
      
    },
    async rolelist () {
      let param = {
        "classname": "sysmanage.develop.optiontype.optiontype",
        "method": "optiontypeselect",
        "content": {
            "pageNumber": 1,
            "pageSize": 20,
            "typename": "role",
            "parameter": {
            }
        }
      }
      const res = await this.$api.requested(param)
      this.options = res.data
    },
    phoneChange (e) {
      if (e.length < 7) return this.visible1 = false

      if (this.isSelectPhone && this.isSelectPhone != e) {
        this.clearData()
      }
      
      this.visible1 = false
      if (this.timer) clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.pepoleParam.content.where.condition = e
        this.$refs.searchInput.listData(() => {
          this.visible1 = true
        })
      },500)
    },
    setLeader (data) {
      console.log(data);
      this.visible1 = false
      this.form.userid = data.userid
      this.form.name = data.name
      this.form.phonenumber = data.phonenumber
      this.form.accountno = data.accountno
      this.isSelectPhone = data.phonenumber
    },
    clearData () {
      this.form.userid = ''
      this.form.name = ''
      this.form.accountno = ''
      this.isSelectPhone = ''
      this.form.usertype = ''
      this.form.roleids = []
    },
    onSubmit () {
      this.$refs['form'].validate((valid) => {
        if (!valid) return false
        if (!this.isSelectPhone) delete this.form.usertype
        this.$api.requested(this.isSelectPhone ? {
            "accesstoken": "be50a88845d9eee3fe75a924642cff22",
            "classname": "webmanage.users.users",
            "method": "addUserSite",
            "content": {
                "userid": this.form.userid,
                "roleids":this.form.roleids,
                "usertype":this.form.usertype
            }
        } : {
          "classname": "webmanage.users.users",
          "method": "insertormodify_user",
          "content": this.form
        }).then(res=>{
          if (res.code === 1) {
            this.$notify({
              title: '成功',
              message: '账号添加成功',
              type: 'success'
            });
            this.drawer = false
            this.$emit('onSuccess')
          } else {
            this.$notify({
              title: '失败',
              message: res.msg,
              type: 'error'
            });
          }
        })
      });
    },
    onClose () {
      this.$refs['form'].resetFields()
    }
  }
}

</script>
<style scoped>
.dialog-footer{
  margin-top:32px;
  text-align: center;
}
</style>