<template>
  <section class="authApply">
    <div class="card">
      <div class="item">
        <div class="label required" v-text="`是否户主`"/>
        <div class="flex">
          <div class="radioBtn" v-for="op in ownerOps" :key="op.dictValue" v-text="op.dictName"
               :class="{current:op.dictValue==form.householdName}" @click="form.householdName=op.dictValue"/>
        </div>
      </div>
      <template v-if="form.householdName==0">
        <div class="flex item">
          <div class="label required" required v-text="`户主身份证号`"/>
          <u-input v-model="form.householdIdNumber" input-align="right" maxlength="18"/>
        </div>
        <div class="flex item">
          <div class="label required" required v-text="`与户主关系`"/>
          <AiSelect v-model="form.householdRelation" dict="householdRelation"/>
        </div>
      </template>
    </div>
    <div class="card">
      <view class="item flex spb" v-for="row in baseInfo" :key="row.prop">
        <span class="label" :class="{required:row.required}" v-text="row.label"/>
        <AiSelect v-if="row.dict" v-model="form[row.prop]" :dict="row.dict" :disabled="row.disabled"/>
        <AiDate v-else-if="row.type=='time'" @change="v=>$set(form,row.prop,v)" :value="form[row.prop]"/>
        <u-input v-else-if="row.type=='idNumber'" v-model="form[row.prop]" input-align="right" maxlength="18"/>
        <u-input v-else-if="row.type=='phone'" v-model="form[row.prop]" input-align="right" maxlength="11"/>
        <u-input v-else v-model="form[row.prop]" input-align="right"/>
      </view>
    </div>
    <div class="card">
      <view class="item" :class="{spb:!row.topLabel,flex:!row.topLabel}" v-for="row in contract" :key="row.prop">
        <span class="label" :class="{required:row.required}" v-text="row.label"/>
        <AiAreaPicker v-if="row.type=='area'" @input="v=>$set(form,row.prop,v)" :fullName.sync="form.currentAreaName" :selectRoot="false">
          <u-icon name="arrow-right" :label="form.currentAreaName||'请选择'" label-pos="left" color="#ddd"/>
        </AiAreaPicker>
        <textarea v-else v-model="form[row.prop]" input-align="right" placeholder="请输入" placeholder-style="color:#999"
                  maxlength="50"/>
      </view>
    </div>
    <div class="card">
      <view class="item" :class="{spb:!row.topLabel,flex:!row.topLabel}" v-for="row in household" :key="row.prop">
        <span class="label" :class="{required:row.required}" v-text="row.label"/>
        <AiAreaPicker v-if="row.prop=='householdAreaId'" all @input="v=>$set(form,row.prop,v)" v-model="form[row.prop]"
                      :fullName.sync="form.householdAreaName" valueLevel="5" isForm :selectRoot="false"/>
        <textarea v-else v-model="form[row.prop]" input-align="right" placeholder="请输入" placeholder-style="color:#999"
                  maxlength="50"/>
      </view>
    </div>
    <div class="card">
      <view class="item flex spb" v-for="row in extra" :key="row.prop">
        <span class="label" :class="{required:row.required}" v-text="row.label"/>
        <AiSelect v-if="row.dict" v-model="form[row.prop]" :dict="row.dict"/>
        <AiDate v-else-if="row.type=='time'" v-model="form[row.prop]"/>
        <AiAreaPicker v-else-if="row.type=='area'" all v-model="form[row.prop]"
                      :fullName.sync="form.birthplaceAreaName">
          <u-icon name="arrow-right" :label="form.birthplaceAreaName||'请选择'" label-pos="left" color="#ddd"/>
        </AiAreaPicker>
        <u-input v-else v-model="form[row.prop]" input-align="right"/>
      </view>
    </div>
    <div class="fixed-bottom">
      <u-button type="primary" @click="submit">
        <text style="font-size: 16px" v-text="`提交`"/>
      </u-button>
    </div>
  </section>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "authApply",
  appName: "居民档案申请",
  computed: {
    ...mapState(['user'])
  },
  data() {
    let required = true
    return {
      ownerOps: [],
      form: {
        householdName: "1",
        currentAreaName: "",
        householdAreaName: "",
        birthplaceAreaName: ""
      },
      baseInfo: [
        {label: "姓名", prop: "name", required},
        {label: "身份证号", prop: "idNumber", required, type: "idNumber"},
        {label: "性别", prop: "sex", required, dict: "sex", disabled: true},
        {label: "出生日期", prop: "birthDate", required, type: "time", disabled: true},
        {label: "联系方式", prop: "phone", type: "phone"},
      ],
      contract: [
        {label: "现住址", prop: "currentAreaId", type: "area", required},
        {label: "详细住址", prop: "currentAddress", type: 'textarea', topLabel: true},
      ],
      household: [
        {label: "户籍地址", prop: "householdAreaId", type: "area"},
        {label: "详细地址", prop: "householdAddress", type: 'textarea', topLabel: true},
      ],
      extra: [
        {label: "籍贯", prop: "birthplaceAreaId", type: "area"},
        {label: "民族", prop: "nation", dict: "nation"},
        {label: "文化程度", prop: "education", dict: "education"},
        {label: "婚姻状况", prop: "maritalStatus", dict: "maritalStatus"},
        {label: "政治面貌", prop: "politicsStatus", dict: "politicsStatus"},
        {label: "兵役状况", prop: "militaryStatus", dict: "militaryStatus"},
        {label: "宗教信仰", prop: "faithType", dict: "faithType"},
        {label: "职业", prop: "job", dict: "job"},
      ]
    }
  },
  onLoad() {
    this.$dict.load("yesOrNo", "householdRelation").then(() => {
      this.ownerOps = this.$dict.getDict("yesOrNo")
    })
    this.getResident()
  },
  watch: {
    'form.idNumber'(v) {
      let {birthday: birthDate, sex} = this.$idCardNoUtil.getIdCardInfo(v),
          age = this.$calcAge(v)
      this.form = {...this.form, birthDate, sex, age}
    }
  },
  methods: {
    getResident() {
      let {residentId: id} = this.user
      if (id) {
        this.$instance.post("/app/appresident/detailForWx", null, {
          params: {id}
        }).then(res => {
          if (res?.data) {
            this.form = res.data.resident
          }
        })
      } else if (uni.getStorageSync("authForm")) {
        let info = uni.getStorageSync("authForm"),
            {birthday: birthDate, sex} = this.$idCardNoUtil.getIdCardInfo(info.idNumber)
        uni.removeStorageSync("authForm")
        this.form = {...this.form, ...info, birthDate, sex}
      }
    },
    submit() {
      let {baseInfo, contract, household, extra, form: {householdName, householdIdNumber, householdRelation}} = this
      if (householdName == 0) {
        if (!householdIdNumber) return this.$u.toast("请输入户主身份证号")
        else if (!this.$idCardNoUtil.checkIdCardNo(householdIdNumber)) return this.$u.toast("请输入正确的身份证号")
        if (!householdRelation) return this.$u.toast("请输入与户主关系")
      }
      let flag = [baseInfo, contract, household, extra].flat().reverse().some(e => {
        if (e.required && !this.form[e.prop]) {
          if (!e.dict && (['textarea', 'idNumber'].includes(e.type) || !e.type)) return this.$u.toast("请输入" + e.label) || true
          else return this.$u.toast("请选择" + e.label) || true
        } else if (e.type == 'idNumber' && !this.$idCardNoUtil.checkIdCardNo(this.form[e.prop])) {
          return this.$u.toast("请输入正确的身份证号") || true
        }
      })
      if (!flag) {
        this.$instance.post("/app/appresident/addOrUpdate", this.form).then(res => {
          if (res?.code == 0) {
            this.$u.toast("提交成功!")
            setTimeout(() => uni.switchTab({url: "/pages/AppMine/AppMine"}), 1000)
            // uni.redirectTo({url: "./authSuccess"})
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.authApply {
  background: #F3F6F9;
  min-height: 100vh;
  padding-bottom: 280px;

  .card {
    background: #fff;
    padding-left: 32px;
    margin-bottom: 24px;


    .label {
      font-size: 34px;
      font-family: PingFangSC-Medium, PingFang SC;
      color: #666;
      font-weight: bold;
      line-height: 112px;
      position: relative;

      &.required:before {
        content: "*";
        color: #FF4466;
        position: absolute;
        left: 0;
        transform: translateX(-100%);
      }
    }

    .item {
      width: 100%;
      min-height: 128px;
      color: #333;
      box-shadow: inset 0px -1px 0px 0px #DDDDDD;
      padding-right: 32px;
      box-sizing: border-box;
      justify-content: space-between;
    }
  }

  .radioBtn {
    width: 218px;
    height: 80px;
    background: #FFFFFF;
    border-radius: 16px;
    border: 1px solid #CCC;
    margin-right: 16px;
    text-align: center;
    line-height: 80px;
    margin-bottom: 48px;

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

  .fixed-bottom {
    width: 100vw;
    height: 120px;
    padding: 16px 32px;
    box-sizing: border-box;
    background: inherit;
  }

  textarea {
    width: 100%;
  }
}
</style>
