<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>用户类型：<em>*</em></span>
          <div class="flex-item">
            <el-select v-model="postData.UserType" placeholder="请选择">
              <el-option
                v-for="item in typeList"
                :key="item.TypeCode"
                :label="item.TypeName"
                :value="item.TypeCode">
              </el-option>
            </el-select>
          </div>
        </li>
        <li class="flex-h">
          <span>用户姓名：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入名称"
                v-model="postData.UserName"
                maxlength="10"
                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.Tel"
                maxlength="11"
                clearable>
              </el-input>
            </div>
          </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 {
      postData: {
        CaseUserId: '', // 主键id
        OrgId: '', // 机构id
        CaseId: this.$route.query.id, // 案场id
        UserName: '', // 用户名称
        Tel: '', // 电话
        UserType: '', // 用户类型
      },
      typeList: [],
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default,
      OrgId: x => x.app.user.OrgId,
    }),
    CaseId: {
      get () {
        return this.postData.caseid || this.defaultCaseId
      },
      set (val) {
        this.postData.caseid = val
      }
    }
  },
  created () {
    this.getUserTypeList()
    this.getCaseUserInfo()
  },
  components: {},
  methods: {
    getCaseUserInfo () {
      if (this.$route.query.userid !== undefined) {
        this.$ajax(this.$api.caseManager.getCaseUserInfo.url, {
          method: this.$api.caseManager.getCaseUserInfo.method,
          urlData: {id: this.$route.query.userid}
        }).then(res => {
          this.postData = res
        })
      }
    },
    getUserTypeList () { // 获取用户类型
      this.$ajax(this.$api.caseManager.getCaseUserType.url, {
        method: this.$api.caseManager.getCaseUserType.method,
      }).then(res => {
        this.typeList = res
        this.postData.UserType = res[0].TypeCode
      })
    },
    submit () { // 提交数据
      if (this.postData.UserName === '') {
        this.$message({
          type: 'error',
          message: '用户名称不能为空'
        })
        return false
      }
      if (this.postData.Tel === '') {
        this.$message({
          type: 'error',
          message: '手机号码不能为空'
        })
        return false
      }
      var myreg = /^[1][3,4,5,6,7,8][0-9]{9}$/
      if (!myreg.test(this.postData.Tel)) {
        this.$message({
          type: 'error',
          message: '请输入正确的手机号！'
        })
        return false
      }
      this.postData.OrgId = this.OrgId
      if (this.$route.query.userid !== undefined) {
        this.$ajax(this.$api.caseManager.editCaseUser.url, {
          method: this.$api.caseManager.editCaseUser.method,
          data: this.postData
        }).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功'
          })
          this.$router.push({ name: 'pplConcerned', query: { id: this.$route.query.id } })
        })
      } else {
        this.$ajax(this.$api.caseManager.addCaseUser.url, {
          method: this.$api.caseManager.addCaseUser.method,
          data: this.postData
        }).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功'
          })
          this.$router.push({ name: 'pplConcerned', query: { id: this.$route.query.id } })
        })
      }
    },
    cancel () { // 取消
      this.$router.push({ name: 'pplConcerned', query: { id: this.$route.query.id } })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
@import "page.scss";
</style>
