<template>
  <div class="waybil_form">
    <h4 class="head_title">{{title}}</h4>
    <div class="card_deliverer" v-if="deliverer" @click="addressBook"><i
        class="el-icon-collection icon_address"></i>配送人员薄</div>
    <div class="select_type" v-else>
      <div style="margin-right: 20px;" v-if="conserveFlag">
        <el-radio v-model="radio" label="1" @click.native.prevent="clickitem('1')">保存到地址簿</el-radio>
      </div>
      <div class="card_book" @click="addressBook"><i class="el-icon-collection icon_address"></i>{{thinTitle}}</div>
    </div>
    <el-form style="width: 100%;" :model="ruleForm" :rules="rules" :label-position="labelPosition" label-width="80px"
      ref="ruleForm">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="ruleForm.phone" placeholder="请输入电话"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address" v-if="!deliverer">
        <el-input v-model="ruleForm.address" placeholder="请输入地址"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {
    regionData,
    CodeToText
  } from 'element-china-area-data'
  export default {
    name: 'waybillForm',
    props: {
      // 地址簿名称
      thinTitle: {
        type: String,
        default: () => '地址簿',
      },
      // 表单tabel数据
      title: {
        type: String,
        default: () => '',
      },
      // 绑定值
      ruleForm: {
        type: Object,
        default: () => {},
      },
      // 配送人
      deliverer: {
        type: Boolean,
        default: () => false,
      },
      // 配送人
      conserveFlag: {
        type: Boolean,
        default: () => true,
      },
    },
    data() {
      return {
        labelPosition: 'left',
        options: regionData,
        selectedOptions: [],
        radio: '1',
        rules: {
          name: {
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          },
          phone: {
            required: true,
            message: '请输入电话',
            trigger: 'blur'
          },
          region: {
            required: true,
            message: '请选择省市区',
            trigger: 'blur,change'
          },
          address: {
            required: true,
            message: '请输入地址',
            trigger: 'blur'
          },
        }
      }
    },
    watch: {
      ruleForm: {
        handler(newVal) {
          this.selectedOptions = newVal.regionCode
        },
        deep: true // 开启深度监听
      }
    },
    methods: {
      addressBook() {
        this.$emit('addressBook')
      },
      clickitem(value) {
        value === this.radio ? this.radio = '' : this.radio = value
        this.$emit('conserveSelect', this.radio)
      },
      //子组件校验，传递到父组件
      validateForm() {
        let flag = null
        this.$refs['ruleForm'].validate(valid => {
          if (valid) {
            flag = true
          } else {
            flag = false
          }
        })
        return flag
      },
      // 重置
      resetForm() {
        this.selectedOptions = []
        this.$refs.ruleForm.resetFields();
      }
    }
  }
</script>

<style lang="scss" scoped>
  .head_title {
    display: flex;
    justify-content: center;
    width: calc(100% - 6px);
    padding-bottom: 6px;
    padding-left: 5px;
    border-bottom: 1px solid #E4E7ED;
    margin: 10px 0px;
  }

  .waybil_form {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
  }

  .select_type {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    margin-left: 80px;
  }

  .icon_address {
    font-size: 23px;
    margin-right: 5px;
  }

  .card_book {
    display: flex;
    align-items: center;
    cursor: pointer;
  }

  .card_deliverer {
    cursor: pointer;
    margin-left: 80px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    margin-bottom: 20px;
  }

  .card_deliverer:hover {
    color: #409EFF;
  }

  .card_book:hover {
    color: #409EFF;
  }
</style>
