<template>
  <el-dialog class="client-data-dialog" title="业务组人员维护" width="40%" :visible.sync="visible" :before-close="handleClose">
      <el-form :model="ruleForm" ref="ruleForm"  label-width="80px" class="demo-ruleForm">
          <el-form-item label="业务组" prop="bizGroup">
              <el-col :span="20"><el-input v-model="ruleForm.bizGroup" placeholder="请输入业务组" :disabled="true"></el-input></el-col>
          </el-form-item>
          <el-form-item label="用户名字">
              <el-autocomplete style="width: 80%"
                               v-model="realName"
                               :fetch-suggestions="querySearchAsync"
                               placeholder="请选择内容"
                               @select="handleSelect"
              ></el-autocomplete>
          </el-form-item>
      </el-form>
      <div slot="footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submitClick('ruleForm')" :loading="ui.submitLoading">新 增</el-button>
      </div>
  </el-dialog>
</template>

<script type="text/ecmascript-6">

  export default {
    name: 'AddDutyBizGroupDialog',
    props: {
      dialogVisible: {
        type: Boolean,
        required: true
      },
      bizGroup: {
        type: String
      },
    },
    data() {
      return {
        loginName: '',
        realName: '',
        ui: {
          submitLoading: false
        },
        visible: this.$props.dialogVisible,
        allUser: [],
        ruleForm: {
            bizGroup: '',
        },
      }
    },

    mounted: function () {

      this.$$get(`/user/select`)
        .then(res => {
          if (res.data.code < 1)
            throw res.data.msg;
          var array = [];
          res.data.data.forEach((item, index) => {
            array[index] = {value: item.userName, lable: item.loginName,}
          })
          this.allUser = array;
        })
        .catch(err => this.$$msg.err(err))
    },

      created() {
          this.ruleForm = {
              bizGroup: this.bizGroup,
          }
      },

    methods: {

      querySearchAsync(queryString, cb) {
        var allUser = this.allUser;
        var results = queryString ? allUser.filter(this.createStateFilter(queryString)) : allUser;
        cb(results);
      },

      handleSelect(item) {
        this.loginName = item.lable;
      },

      createStateFilter(queryString) {
        return (state) => {
          return (state.value.toLowerCase().search(queryString.toLowerCase()) >= 0);
        };
      },

      /** 提交点击 */
      async submitClick() {
        this.$$post('/group/insert', {
          bizGroup: this.ruleForm.bizGroup,
          loginName: this.loginName
        })
          .then(res => {
            if (res.data.code < 1) {
              this.$message.error(res.data.msg);
              return
            }
            this.$message({message: '新增成功', type: 'success'});
            this.handleClose();
            window.location.reload();
          })
          .catch(err => {
            this.showErrorAlert(err)
          })
      }
      ,

      /** 关闭处理 */
      handleClose() {
        this.visible = false;
        setTimeout(() => {
          this.$emit('update:dialogVisible', false)
        }, 300);
      }
    }
  }
</script>
