<template>
  <div class="addAddress">
    <div class="form-box" style="margin-top: 0">
      <div>
        <span style="color: rgb(64, 158, 255);cursor: pointer;margin-left: 20px" @click="cancel">地址列表</span>
        <span style="margin: 0 5px;color: #C0C4CC;">/</span>
        <span>地址编辑</span>
      </div>
    </div>
    <div class="form-box">
      <el-form :model="msg" style="padding:0 50px;" :rules="rules" ref="msg" label-width="120px">
        <el-form-item label="收件人姓名" prop="Name" class="is-required">
          <el-input v-model="msg.Name" placeholder="请输入收件人姓名"/>
        </el-form-item>

        <el-form-item label="联系方式" prop="Mobile" class="is-required">
          <el-input v-model="msg.Mobile" placeholder="请输入联系方式"/>
        </el-form-item>

        <el-form-item label="省市区" prop="position" class="is-required" >
          <el-cascader v-model="msg.position" :placeholder="addressName"  :options="provinceList" @active-item-change="handleItemChange" :props="props" style="width: 300px"></el-cascader>

        </el-form-item>


        <el-form-item label="详细地址" prop="Address" class="is-required">
          <el-input v-model="msg.Address" placeholder="请输入详细地址"/>
        </el-form-item>

        <el-form-item label="备注">
          <el-input v-model="msg.Remarks" placeholder="请输入退货备注"/>
        </el-form-item>
      </el-form>
    </div>
    <el-button type="primary" style="margin: 12px 0 " @click="preserve('msg')">保存</el-button>

  </div>
</template>

<script>
    export default {
        name: "addAddress",
      data(){
          return{
            provinceList: [],     //省市县列表
            props: {value: 'value', children: 'cities'},


            Name:[],
            msg:{
              ID:0,
              Name:'',
              Mobile:'',
              Address:'',
              Remarks:'',
              ProvinceId:0,
              CityId:0,
              DistrictId:0,
              position:[],

            },
            addressName:'请选择',
            cityindex:0,
            rules:{
              Name:[
                {required: true, message: "请输入收件人姓名", trigger: "blur"}
              ],
              Mobile:[
                {required: true, message: "请输入联系方式", trigger: "blur"}
              ],
              Address:[
                {required: true, message: "请输入详细地址", trigger: "blur"}
              ],
              position:[
                { type: 'array', required: true, message: '请选择省市区', trigger: 'change' }
              ],
            }
          }
      },
      created(){
        if(this.$route.query.ID){
          this.getLogisticsDelivery(this.$route.query.ID)
        }
      },
      methods:{


        cancel(){
          this.$router.go(-1);//返回上一层
        },
        getLogisticsDelivery(ID){
          this.apipost("/api/MallBase/GetLogisticsDelivery",{'ID':ID} , res => {
            if (res.data.resultCode == 1) {
              this.msg =res.data.data
              this.msg.position =[res.data.data.ProvinceId,res.data.data.CityId,res.data.data.DistrictId]
              this.addressName = res.data.data.ProvinceName+'/'+res.data.data.CityName+'/'+res.data.data.DistrictName
            } else {
              this.Info(res.data.message);
            }
          })
        },
        preserve(formName){
          this.msg.ProvinceId = this.msg.position[0];
          this.msg.CityId = this.msg.position[1];
          this.msg.DistrictId = this.msg.position[2];
          this.$refs[formName].validate((valid) => {
            if (valid) {
              delete this.msg.position //删除position 不传到后台
              this.apipost("/api/MallBase/AddOrUpdateLogisticsDelivery",this.msg , res => {
                if (res.data.resultCode == 1) {
                  this.Success(res.data.message);
                  this.$router.go(-1);//返回上一层

                } else {
                  this.Info(res.data.message);
                }
              })
            } else {
              return false;
            }
          });
        },
        getPosition(val, cb) {

          let vm = this; //查询省市县
          let params = {};
          if(!val) { //初始化加载 获取所有省份数据
            params = {Id:1}
          } else if (val.length === 1) { //加载二级  获取市级数据
            params = {Id: val[0]}
          } else {  //加载3级   获取县级数据
            params = {Id: val[1]}
          }

          this.apipost("/api/Destination/GetChildList",params , res => {
            if (res.data.resultCode == 1) {
              if(!val) { //初始化加载   查询省份数据
                vm.provinceList = res.data.data.map((e) => {
                  return {value: e.ID, label: e.Name, cities: []}
                });
              } else if (val.length === 1) { //加载二级    查询该省下市级数据
                vm.provinceList.map((item) => {
                  if(item.value === val[0]) {
                    item.cities = res.data.data.map((e) => {
                      return {value: e.ID, label: e.Name, cities: []}
                    })
                  }
                })
              } else {  //加载3级   查询该省市下县级数据
                vm.provinceList.map((item) => {
                  if(item.value === val[0]) {
                    item.cities.map((value) => {
                      if(value.value === val[1]){
                        value.cities = res.data.data.map((e) => {
                          return {value: e.ID, label: e.Name}
                        })
                      }
                    })
                  }
                })
              }
              cb&&cb(res);
            } else {
              // this.Info(res.data.message);
            }
          })

        },
        handleItemChange (val) {
          this.getPosition(val)
        },
      },
      mounted(){
        this.getPosition(null)

      }
    }
</script>

<style scoped>
  .addAddress .form-box{
    padding: 20px 0;
    background-color: #fff;
    margin-bottom: 20px;
    padding-right: 50%;
    margin-top: 10px;
  }
  .addAddress .el-input__inner{
    height: 32px;
    line-height: 32px
  }

</style>
