<template>
  <div class="add">
    <div class="pad-l32">
      <div class="item">
        <span class="label"><span class="tips">*</span>类型</span>
        <div class="value" @click="selectClick('appSpecialTypeFive', 'userType')">
          <span :class="form.userType === '' ? 'color-999' : ''">{{ $dict.getLabel('appSpecialTypeFive', form.userType) || '请选择'}}</span>
          <u-icon name="arrow-right" color="#cccccc" size="14"/>
        </div>
      </div>
      <div class="item">
        <span class="label"><span class="tips">*</span>姓名</span>
        <div class="value">

          <u-input type="text" placeholder="请输入" v-model="form.name" input-align="right"  placeholder-style="color:#999;font-size:16px;" height="48" :maxlength="15" :clearable="false" />
        </div>
      </div>
      <div class="item">
        <span class="label"><span class="tips">*</span>身份证号</span>
        <div class="value">
          <u-input type="text" placeholder="请输入" v-model="form.idNumber" input-align="right"  placeholder-style="color:#999;font-size:16px;" height="48" :maxlength="18" :clearable="false" @input="changeIdNumber"/>
        </div>
      </div>
      <div class="item">
        <span class="label"><span class="tips">*</span>性别</span>
        <div class="value">
          <u-input  placeholder="请输入" v-model="form.gender" input-align="right"  placeholder-style="color:#999;font-size:16px;" height="48" :maxlength="15" :clearable="false" />
        </div>
      </div>
      <div class="item">
        <span class="label"><span class="tips">*</span>出生日期</span>
        <div class="value">

          <u-input type="text" placeholder="请输入" v-model="form.birth" input-align="right"  placeholder-style="color:#999;font-size:16px;" height="48" :maxlength="19" :clearable="false" />
        </div>
      </div>
      <div class="item">
        <span class="label"><span class="tips">*</span>联系电话</span>
        <div class="value">
          <u-input type="number" placeholder="请输入" v-model="form.phone" input-align="right"  placeholder-style="color:#999;font-size:16px;" height="48" :maxlength="11" :clearable="false" />
        </div>
      </div>
      <div class="item">
        <span class="label"><span class="tips">*</span>所属区域</span>
        <div class="value">
          <AiAreaPicker v-model="form.areaId" :areaId="user.areaId" @select="areaSelect" :name.sync="form.areaName">
            <span class="label" v-if="form.areaName">{{ form.areaName }}</span>
            <span v-else>请选择</span>
            <u-icon name="arrow-right" color="#666" size="24" style="margin-left:4px;"/>
          </AiAreaPicker>
        </div>
      </div>
      <div class="item">
        <span class="label"><span class="tips">*</span>详细地址</span>
        <div class="value">
          <u-input type="text" placeholder="请输入" v-model="form.address" input-align="right"  placeholder-style="color:#999;font-size:16px;" height="48"/>
        </div>
      </div>
      <div class="item">
        <span class="label"><span class="tips">*</span>所属网格</span>
        <div class="value">
          <u-input type="text" placeholder="请输入" v-model="form.girdName" input-align="right"  placeholder-style="color:#999;font-size:16px;" height="48" :clearable="false"/>
        </div>
      </div>
      <div v-if="this.form.userType != ''">
        <div v-for="(item, index) in tableData[this.form.userType].list" :key="index">

          <div class="item" v-if="item.type == 'input'">
            <span class="label"><span class="tips"></span>{{item.label}}</span>
            <div class="value">
              <u-input type="text" placeholder="请输入" v-model="form[item.formDbName]" input-align="right"  placeholder-style="color:#999;font-size:16px;" height="48" :maxlength="15" :clearable="false" />
            </div>
          </div>

          <div class="item" v-if="item.type == 'select'">
            <span class="label"><span class="tips"></span>{{item.label}}</span>
            <div class="value" @click="selectClick(item.dict, item.formDbName)">
              <span :class="form[item.formDbName] === '' ? 'color-999' : ''">{{ $dict.getLabel(item.dict, form[item.formDbName]) || '请选择'}}</span>
              <u-icon name="arrow-right" color="#cccccc" size="14"/>
            </div>
          </div>

          <div class="item" v-if="item.type == 'time'">
            <span class="label"><span class="tips"></span>{{item.label}}</span>
            <div class="value" @click="selectTime(item.formDbName)">
              <span :class="form[item.formDbName] === '' ? 'color-999' : ''">{{ $dateFormat(form[item.formDbName],'YYYY-MM-DD') || '请选择' }}</span>
              <u-icon name="arrow-right" color="#cccccc" size="14"/>
            </div>
          </div>

        </div>
      </div>
    </div>
    <div class="footer" @click="submit">
      <div class="btn">保存</div>
    </div>

    <u-picker mode="time" v-model="dateShow" :params="deteParams" start-year="2010" @confirm="dateConfirm">请选择</u-picker>
    <u-select v-model="showSelect" :list="selectList" label-name="dictName" value-name="dictValue" @confirm="confirmSelect"/>
  </div>
