<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>帐户名称：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入帐户名称，只限英文"
                v-model="postData.UserName"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h" style="padding: 0;margin: 0;">
          <span></span>
          <div class="flex-item">
            <div style="min-height: 0;">
              <span style="font-size:10px;color:#bbb;">默认密码：123456</span>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>用户头像：</span>
          <div class="flex-item">
            <div>
              <el-upload
                class="avatar-uploader"
                action='string'
                :http-request="toolClass.upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess">
                <img v-if="postData.Headimgurl" :src="postData.Headimgurl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>真实姓名：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入真实姓名"
                v-model="postData.RealName"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>邮箱：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入邮箱"
                v-model="postData.Email"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>工号：</span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入工号"
                v-model="postData.WorkNo"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>手机号：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入手机号"
                v-model="postData.Phone"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>用户标签：</span>
          <div class="flex-item">
            <div style="width:50%">
              <el-checkbox-group v-model="TagIds" @change="handleCheckedCitiesChange">
                <el-checkbox v-for="(item,index) in TagList" :label="item.TagId" :key="index">{{item.TagName}}</el-checkbox>
              </el-checkbox-group>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>用户类型：</span>
          <div class="flex-item">
            <div style="width:50%">
              <el-select v-model="postData.TypeId" placeholder="请选择">
                <el-option
                  v-for="item in typeList"
                  :key="item.TypeId"
                  :label="item.TypeName"
                  :value="item.TypeId">
                </el-option>
              </el-select>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>选择案场：</span>
          <div class="flex-item">
            <el-select v-model="CaseId" placeholder="请选择">
              <el-option
                v-for="item in cases"
                :key="item.CaseId"
                :label="item.CaseName"
                :value="item.CaseId">
              </el-option>
            </el-select>
          </div>
        </li>
        <li style="text-align:center">
          <el-button type="primary" size="mini" @click="submit">保存</el-button>
          <el-button type="danger" size="mini" @click="cancel">取消</el-button>
        </li>
      </ul>
    </form>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: '',
  data () {
    return {
      TagIds: [],
      typeList: [],
      TagList: [],
      CheckPwd: '',
      postData: {
        UserId: '', // 用户id
        UserName: '', // 用户名
        RealName: '', // 真实姓名
        Pwd: '', // 密码
        OrgId: '', // 机构id
        Sex: '', // 性别
        WorkNo: '', // 工号
        Email: '', // 邮箱
        Phone: '', // 手机号
        Headimgurl: '', // 头像
        TypeId: '', // 用户类型id
        TagIds: '', // 用户标签
        BelongCaseId: '', // 所属案场id
      }
    }
  },
  components: {},
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default,
    }),
    CaseId: {
      get () {
        return this.postData.BelongCaseId || this.defaultCaseId
      },
      set (val) {
        this.postData.BelongCaseId = val
      }
    }
  },
  created () {
    this.getCaseTagList()
    this.getUserTypeList()
  },
  methods: {
    getCaseTagList () { // 获取标签数据
      this.$ajax(this.$api.caseManager.getCaseTagList.url, {
        method: this.$api.caseManager.getCaseTagList.method,
        queryData: { caseid: this.CaseId, page: 1, pagesize: 10000 }
      }).then(res => {
        this.TagList = res.list
      })
    },
    getUserTypeList () { // 获取用户类型
      this.$ajax(this.$api.caseManager.getUserTypeList.url, {
        method: this.$api.caseManager.getUserTypeList.method,
      }).then(res => {
        this.typeList = res
        this.postData.TypeId = res[0].TypeId
      })
    },
    handleCheckedCitiesChange (val) {
      this.postData.TagIds = val.join(',')
    },
    handleAvatarSuccess (res, file) {
      this.postData.Headimgurl = res.result.url
    },
    submit () { // 提交数据
      if (this.postData.UserName === '') {
        this.$message({
          type: 'error',
          message: '帐户名称不能为空'
        })
        return false
      }
      var re = new RegExp('^[a-zA-Z0-9]+$')
      if (!re.test(this.postData.UserName)) {
        this.$message({
          type: 'error',
          message: '帐户名称必须为英文或数字'
        })
        return false
      }
      if (this.postData.RealName === '') {
        this.$message({
          type: 'error',
          message: '真实姓名不能为空'
        })
        return false
      }
      if (this.postData.Phone === '') {
        this.$message({
          type: 'error',
          message: '手机号不能为空'
        })
        return false
      }
      var myreg = /^[1][3,4,5,6,7,8][0-9]{9}$/
      if (!myreg.test(this.postData.Phone)) {
        this.$message({
          type: 'error',
          message: '请输入正确的手机号！'
        })
        return false
      }
      if (this.postData.Email !== '') {
        var emailreg = new RegExp('^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$')
        if (!emailreg.test(this.postData.Email)) {
          this.$message({
            type: 'error',
            message: '邮箱格式不正确！'
          })
          return false
        }
      }
      if (this.postData.BelongCaseId === '') this.postData.BelongCaseId = this.CaseId
      this.$ajax(this.$api.systemSet.addUser.url, {
        method: this.$api.systemSet.addUser.method,
        data: this.postData
      }).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        this.$router.push({name: 'userManager'})
      })
    },
    cancel () {
      this.$router.go(-1)
    }
  },
  mounted () { }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
