<template>
  <div class="AddUser">
    <div class="form-item__group">
      <div class="form-item">
        <div class="form-item__wrapper">
          <div class="form-item__title">
            <i>*</i>
            <h2>姓名</h2>
          </div>
          <div class="form-item__right">
            <input placeholder="请输入" v-model="form.name" :maxlength="20"/>
          </div>
        </div>
      </div>
      <div class="form-item">
        <div class="form-item__wrapper">
          <div class="form-item__title">
            <i>*</i>
            <h2>身份证号</h2>
          </div>
          <div class="form-item__right">
            <input placeholder="请输入" type="idcard" v-model="form.idNumber" :maxlength="18"/>
          </div>
        </div>
      </div>
      <div class="form-item">
        <div class="form-item__wrapper">
          <div class="form-item__title">
            <i>*</i>
            <h2>手机号码</h2>
          </div>
          <div class="form-item__right">
            <input placeholder="请输入" type="number" v-model="form.phone" :maxlength="11"/>
          </div>
        </div>
      </div>
      <div class="form-item">
        <div class="form-item__wrapper">
          <div class="form-item__title">
            <i>*</i>
            <h2>上报地区</h2>
          </div>
          <div class="form-item__right">
            <AiAreaPicker class="ai-area" :areaId="$areaId" v-model="form.areaId" :fullName.sync="form.areaName">
              <div class="ai-area__wrapper">
                <span class="label" v-if="form.areaName">{{ form.areaName }}</span>
                <i v-else>请选择</i>
                <u-icon name="arrow-right" color="#ddd"/>
              </div>
            </AiAreaPicker>
          </div>
        </div>
      </div>
      <div class="form-item form-item__textarea">
        <div class="form-item__wrapper">
          <div class="form-item__title">
            <i>*</i>
            <h2>详细地址</h2>
          </div>
          <div class="form-item__right">
            <textarea auto-height v-model="form.address" :maxlength="500" placeholder="请输入详细地址"
                      placeholder-style="font-size: 16px;"></textarea>
          </div>
        </div>
      </div>
    </div>
    <div class="btn-wrapper">
      <div class="btn" hover-class="text-hover" @click="submit">提交</div>
    </div>
  </div>
</template>

<script>
import {mapState} from 'vuex'

export default {
  appName: "添加上报人员",
  data() {
    return {
      form: {
        name: '',
        idNumber: '',
        phone: '',
        areaId: '',
        areaName: '',
        address: '',
      },
      $areaId: '',
      flag: false
    }
  },
  computed: {
    ...mapState(['user'])
  },

  methods: {
    submit() {
      if (!this.form.name) {
        return this.$toast('请输入上报人姓名')
      }

      if (!this.form.idNumber) {
        return this.$toast('请输入上报人身份证号')
      }

      if (!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(this.form.idNumber)) {
        return this.$toast('请输入正确的身份证账号')
      }
      if (!this.form.phone) {
        return this.$toast('请选择上报人手机号码')
      }

      if (!this.form.phone) {
        return this.$toast('请输入正确的手机号码')
      }

      if (!this.form.areaId) {
        return this.$toast('请选择上报地区')
      }

      if (this.form.areaId.substr(this.form.areaId.length - 3, 3) === '000') {
        return this.$toast('上报地区必须选到村或社区')
      }

      if (!this.form.address) {
        return this.$toast('请输入详细地址')
      }
      if (this.flag) return
      this.flag = true

      this.$loading()
      this.$instance.post(`/app/appepidemicpreventionhealthreportinfo/addReportUser`, {
        ...this.form,
      }).then(res => {
        this.$hideLoading()
        this.flag = false
        if (res.code == 0) {
          this.$toast('提交成功')
          uni.$emit('update')
          setTimeout(() => {
            uni.redirectTo({
              url: `./Result?id=${res.data.id}&name=${res.data.name}&idNumber=${res.data.idNumber}`
            })
          }, 400)
        }
      })
    }
  }
}
</script>

<style lang="scss">
.AddUser {
  padding-bottom: 140px;

  .form-item__group {
    margin-bottom: 24px;
    background: #fff;
  }

  .form-item {
    padding-left: 32px;

    .form-item__checkbox {
      width: 100%;

      div {
        width: 100%;
        height: 80px;
        line-height: 80px;
        margin-bottom: 24px;
        text-align: center;
        background: #FFFFFF;
        border-radius: 16px;
        color: #333333;
        font-size: 28px;
        border: 1px solid #CCCCCC;

        &.active {
          background: #4181FF;
          color: #fff;
          border-color: #4181FF;
        }
      }
    }

    .form-item__radio {
      display: flex;
      align-items: center;
      flex-wrap: wrap;

      div {
        width: 212px;
        height: 80px;
        line-height: 80px;
        margin-right: 16px;
        margin-bottom: 8px;
        text-align: center;
        background: #FFFFFF;
        border-radius: 16px;
        color: #333333;
        font-size: 28px;
        border: 1px solid #CCCCCC;

        &:nth-of-type(3n) {
          margin-right: 0;
        }

        &.active {
          background: #4181FF;
          color: #fff;
          border-color: #4181FF;
        }
      }
    }

    .ai-area__wrapper {
      display: flex;
      align-items: center;
      padding-left: 100px;

      span {
        color: #333;
        font-size: 30px;
      }

      i {
        color: #999;
        font-size: 30px;
      }

      image {
        width: 16px;
        height: 8px;
      }
    }

    .form-item__wrapper {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 128px;
      padding-right: 28px;
      border-bottom: 1px solid #DDDDDD;

      input {
        flex: 1;
        height: 100%;
        text-align: right;
        color: #333;
        padding-right: 10px;
      }

      .form-item__right {
        display: flex;
        align-items: center;
        font-size: 32px;

        span {
          max-width: 400px;
          margin-right: 8px;
          color: #333333;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }

        i {
          margin-right: 8px;
          color: #999999;
        }
      }
    }

    &:last-child {
      .form-item__wrapper {
        border-bottom: none;
      }
    }

    .form-item__title {
      display: flex;
      align-items: center;

      i {
        font-size: 32px;
        color: #FF4466;
      }

      span {
        font-size: 28px;
        color: #999999;
      }

      h2 {
        padding: 0 4px;
        font-weight: 600;
        font-size: 32px;
        color: #333333;
      }
    }


    &.form-item__imgs, &.form-item__textarea {
      .form-item__wrapper {
        display: block;
        height: auto;
        padding-bottom: 32px;
      }

      textarea {
        width: 100%;
        height: 90px;
      }

      .form-item__title {
        padding: 32px 0;
      }

      .form-item__right {
        padding-left: 18px;
      }
    }
  }
}
</style>
