<!-- 家庭地址 -->
<template>
  <span>
    <!-- 省市区选择 -->
    <cs-field
      ref="csField"
      v-model="areaValue"
      :label="label"
      :rules="rules"
      :rules-key="rulesKey"
      :disabled="disabled"
      :colon="colon"
      :required="required"
      :readonly="readonly"
      :error-message-align="errorMessageAlign"
      :label-class="labelClass"
      :label-width="labelWidth"
      :label-align="labelAlign"
      :input-align="inputAlign"
      right-icon="arrow"
      @click="showArea = true"
    >
      <div :class="disabled ? 'disabled' : ''">
        <span v-if="areaValue">{{ areaValue }}</span>
        <span class="placeholder" v-else>{{ currentPlaceholder }}</span>
      </div>
    </cs-field>
    <van-popup v-model="showArea" position="bottom">
      <van-area
        :area-list="areaList"
        :cancel-button-text="i18nLang.$t('common.clean')"
        @confirm="areaConfirm"
        @cancel="cancel()"
      />
    </van-popup>

    <!-- 详细地址 -->
    <cs-field
      ref="csFieldTextarea"
      class="field-address-textarea"
      v-model="address"
      :label="'详细地址'"
      :rules="rules"
      :disabled="disabled"
      :colon="colon"
      :required="required"
      :readonly="readonly"
      :error-message-align="errorMessageAlign"
    >
      <van-field
        class="padding-none"
        type="textarea"
        rows="2"
        autosize
        v-model="address"
        :placeholder="currentTextareaPlaceholder"
      />
    </cs-field>
  </span>
</template>
<script>
import CsField from '../Base'
import mixin from '../mixin'
import Data from '../../../../lib/data/city.json'
import { i18n } from '../../../..//lib/locale/lang/index'

export default {
  name: 'CsFieldAddress',
  components: { CsField },
  mixins: [mixin],
  props: {
    // 当前输入的值
    value: {
      default: null
    },
    // 文本域占位文字
    textareaPlaceholder: {
      default: null
    }
  },
  data() {
    return {
      i18nLang: new Vue({ i18n }),
      areaValue: '',
      address: '',

      showArea: false,
      areaList: null
    }
  },
  //监听属性 类似于data概念
  computed: {
    currentPlaceholder() {
      if (this.placeholder) {
        return this.placeholder
      } else {
        return this.i18nLang.$t('common.placeholderAddress')
      }
    },
    currentTextareaPlaceholder() {
      if (this.textareaPlaceholder) {
        return this.textareaPlaceholder
      } else {
        return this.i18nLang.$t('common.placeholderAddress2')
      }
    }
  },
  //监控data中的数据变化
  watch: {
    value: {
      handler(val, oldVal) {
        if (val != oldVal) {
          this.setData()
        }
      },
      immediate: true, // 首次绑定是否执行
      deep: true // 对象内部的属性监听，也叫深度监听
    },
    address: {
      handler(val, oldVal) {
        if (val != oldVal) {
          this.submit()
        }
      },
      immediate: false, // 首次绑定是否执行
      deep: true // 对象内部的属性监听，也叫深度监听
    }
  },
  //方法集合
  methods: {
    change() {
      this.$forceUpdate()
    },

    /** 选中地址 */
    areaConfirm(areaValues) {
      let names = []
      areaValues.forEach(value => {
        if (value.name && value.name != '-') {
          names.push(value.name)
        }
      })
      this.areaValue = names.join('-')
      this.showArea = false
      this.submit()
    },

    cancel() {
      this.areaValue = ''
      this.showArea = false
      this.$emit('input', '')
    },

    /** 确认提交 */
    submit() {
      let areaValue = this.areaValue
      if (!areaValue) {
        areaValue = '--'
      }
      this.$emit('input', `${areaValue}-${this.address}`)
    },

    /** 绑定数据 */
    setData() {
      if (this.value) {
        let args = this.value.split('-')
        var areaValue = ''
        var address = ''
        if (args.length < 4) {
          address = this.value
        } else {
          areaValue = `${args[0]}-${args[1]}-${args[2]}`
          address = args.splice(3, args.length - 3).join('-')
        }
        if (areaValue == '--') {
          this.areaValue = ''
        } else {
          this.areaValue = areaValue
        }
        this.address = address
      }
    },

    init() {
      if (!this.areaList) {
        this.areaList = Data
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.init()
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang="scss">
.field-address-textarea {
  .van-field__label {
    display: none;
  }

  .van-cell--required::before {
    display: none;
  }
}
</style>
