<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 disabled 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" disabled 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"
                   @input="changePhone"/>
        </div>
      </div>
      <div class="item">
        <span class="label"><span class="tips">*</span>所属区域</span>
        <div class="value">
          <AiAreaPicker v-model="form.areaId"  @select="areaSelect" :fullName.sync="form.areaName" mode="custom" all>
            <span class="label" v-if="form.areaName" >{{ form.areaName }}</span>
            <span v-else :class="form.areaName === '' ? 'color-999' : ''" >请选择</span>
            <u-icon name="arrow-right" color="#cccccc" size="24" style="margin-left:4px;"/>
          </AiAreaPicker>
        </div>
      </div>
      <div class="address">
        <span class="label"><span class="tips"></span>详细地址</span>
        <div class="value">
          <u-input type="textarea" placeholder="请输入" v-model="form.address"
                   placeholder-style="color:#999;font-size:16px;"  :maxlength="20" auto-height/>
        </div>
      </div>
      
      <div class="item">
        <span class="label"><span class="tips"></span>所属网格</span>
        <div class="value" @click="showGird=true">
          <span
              :class="form.girdName === '' ? 'color-999' : ''">{{
              $dict.getLabel('appSpecialTypeFive', form.girdName) || '请选择'
            }}</span>
          <u-icon name="arrow-right" color="#cccccc" size="24" style="margin-left:4px;"/>
        </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="item.maxlength" :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="24"/>
            </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="24"/>
            </div>
          </div>

        </div>
      </div>
    </div>
    <div class="footer" @click="submit">
      <div class="btn">{{ isEdit? '保存' : '提交'}}</div>
    </div>

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

<script>

import {mapState} from 'vuex'

