<template>
  <div class="drawerBox">
    <!-- direction="btt" -->
    <el-drawer v-model="drawer" :before-close="handleBeforeClose" size="50%">
      <template #header>
        <h2>{{ drawerInfo.label }}</h2>
      </template>
      <template #default>
        <div class="drawerContent">
          <div class="item" v-for="e in menuList" :key="e.name">
            <div class="left">{{ e.name }}</div>
            <div class="right">
              <el-input class="addressInput" v-model="e.value" :placeholder="e.placeholder" />
              <el-icon v-if="e.icon" class="rightArrow"> <ArrowRight /></el-icon>
            </div>
          </div>
          <div class="quickBox" v-if="addressList.length > 0">
            <span class="title">快捷地址</span>
            <div
              class="quickItem"
              v-for="(quick, index) in addressList"
              :key="index"
              @click="setAddress(quick)"
            >
              <el-icon class="locationIcon"><Location /></el-icon>
              <span>{{ quick.region }}{{ quick.city }}{{ quick.street }}{{ quick.location }}</span>
            </div>
          </div>
        </div>
      </template>
      <template #footer>
        <div class="buttonBox">
          <div class="left" @click.stop="resetClick">
            <el-icon class="buttonIcon"><RefreshRight /></el-icon>
            <span>重置</span>
          </div>
          <div class="right" @click.stop="confirmClick">
            <el-icon><Check /></el-icon>
            <span>保存</span>
          </div>
        </div>
      </template>
    </el-drawer>
  </div>
</template>
<script setup name="AddressDrawer">
import deepClone from '@/utils/common'
import { watch, ref } from 'vue'
const props = defineProps({
  showAddressDrawer: {
    type: Boolean,
    default: null
  },
  drawerInfo: {
    type: Object,
    default: () => {
      return {}
    }
  },
  addressMenuList: {
    type: Array,
    default: () => {
      return []
    }
  },
  addressQuickList: {
    type: Array,
    default: () => {
      return []
    }
  }
})
const emit = defineEmits(['close', 'confirmDrawer'])
const { proxy } = getCurrentInstance()
const drawer = ref(false)
const menuList = ref([])

const addressList = ref([
  {
    region: '中国',
    city: '北京',
    street: '朝阳区',
    location: '三里屯'
  }
])
// 处理抽屉关闭前的事件
const handleBeforeClose = done => {
  menuList.value = props.addressMenuList
  emit('close')
  drawer.value = false
  done()
}
const resetClick = () => {
  menuList.value.forEach(e => {
    e.value = ''
  })
}
const confirmClick = () => {
  let info = JSON.parse(JSON.stringify(props.drawerInfo))
  //收集填写的地址传给父组件
  let location = {}
  menuList.value.forEach(e => {
    location[e.key] = e.value
  })
  for (const key in props.drawerInfo) {
    if (key === info.key) {
      info[key] = location
    }
  }
  emit('confirmDrawer', info)
  drawer.value = false
}
//通过快捷电话设置区号和电话号码
const setAddress = item => {
  menuList.value.forEach(e => {
    for (const key in item) {
      if (e.key === key) {
        e.value = item[e.key]
      }
    }
  })
}
// 初始化menuList数据
const initMenuList = () => {
  // 使用深拷贝创建新的数组，避免修改props
  menuList.value = deepClone(props.addressMenuList)
}
// 监听父组件传递的showAddressDrawer变化
watch(
  () => props.showAddressDrawer,
  val => {
    drawer.value = val
    if (val) {
      // 每次打开抽屉时初始化数据
      nextTick(() => {
        initMenuList()
      })
    }
  }
)
watch(
  () => props.addressQuickList,
  val => {
    addressList.value = val
  },
  { immediate: true }
)
watch(drawer, val => {
  if (!val) {
    proxy.$emit('close', false)
  }
})
</script>
<style lang="scss" scoped>
::v-deep .el-drawer__header {
  margin-bottom: 0 !important;
}
::v-deep .el-drawer__footer {
  padding: 0;
}
.addressInput ::v-deep .el-input__wrapper {
  box-shadow: none !important;
}
.addressInput ::v-deep .el-input__inner::placeholder {
  text-align: right;
}
.addressInput ::v-deep .el-input__inner {
  text-align: right;
}
.addressInput ::v-deep .el-input__wrapper {
  padding: 0 !important;
}
.drawerContent {
  .item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #e5e6e8;
    margin-bottom: 20px;
    padding: 10px;
    color: #4e5969;
    font-size: 14px;
    .right {
      display: flex;
      align-items: center;
    }
    .rightArrow {
      color: #4e5969;
      font-size: 14px;
    }
  }
  .quickBox {
    .title {
      font-size: 18px;
      border-left: 4px solid #165dff;
      padding-left: 5px;
    }
    .quickItem {
      display: flex;
      align-items: center;
      justify-content: flex-start;
      margin-top: 10px;
      color: #1d2129;
      font-size: 14px;
      border: 1px solid #e5e6e8;
      border-radius: 3px;
      padding: 10px;
      .locationIcon {
        color: #1d2129;
        font-size: 16px;
        margin-right: 5px;
      }
    }
  }
}
.buttonBox {
  box-shadow: 0 -1px 14px #00000014;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .left,
  .right {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 50%;
    font-size: 16px;
    height: 60px;
  }
  .left {
    background-color: #fff;
    color: #165dff;
    .buttonIcon {
      font-size: 16px;
      color: #165dff;
    }
  }
  .right {
    background-color: #165dff;
    color: #fff;
    .buttonIcon {
      font-size: 16px;
      color: #fff;
    }
  }
}
</style>
