<template>
  <div >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="收货人姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入收货人姓名" />
        </el-form-item>
        <el-form-item label="收货人电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入收货人电话" />
        </el-form-item>
        <el-form-item label="收货地址" prop="district">
          <el-row :gutter="20">
          <el-col :span="6">
             <el-select @change="provinceChange" v-model="form.province" placeholder="请选择">
                <el-option
                  v-for="item in province"
                  :key="item.cityId"
                  :label="item.name"
                  :value="item.cityId">
                </el-option>
              </el-select>
          </el-col>
          <el-col :span="6">
            <el-select @change="cityChange" v-model="form.city" placeholder="请选择">
                <el-option
                  v-for="item in city"
                  :key="item.cityId"
                  :label="item.name"
                  :value="item.cityId">
                </el-option>
              </el-select>
          </el-col>
          <el-col :span="6">
             <el-select @change="districtChange" v-model="form.district" placeholder="请选择">
                <el-option
                  v-for="item in district"
                  :key="item.cityId"
                  :label="item.name"
                  :value="item.cityId">
                </el-option>
              </el-select>
          </el-col>
        </el-row>
        </el-form-item>
        <el-form-item label="详细地址" prop="detail">
          <el-input v-model="form.detail" placeholder="请输入收货人详细地址" />
        </el-form-item>
      </el-form>
      <div  style="text-align:right;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
  </div>
</template>

<script>
import { listUserAddress, getUserAddress, delUserAddress, addUserAddress, updateUserAddress, exportUserAddress } from "@/api/users/userAddress";
import {getCitys} from "@/api/hisStore/city";
export default {
  name: "add",
  data() {
    return {
      citys:[],
      province:[],
      city:[],
      district:[],
      // 表单参数
      form: {
        province:null,
        city:null,
        district:null,
        cityId:null,
      },
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "会员不能为空", trigger: "blur" }
        ],
        realName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "电话不能为空", trigger: "blur" }
        ],
        detail: [
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
        district: [
          { required: true, message: "收货地址不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created(){
    this.getCityList();
  },
  methods: {
    districtChange(val){
      const item = this.district.find(i => i.cityId === val)
      this.form.district=item.name;
    },
    cityChange(val){
      const item = this.city.find(i => i.cityId === val)
      this.district = item.children
      this.form.district=null;
      this.form.city=item.name;
      this.form.cityId=val;
    },
    provinceChange(val){
      const item = this.citys.find(i => i.cityId === val)
      this.city = item.children
      this.district=[];
      this.form.city=null;
      this.form.district=null;
      this.form.province=item.name;
    },
    getCityList(){
      getCitys().then(res => {
          this.loading = false;
          this.citys = this.convertCityData(res.data)
          this.province=this.citys.filter(item => item.parentId===0 )
        })
    },
    convertCityData(array) {
      return array.map(item => {
        return {
          'cityId': item.value,
          'name': item.label,
          'parentId':  item.pid,
          'children': item.children && this.convertCityData(item.children)
        }
      });
    },
    init(userId){
      this.form.userId=userId;
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addUserAddress(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess("新增成功");
              this.$emit("addUserAddress")
            }
          });
        }
      });
    },
  }
};
</script>
<style scoped>

</style>