</template>

<script>

import {mapActions, mapState} from 'vuex'
import AiAreaPicker from '../../components/AiAreaPicker.vue'

export default {
  components: { AiAreaPicker },

  data() {
    return {
      form: {
        userType: '',
        name: '',
        idNumber: '',
        gender: '',
        birth: '',
        phone: '',
        areaName: '',
        address: '',
        girdName: '',
        areaId: '',
        sex: ''
      },
      dateShow: false,
      deteParams: {year: true, month: true, day: true, hour: false, minute: false, second: false},
      showSelect: false,
      selectDict: '',
      selectName: '',
      tableIndex: 0,
      tableData: [
        {
          list: [  // 精神病人
            {
              label: '家庭年收入',
              type: 'input',
              formDbName: 'income'
            },
            {
              label: '发病日期',
              type: 'time',
              formDbName: 'sickTime'
            },
            {
              label: '监护人姓名',
              type: 'input',
              formDbName: 'helpName'
            },
            {
              label: '监护人联系方式',
              type: 'input',
              formDbName: 'helpPhone',
            },
            {
              label: '目前危险等级',
              type: 'select',
              dict: 'appSpecialDengerLevel',
              formDbName: 'level',
            },
            {
              label: '治疗情况',
              type: 'select',
              dict: 'appSpecialCure',
              formDbName: 'situation',
            },
          ]
        },
        {
          list: [   // 残疾人
            {
              label: '家庭年收入（万）',
              type: 'input',
              formDbName: 'income'
            },
            {
              label: '婚姻情况',
              type: 'select',
              dict: 'appSpecialMarriage',
              formDbName: 'marriage'
            },
            {
              label: '身体状况',
              type: 'select',
              dict: 'appSpecialHealth',
              formDbName: 'health'
            },
            {
              label: '残疾类型',
              type: 'select',
              dict: 'appSpecialDisableType',
              formDbName: 'type'
            },
            {
              label: '残疾级别',
              type: 'select',
              dict: 'appSpecialDisableLevel',
              formDbName: 'level'
            },
          ]
        },
        {
          list: [  // 社区矫正人群
            {
              label: '具体罪名',
              type: 'input',
              formDbName: 'crime'
            },
            {
              label: '矫正类别',
              type: 'select',
              dict: 'appSpecialChangeType',
              formDbName: 'type'
            },
            {
              label: '矫正开始日期',
              type: 'time',
              formDbName: 'startTime',
            },
            {
              label: '矫正结束日期',
              type: 'time',
              formDbName: 'endTime',
            },
            {
              label: '是否建立矫正小组',
              type: 'select',
              dict: 'isCreateGroup',
              formDbName: 'isCreateGroup',
            },
            {
              label: '是否托管',
              type: 'select',
              dict: 'isRelease',
              formDbName: 'isRelease',
            }
          ]
        },
        {
          list: [  //  戒毒人员
            {
              label: '初次发现日期',
              type: 'time',
              formDbName: 'firstTime',
            },
            {
              label: '吸毒状态',
              type: 'select',
              dict: 'appSpecialDrug',
              formDbName: 'status',
            },
            {
              label: '戒毒情况',
              type: 'select',
              dict: 'appSpecialDebug',
              formDbName: 'houselivingStatus',
            },
            {
              label: '管控情况',
              type: 'select',
              dict: 'appSpecialControl',
              formDbName: 'control',
            },
            {
              label: '管控人姓名',
              type: 'input',
              formDbName: 'controlName'
            },
            {
              label: '管控人联系方式',
              type: 'input',
              formDbName: 'controlPhone'
            },
            {
              label: '有无犯罪史',
              type: 'select',
              dict: 'houselivingStatus',
              formDbName: 'crime'
            },
          ]
        },
        {
          list: [  // 刑满释放
            {
                label: '是否累犯',
                type: 'select',
                dict: 'houselivingStatus',
                formDbName: 'isSecond'
            },
            {
              label: '原罪名',
              type: 'select',
              dict: 'appSpecialCrime',
              formDbName: 'crime'
            },
            {
              label: '服刑场所',
              type: 'input',
              formDbName: 'place'
            },
            {
              label: '危险性评估',
              type: 'select',
              dict: 'appSpecialDenger',
              formDbName: 'denger'
            },
            {
              label: '安置情况',
              type: 'select',
              dict: 'appSpecialPlacement',
              formDbName: 'placement'
            },
            {
              label: '安置时间',
              type: 'time',
              formDbName: 'placeTime'
            },
            {
              label: '衔接情况',
              type: 'select',
              dict: 'appSpecialSituation',
              formDbName: 'situation'
            },
            {
              label: '衔接时间',
              type: 'time',
              formDbName: 'situationTime'
            },
            {
              label: '是否重新犯罪',
              type: 'select',
              dict: 'houselivingStatus',
              formDbName: 'isRepeat'
            },
          ]
        }
      ],
      selectList: [],
    }
  },
  computed: {
    ...mapState(['user']),
  },
  created() {
    this.$dict.load('appSpecialSituation', 'appSpecialPlacement', 'appSpecialDenger', 'appSpecialCrime',
        'appSpecialControl', 'appSpecialDebug', 'appSpecialDrug', 'appSpecialChangeType','appSpecialCure','appSpecialDengerLevel',
        'appSpecialDisableLevel','appSpecialDisableType','appSpecialHealth','appSpecialMarriage','appSpecialTypeFive').then(() => {
    })
  },
  onShow() {
    document.title = '新增人员'
  },

  methods: {
    changeIdNumber() {
      if(this.form.idNumber.length == 18){
          var people = this.$idCardNoUtil.getIdCardInfo(this.form.idNumber)
          this.form.birth = people.birthday
          this.form.gender = people.gender
        
          let reg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
          if (!reg.test(this.form.idNumber)) {
            return this.$u.toast('请输入正确的身份证号码')
          }
      }
      
    },
    submit() {
      if(this.userType === ''){
        return this.$u.toast('请选择类型')
      }
      if(!this.form.name){
        return this.$u.toast('请输入姓名')
      }
      if(!this.form.idNumber){
        return this.$u.toast('请输入身份证号')
      }
      if(!this.form.phone){
        return this.$u.toast('请输入联系电话')
      }
      if(!this.form.areaName){
        return this.$u.toast('请选择区域')
      }
      if(!this.form.address){
        return this.$u.toast('请输入详细地址')
      }
      if(!this.form.girdName){
        return this.$u.toast('请选择网格')
      }

      var urlList = ['app/appspecialmental/addOrUpdate', 'app/appspecialdisabled/addOrUpdate', 'app/appspecialdisabled/addOrUpdate',
       'app/appspecialdisabled/addOrUpdate', 'app/appspecialdisabled/addOrUpdate']

      this.$http.post(urlList[this.form.userType], this.form).then(res => {
        if(res.code == 0) {
          this.$u.toast('提交成功')
          uni.$emit('specialPeopleList')
          setTimeout(() => {
            uni.navigateBack()
          }, 600)
        }
      })
    },
    areaSelect(e){
      console.log(e);
      this.form.areaId = e
    },

    selectClick(dictName, formName) {
      console.log(dictName)
      this.selectList = this.$dict.getDict(dictName)
      this.selectName = formName
      this.showSelect = true
    },
    confirmSelect(e) {
      this.form[this.selectName] = e[0].value

      if(this.selectName == 'userType'){
        this.tableData[this.form.userType].list.map((item) => {
          this.form[item.formDbName] = ''
        })
      }
    },
    selectTime(formName) {
      this.selectName = formName
      this.dateShow = true
    },
    dateConfirm(e) {
      this.form[this.selectName] = `${e.year}-${e.month}-${e.day} 00:00:00`
    }
  }
}
</script>

<style lang="scss" scoped>
.add {
  padding-bottom: 112px;

  .item {
    width: 100%;
    padding: 34px 32px 34px 0;
    background: #FFFFFF;
    border-bottom: 1px solid #D8DDE6;
    display: flex;
    justify-content: space-between;
    font-size: 32px;
    font-family: PingFangSC-Regular, PingFang SC;
    color: #333;
    line-height: 44px;
    box-sizing: border-box;

    .color-999 {
      color: #999;
    }

    .value {
      .u-icon {
        margin-left: 16px;
      }
    }

    .tips{
      display: inline-block;
      width: 16px;
      font-size: 32px;
      font-family: PingFangSC-Regular, PingFang SC;
      color: #F46;
      line-height: 44px;
    }
  }

  .footer {
    width: 100%;
    position: fixed;
    bottom: 0;
    left: 0;
  }

  .btn {
    width: 100%;
    height: 112px;
    line-height: 112px;
    text-align: center;
    background: #1365DD;
    font-size: 32px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #FFF;
  }

  .line-bg {
    width: 100%;
    height: 16px;
    background: #F5F5F5;
  }

  .pad-l32 {
    padding-left: 32px;
    background-color: #fff;
  }
}
</style>
