<template>
  <div class="container">
    <p class="title pl10 pv12 mt10">基本信息</p>
    <div v-if="source == 'app'">
      <van-field
        v-model="userInfo.customerName"
        required
        clearable
        label="姓名"
        name="姓名"
        placeholder="请输入"
        v-validate="'required|name'"
      />
      <van-field
        v-model="userInfo.customerPhone"
        required
        clearable
        label="移动电话"
        name="移动电话"
        placeholder="请输入"
        maxlength="11"
        type="tel"
        v-validate="'required|mobile'"
      />
    </div>
    <div v-else>
      <van-field v-model="userInfo.customerName" required readonly label="姓名" />
      <van-field v-model="userInfo.customerPhone" required readonly label="移动电话" />
    </div>
    <p class="bd"></p>
    <form-block :userInfo="userInfo" ref="formBlock"></form-block>
    <van-button type="danger" class="bottom-btn" @click="save" v-no-more-click="1000">保存</van-button>
  </div>
</template>

<script>
import { Field, CellGroup, Cell } from 'vant'
import { getAgentCustomerInfo, updateCustomerInfo } from '@/api/ebiz/customer/customer'
import formBlock from '@/components/ebiz/customer/formBlock'
import { constants } from 'crypto'

export default {
  name: 'editCustomer',
  components: {
    [Field.name]: Field,
    [CellGroup.name]: CellGroup,
    [Cell.name]: Cell,
    [formBlock.name]: formBlock
  },
  data() {
    return {
      source: this.$route.query.from,
      appName: this.$route.query.name,
      appPhone: this.$route.query.tel,
      customerNumber: localStorage.getItem('customerNumber'),
      userInfo: {
        customerName: '', //姓名
        customerPhone: '', // 手机号
        customerSex: '0', //性别
        nativeplace: '1', //国家地区
        province: '', //户籍省
        city: '', //户籍市
        birthday: '', //出生日期
        idType: '1', //证件类型
        customerIdNumber: '', //证件号码
        certificateValidate: '', //证件起始日期
        certiexpiredate: '', //证件到期时间
        effectiveDateType: false, //是否长期
        email: '', //电子邮箱
        stature: '', //身高
        weight: '', //体重,
        customerType: '0', //客户类别
        customerSource: '-1', //客户来源
        degree: '', //文化程度
        socialSecurity: '0', //有无社保
        taxIdentity: '1', //税收居民身份
        occupationCode: '', //职业类别
        occupationName: '',
        lifeGrade: '', //寿险等级
        healthGrade: '', //健康等级
        averageYearlyIncome: '', //平均年收入
        liabilitiesMoney: '', //负债金额
        workUnits: '', //工作单位
        workCondition: '', //在职情况
        companyProvince: '', //单位省
        companyCity: '', //单位市
        companyArea: '', //单位区
        companyAddress: '', //单位详细地址
        companyZip: '', //单位邮编
        companyPhone: '', //单位电话
        marriage: '', //婚姻状况
        familyAnnualIncome: '', //家庭年收入
        homeProvince: '', //家庭省
        homeCity: '', //家庭市
        homeArea: '', //家庭区
        homeAddress: '', //家庭详细地址
        homeZip: '', //家庭邮编
        homePhone: '' //家庭电话
      }
    }
  },
  mounted() {
    //this.filterBtn() //按钮初始化
    //window.appCallBack = this.appCallBack //app回调
    //数据回显
    if (this.source == 'app') {
      this.userInfo.customerName = this.appName
      this.userInfo.customerPhone = this.appPhone
      this.customerNumber = ''
    } else {
      this.getCustomerInfo() //获取客户信息
    }
  },
  methods: {
    // filterBtn() {
    //   EWebBridge.webCallAppInJs('webview_right_button', {
    //     btns: [
    //       {
    //         img:"@/assets/images/u12045.png",
    //       }
    //     ]
    //   })
    // },
    // appCallBack(data) {
    //   if (data.trigger == 'right_button_click') {
    //     if (data.index == 0 && this.loading) {
    //       this.updateCustomerInfo() //保存信息
    //     }
    //   }
    // },
    getCustomerInfo() {
      let $this = this
      let data = {
        customerNumber: $this.customerNumber
      }
      getAgentCustomerInfo(data)
        .then(res => {
          if (res.result == 0) {
            let result = res.content
            $this.userInfo.nativeplace = result.country
            $this.userInfo.idType = result.customerIdType
            $this.userInfo.degree = result.educationLevel
            $this.userInfo.taxIdentity = result.residentStatus
            $this.userInfo.marriage = result.marryStatus
            $this.userInfo.socialSecurity = result.socialSecurity
            $this.userInfo.customerName = result.customerName
            $this.userInfo.customerPhone = result.customerPhone
            $this.userInfo.customerSex = result.customerSex
            $this.userInfo.birthday = result.birthday
            $this.userInfo.customerIdNumber = result.customerIdNumber
            $this.userInfo.certificateValidate = result.idEffectStartDate
            $this.userInfo.certiexpiredate = result.idEffectEndDate
            $this.userInfo.email = result.email
            $this.userInfo.stature = result.height
            $this.userInfo.weight = result.weight
            $this.userInfo.customerType = result.customerType
            $this.userInfo.customerSource = result.customerSource
            $this.userInfo.occupationName = result.occupationName
            $this.userInfo.occupationCode = result.occupationCode
            $this.userInfo.lifeGrade = result.lifeGrade
            $this.userInfo.healthGrade = result.healthGrade
            $this.userInfo.averageYearlyIncome = result.averageYearlyIncome
            $this.userInfo.workUnits = result.workUnits
            $this.userInfo.province = result.province
            $this.userInfo.city = result.city
            $this.userInfo.homeAddress = result.homeAddress
            $this.userInfo.homeZip = result.homeZip
            $this.userInfo.homePhone = result.homePhone
            $this.userInfo.familyAnnualIncome = result.familyAnnualIncome
            $this.userInfo.workCondition = result.jobStatus
            $this.userInfo.companyAddress = result.companyAddress
            $this.userInfo.companyZip = result.companyZip
            $this.userInfo.companyPhone = result.companyPhone
            $this.userInfo.liabilitiesMoney = result.liabilitiesMoney
            $this.userInfo.companyProvince = result.companyProvince
            $this.userInfo.companyCity = result.companyCity
            $this.userInfo.companyArea = result.companyArea
            $this.userInfo.homeProvince = result.homeProvince
            $this.userInfo.homeCity = result.homeCity
            $this.userInfo.homeArea = result.homeArea
            sessionStorage.setItem('birthday', result.birthday)
            sessionStorage.setItem('idType', result.customerIdType)

            switch ($this.userInfo.companyArea) {
              case '500229':
                $this.userInfo.companyArea = '500129'
                break
              case '500230':
                $this.userInfo.companyArea = '500130'
                break
              case '500231':
                $this.userInfo.companyArea = '500131'
                break
              case '500233':
                $this.userInfo.companyArea = '500133'
                break
              case '500235':
                $this.userInfo.companyArea = '500135'
                break
              case '500236':
                $this.userInfo.companyArea = '500136'
                break
              case '500237':
                $this.userInfo.companyArea = '500137'
                break
              case '500238':
                $this.userInfo.companyArea = '500138'
                break
              case '500240':
                $this.userInfo.companyArea = '500140'
                break
              case '500241':
                $this.userInfo.companyArea = '500141'
                break
              case '500242':
                $this.userInfo.companyArea = '500142'
                break
              case '500243':
                $this.userInfo.companyArea = '500143'
                break
            }
            switch ($this.userInfo.homeArea) {
              case '500229':
                $this.userInfo.homeArea = '500129'
                break
              case '500230':
                $this.userInfo.homeArea = '500130'
                break
              case '500231':
                $this.userInfo.homeArea = '500131'
                break
              case '500233':
                $this.userInfo.homeArea = '500133'
                break
              case '500235':
                $this.userInfo.homeArea = '500135'
                break
              case '500236':
                $this.userInfo.homeArea = '500136'
                break
              case '500237':
                $this.userInfo.homeArea = '500137'
                break
              case '500238':
                $this.userInfo.homeArea = '500138'
                break
              case '500240':
                $this.userInfo.homeArea = '500140'
                break
              case '500241':
                $this.userInfo.homeArea = '500141'
                break
              case '500242':
                $this.userInfo.homeArea = '500142'
                break
              case '500243':
                $this.userInfo.homeArea = '500143'
                break
            }
            this.$refs.formBlock.isEndFlag()
            //查询职业名称
            // occupationList.forEach(first => {
            //   first.subs.forEach(second => {
            //     second.subs.forEach(third => {
            //       if (third.code == result.occupationCode) {
            //         $this.userInfo.occupationName = third.name
            //       }
            //     })
            //   })
            // })
          }
        })
        .catch(err => {})
    },
    save() {
      this.$validator.validate().then(valid => {
        if (true === valid) {
          let vf1 = this.$refs.formBlock.validateForm()
          Promise.all([vf1]).then(result => {
            if (result.indexOf(false) > -1) {
              this.$toast(this.$refs.formBlock.$validator.errors.all()[0])
              return
            } else if (this.userInfo.customerIdNumber) {
              this.$refs.formBlock.validateIdType()
            } else {
              if (this.userInfo.birthday && this.userInfo.marriage) {
                this.$refs.formBlock.validateMarr()
              } else {
                this.updateCustomerInfo()
              }
            }
          })
        } else {
          this.$toast(this.$validator.errors.all()[0])
        }
      })
    },
    updateCustomerInfo() {
      let $this = this
      let data = {
        agentCustomerInfoDTO: {
          customerNumber: $this.customerNumber,
          customerName: $this.userInfo.customerName,
          customerPhone: $this.userInfo.customerPhone,
          customerSex: $this.userInfo.customerSex,
          country: $this.userInfo.nativeplace,
          //rgtaddress: $this.userInfo.census,
          birthday: $this.userInfo.birthday,
          customerIdType: $this.userInfo.idType,
          customerIdNumber: $this.userInfo.customerIdNumber,
          idEffectStartDate: $this.userInfo.certificateValidate,
          idEffectEndDate: sessionStorage.getItem('isEnd') || $this.userInfo.certiexpiredate,
          email: $this.userInfo.email,
          height: $this.userInfo.stature,
          weight: $this.userInfo.weight,
          customerType: $this.userInfo.customerType,
          customerSource: $this.userInfo.customerSource,
          educationLevel: $this.userInfo.degree,
          socialSecurity: $this.userInfo.socialSecurity,
          residentStatus: $this.userInfo.taxIdentity,
          occupationCode: $this.userInfo.occupationCode,
          occupationName: $this.userInfo.occupationName,
          lifeGrade: $this.userInfo.lifeGrade,
          healthGrade: $this.userInfo.healthGrade,
          averageYearlyIncome: $this.userInfo.averageYearlyIncome,
          liabilitiesMoney: $this.userInfo.liabilitiesMoney,
          workUnits: $this.userInfo.workUnits,
          jobStatus: $this.userInfo.workCondition,
          companyProvince: $this.userInfo.companyProvince,
          companyCity: $this.userInfo.companyCity,
          companyArea: $this.userInfo.companyArea,
          companyAddress: $this.userInfo.companyAddress,
          companyZip: $this.userInfo.companyZip,
          companyPhone: $this.userInfo.companyPhone,
          marryStatus: $this.userInfo.marriage,
          homeProvince: $this.userInfo.homeProvince,
          homeCity: $this.userInfo.homeCity,
          homeArea: $this.userInfo.homeArea,
          homeAddress: $this.userInfo.homeAddress,
          homeZip: $this.userInfo.homeZip,
          familyAnnualIncome: $this.userInfo.familyAnnualIncome,
          province: $this.userInfo.province,
          city: $this.userInfo.city,
          homePhone: $this.userInfo.homePhone
        }
      }
      switch (data.agentCustomerInfoDTO.companyArea) {
        case '500129':
          data.agentCustomerInfoDTO.companyArea = '500229'
          break
        case '500130':
          data.agentCustomerInfoDTO.companyArea = '500230'
          break
        case '500131':
          data.agentCustomerInfoDTO.companyArea = '500231'
          break
        case '500133':
          data.agentCustomerInfoDTO.companyArea = '500233'
          break
        case '500135':
          data.agentCustomerInfoDTO.companyArea = '500235'
          break
        case '500136':
          data.agentCustomerInfoDTO.companyArea = '500236'
          break
        case '500137':
          data.agentCustomerInfoDTO.companyArea = '500237'
          break
        case '500138':
          data.agentCustomerInfoDTO.companyArea = '500238'
          break
        case '500140':
          data.agentCustomerInfoDTO.companyArea = '500240'
          break
        case '500141':
          data.agentCustomerInfoDTO.companyArea = '500241'
          break
        case '500142':
          data.agentCustomerInfoDTO.companyArea = '500242'
          break
        case '500143':
          data.agentCustomerInfoDTO.companyArea = '500243'
          break
      }
      switch (data.agentCustomerInfoDTO.homeArea) {
        case '500129':
          data.agentCustomerInfoDTO.homeArea = '500229'
          break
        case '500130':
          data.agentCustomerInfoDTO.homeArea = '500230'
          break
        case '500131':
          data.agentCustomerInfoDTO.homeArea = '500231'
          break
        case '500133':
          data.agentCustomerInfoDTO.homeArea = '500233'
          break
        case '500135':
          data.agentCustomerInfoDTO.homeArea = '500235'
          break
        case '500136':
          data.agentCustomerInfoDTO.homeArea = '500236'
          break
        case '500137':
          data.agentCustomerInfoDTO.homeArea = '500237'
          break
        case '500138':
          data.agentCustomerInfoDTO.homeArea = '500238'
          break
        case '500140':
          data.agentCustomerInfoDTO.homeArea = '500240'
          break
        case '500141':
          data.agentCustomerInfoDTO.homeArea = '500241'
          break
        case '500142':
          data.agentCustomerInfoDTO.homeArea = '500242'
          break
        case '500143':
          data.agentCustomerInfoDTO.homeArea = '500243'
          break
      }
      updateCustomerInfo(data)
        .then(res => {
          if (res.result == 0) {
            //  EWebBridge.webCallAppInJs("bridge",{
            //   "flag":"webview_toast",
            //   "extra":{"content":res.resultMessage}
            // })
            //EWebBridge.webCallAppInJs('goBack', { refresh: '1', index: '-2' })
            this.$jump({
              flag: 'goBack',
              extra: {
                refresh: '1',
                index: this.$route.query.from == 2 ? '-2' : '-1'
              },
              routerInfo: {
                path: '/customer/customerList'
              }
            })
          } else {
            $this.$toast(res.resultMessage)
            // EWebBridge.webCallAppInJs("bridge",{
            //   "flag":"webview_toast",
            //   "extra":{"content":res.resultMessage}
            // })
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    go(path) {
      this.$jump({
        flag: 'h5',
        extra: {
          url: location.origin + '/#/customer/' + path
        },
        routerInfo: {
          path: '/customer/' + path
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.van-index-anchor {
  background: #fff;
}
.container {
  padding-bottom: 45px;
  .header {
    display: flex;
    justify-content: flex-end;
    padding: 10px;
  }
  .title {
    font-size: 15px;
    font-weight: bold;
    background: #fff;
    border-bottom: 1px solid #ebedf0;
  }
}
.bd {
  border-bottom: 1px solid #ebedf0;
  margin-left: 15px;
}
</style>
