<template>
  <div >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="智能识别" prop="address">
          <el-input v-model="address" placeholder="输入需要识别的地址" >  <el-button slot="append" icon="el-icon-search" @click="sAddress"></el-button></el-input>
        </el-form-item>
          <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="省市区" >
            <el-cascader
            ref="citySelect"
            v-model="cityIds"
            :options="citys"
            @change="handleCityChange">
            </el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="detail">
            <el-input v-model="form.detail" placeholder="请输入收货人详细地址" />
          </el-form-item>
         <el-form-item label="是否默认地址" prop="isDefault">
            <el-radio-group v-model="form.isDefault">
                <el-radio :label="item.dictValue" v-for="item in orOptions" >{{item.dictLabel}}</el-radio>
            </el-radio-group>
         </el-form-item>
      </el-form>
      <div  style="text-align:right;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
  </div>
</template>

<script>
import { getAddress,listUserAddress, getUserAddress, delUserAddress, addUserAddress, updateUserAddress, exportUserAddress } from "@/api/store/userAddress";
import {getCitys} from "@/api/store/city";
export default {
  name: "add",
  data() {
    return {
      citys:[],
      orOptions:[],
      cityIds:null,
      address:null,
      // 表单参数
      form: {
        province:null,
        city:null,
        district:null,
        cityId:null,
      },
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        realName: [
          { required: true, message: "收货人姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "收货人电话不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        province: [
          { required: true, message: "收货人所在省不能为空", trigger: "blur" }
        ],
        city: [
          { required: true, message: "收货人所在市不能为空", trigger: "blur" }
        ],
        district: [
          { required: true, message: "收货人所在区不能为空", trigger: "blur" }
        ],
        detail: [
          { required: true, message: "收货人详细地址不能为空", trigger: "blur" }
        ],
        isDefault: [
          { required: true, message: "是否默认不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created(){
    this.getCitys();
    this.getDicts("sys_company_or").then(response => {
      this.orOptions = response.data;
    });
  },
  methods: {
    districtChange(val){
      var item=this.citys.find((item)=>{
        return item.cityId==val;
      })
      console.log(item)
      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;
      })
      console.log(item)
      this.form.city=item.name;
      this.form.cityId=val;

    },
    provinceChange(val){
      console.log(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;
    },
    getCitys(){
        getCitys().then(res => {
          this.citys=res.data;
        })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.addressId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleCityChange(value) {
      var nodes=this.$refs.citySelect.getCheckedNodes();
      this.cityIds=value.toString();
      this.form.province=nodes[0].pathLabels[0];
      this.form.city=nodes[0].pathLabels[1];
      this.form.district=nodes[0].pathLabels[2];
    },

    sAddress(){
      getAddress(this.address).then(response => {
        var addressJson = response.data;
        this.msgSuccess(addressJson.reason);
        this.form.province=addressJson.data.provinceName;
        this.form.city=addressJson.data.cityName;
        this.form.district=addressJson.data.expAreaName;
        this.form.detail=addressJson.data.streetName+addressJson.data.address;
        this.form.realName=addressJson.data.name;
        this.form.phone=addressJson.data.mobile;
        var province= this.citys.find(function(o) {
           return o.label ==addressJson.data.provinceName ;
         });
         var city= province.children.find(function(o) {
            return o.label == addressJson.data.cityName;
          });
        var district= city.children.find(function(o) {
             return o.label == addressJson.data.expAreaName;
           });
        this.cityIds=[province.value,city.value,district.value]
      });
    },
    init(userId){

      this.form.userId=userId;
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.city==null||this.form.province==null||this.form.district==null){
           return this.msgSuccess("省市区不能为空");
          }
          addUserAddress(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess("新增成功");
              this.$emit("addUserAddress")
            }
          });
        }
      });
    },
  }
};
</script>
<style scoped>

</style>