export default {
  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',
              maxLength: 20
            },
            {
              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: 'income',
              maxLength: 20
            },
            {
              label: '发病日期',
              type: 'time',
              formDbName: 'sickTime'
            },
            {
              label: '监护人姓名',
              type: 'input',
              formDbName: 'helpName',
              maxLength: 20
            },
            {
              label: '监护人联系方式',
              type: 'input',
              formDbName: 'helpPhone',
              maxLength: 11
            },
            {
              label: '目前危险等级',
              type: 'select',
              dict: 'appSpecialDengerLevel',
              formDbName: 'level',
            },
            {
              label: '治疗情况',
              type: 'select',
              dict: 'appSpecialCure',
              formDbName: 'situation',
            },
          ]
        },
        {
          list: [  // 社区矫正人群
            {
              label: '具体罪名',
              type: 'input',
              formDbName: 'crime',
              maxLength: 20
            },
            {
              label: '矫正类别',
              type: 'select',
              dict: 'appSpecialChangeType',
              formDbName: 'type'
            },
            {
              label: '矫正开始日期',
              type: 'time',
              formDbName: 'startTime',
            },
            {
              label: '矫正结束日期',
              type: 'time',
              formDbName: 'endTime',
            },
            {
              label: '是否建立矫正小组',
              type: 'select',
              dict: 'yesOrNo',
              formDbName: 'isCreateGroup',
            },
            {
              label: '是否托管',
              type: 'select',
              dict: 'yesOrNo',
              formDbName: 'isRelease',
            }
          ]
        },
        {
          list: [  //  戒毒人员
            {
              label: '初次发现日期',
              type: 'time',
              formDbName: 'firstTime',
            },
            {
              label: '吸毒状态',
              type: 'select',
              dict: 'appSpecialDrug',
              formDbName: 'status',
            },
            {
              label: '戒毒情况',
              type: 'select',
              dict: 'appSpecialDebug',
              formDbName: 'debug',
            },
            {
              label: '管控情况',
              type: 'select',
              dict: 'appSpecialControl',
              formDbName: 'control',
            },
            {
              label: '管控人姓名',
              type: 'input',
              formDbName: 'controlName',
              maxLength: 20
            },
            {
              label: '管控人联系方式',
              type: 'input',
              formDbName: 'controlPhone',
              maxLength: 11
            },
            {
              label: '有无犯罪史',
              type: 'select',
              dict: 'isReflection',
              formDbName: 'crime'
            },
          ]
        },
        {
          list: [  // 刑满释放
            {
              label: '是否累犯',
              type: 'select',
              dict: 'yesOrNo',
              formDbName: 'isSecond'
            },
            {
              label: '原罪名',
              type: 'select',
              dict: 'appSpecialCrime',
              formDbName: 'crime'
            },
            {
              label: '服刑场所',
              type: 'input',
              formDbName: 'place',
              maxLength: 20
            },
            {
              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: 'yesOrNo',
              formDbName: 'isRepeat'
            },
          ]
        }
      ],
      selectList: [],
      id: "",
      index: "",
      showGird: false,
      girdlist: [],
      isEdit: false,
    }
  },
  computed: {
    ...mapState(['user']),

  },
  onLoad(o) {
    this.$dict.load('appSpecialSituation', 'appSpecialPlacement', 'appSpecialDenger', 'appSpecialCrime',
        'appSpecialControl', 'appSpecialDebug', 'appSpecialDrug', 'appSpecialChangeType', 'appSpecialCure', 'appSpecialDengerLevel',
        'appSpecialDisableLevel', 'appSpecialDisableType', 'appSpecialHealth', 'appSpecialMarriage', 'appSpecialTypeFive','isReflection','yesOrNo').then(() => {
    }),
    this.gridName()
    if (o) {
      this.isEdit = true
      this.id = o.id
      this.index = o.index
      this.getDetail()
    }
    if (!o.id) {
      this.isEdit = false
      this.getDetail()
    }

  },
  onShow() {
    document.title = '新增人员'
  },

  methods: {
    getDetail() {
      if (!this.id) return
      var urlList = ['app/appspecialdisabled/queryDetailById', 'app/appspecialmental/queryDetailById', 'app/appspecialadjustment/queryDetailById',
        'app/appspecialdrug/queryDetailById', 'app/appspecialprison/queryDetailById']

      this.$http.post(urlList[this.index], null, {
        params: {
          id: this.id
        }
      }).then((res) => {
        if (res.code == 0) {
          this.form = res.data
          this.form.userType = this.index
          this.$forceUpdate()
        }
      })
    },


    changeIdNumber() {
      if (this.form.idNumber.length != 18) {
        return this.$u.toast('请输入正确的身份证号码')
      } else {
        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('请输入正确的身份证号码')
        }
      }
    },

    changePhone() {
      if (this.form.phone.length != 11) {
        return this.$u.toast('请输入正确的联系电话')
      }
    },
    confirmGirdSelect(e) {
      this.form.girdId = e[0].value
      this.form.girdName = e[0].label
    },

    gridName() {
      this.$http.post('app/appgirdmemberinfo/queryMyGirdListByLevel2AndUser').then(res => {
        if (res.code == 0) {
          var list = []
          res.data.map((e, index) => {
            list.push({dictName: e.girdName, dictValue: index})
          })
          this.girdlist = list
        }
      })
    },

    submit() {
      if (this.form.userType === '') {
        return this.$u.toast('请选择类型')
      }
      if (!this.form.name) {
        return this.$u.toast('请输入姓名')
      }
      if (!this.form.idNumber) {
        return this.$u.toast('请输入身份证号')
      }
      if (
        !/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(this.form.idNumber)
      ) {
        return this.$u.toast("请输入正确的身份证账号");
      }
      if (!this.form.phone) {
        return this.$u.toast('请输入联系电话')
      }
      if (!/^1[0-9]{10,10}$/.test(this.form.phone)) {
        return this.$u.toast("请输入正确的联系电话");
      }
      if (!this.form.areaName) {
        return this.$u.toast('请选择区域')
      }

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

      this.$http.post(urlList[this.form.userType], {
        ...this.form,
        id: this.isEdit ? this.id : ''
        }).then(res => {
        if(res.code == 0) {
          this.$u.toast(this.isEdit ? '信息保存成功' : '信息提交成功')
          uni.$emit('specialPeopleList')
          setTimeout(() => {
            uni.navigateBack()
          }, 600)
          this.isEdit = !this.isEdit
        } else if(res.code == 1) {
          this.$u.toast('保存失败,信息未填写')
        } else {
          this.$confirm('是否放弃编辑当前信息').then(()=>{
            uni.navigateBack()
          })
        }
      })
    },
    areaSelect(e){
      this.form.areaId = e
    },

    selectClick(dictName, formName) {
      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}`
    }
  }
}
</script>

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

  .item {
    width: 100%;
    padding: 34px 36px 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 {
      width: 60%;
      display: flex;
      justify-content: flex-end;
      .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;
    }
  }

  .address {
    font-size: 32px;
    font-family: PingFangSC-Regular, PingFang SC;
    color: #333;
    background: #FFFFFF;
    border-bottom: 1px solid #D8DDE6;
    padding: 34px 36px 34px 0;
    box-sizing: border-box;

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

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

  .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>
