<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/hisStore/userAddress";
import {getAllList} 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){
      var item=this.citys.find((item)=>{
        return item.cityId==val;
      })
      this.form.district=item.name;

    },
    cityChange(val){
      this.district=this.citys.filter(item => item.parentId===val )
      this.form.district=null;
      var item=this.citys.find((item)=>{
        return item.cityId==val;
      })
      this.form.city=item.name;
      this.form.cityId=val;

    },
    provinceChange(val){
      this.city=this.citys.filter(item => item.parentId===val )
      this.district=[];
      this.form.city=null;
      this.form.district=null;
      var item=this.citys.find((item)=>{
        return item.cityId==val;
      })
      this.form.province=item.name;
    },
    getCityList(){
        getAllList().then(res => {
          this.loading = false;
          this.citys=res.data;
          this.province=res.data.filter(item => item.level===0 )
        })
    },
    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>
