<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" @click="openCountryDrawer(e)">
              <el-input
                class="phoneInput"
                v-model="e.value"
                :placeholder="e.placeholder"
                :type="e.type"
              />
              <el-icon v-if="e.icon" class="rightArrow"> <ArrowRight /></el-icon>
            </div>
          </div>
          <div class="quickBox" v-if="quickList.length > 0">
            <span class="title">快捷电话</span>
            <div
              class="quickItem"
              v-for="(quick, index) in quickList"
              :key="index"
              @click="setPhone(quick)"
            >
              <span>{{ quick.code }}</span>
              <span style="margin-left: 5px">{{ quick.mobile }}</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>
    <Country
      :showCountryDrawer="showCountryDrawer"
      @close="hanleCountryClose"
      @confirmCountry="confirmCountry"
    />
  </div>
</template>
<script setup name="PhoneDrawer">
import { watch, ref } from 'vue'
import Country from '@/views/components/country'
import deepClone from '@/utils/common'
const props = defineProps({
  showDrawer: {
    type: Boolean,
    default: null
  },
  drawerInfo: {
    type: Object,
    default: () => {
      return {}
    }
  },
  phoneMenuList: {
    type: Array,
    default: () => {
      return []
    }
  },
  phoneQuickList: {
    type: Array,
    default: () => {
      return []
    }
  }
})

const emit = defineEmits(['close', 'confirmDrawer'])
const { proxy } = getCurrentInstance()
const drawer = ref(false)
const showCountryDrawer = ref(false) //国家地区抽屉开关
const menuList = ref([])
const quickList = ref([])
const confirmCountry = item => {
  menuList.value[0].value = item.areaCode

  hanleCountryClose()
}
const openCountryDrawer = item => {
  if (item.arrow) {
    showCountryDrawer.value = true
  }
}
// 处理抽屉关闭前的事件
const handleBeforeClose = done => {
  menuList.value = props.phoneMenuList
  emit('close')
  drawer.value = false
  done()
}
const resetClick = () => {
  menuList.value.forEach(e => {
    e.value = ''
  })
}
const confirmClick = () => {
  let error = true
  let info = JSON.parse(JSON.stringify(props.drawerInfo))
  //收集填写的电话传给父组件
  let phone = {}
  menuList.value.forEach(e => {
    if (!e.value) {
      error = false
      proxy.$message.warning('请填写' + e.name)
      return
    }
    phone[e.key] = e.value
  })
  if (!error) return
  for (const key in props.drawerInfo) {
    if (key === info.key) {
      info[key] = phone
    }
  }
  emit('confirmDrawer', info)
  drawer.value = false
}
//通过快捷电话设置区号和电话号码
const setPhone = item => {
  console.log('menuList', menuList.value)
  console.log('item', item)
  menuList.value[0].value = item.code
  menuList.value[1].value = item.mobile
}
// 关闭国家地区的抽屉
const hanleCountryClose = () => {
  showCountryDrawer.value = false
}
// 初始化menuList数据
const initMenuList = () => {
  // 使用深拷贝创建新的数组，避免修改props
  menuList.value = deepClone(props.phoneMenuList)
}
watch(
  () => props.showDrawer,
  val => {
    drawer.value = val
    if (val) {
      // 每次打开抽屉时初始化数据
      nextTick(() => {
        initMenuList()
      })
    }
  }
)
watch(drawer, val => {
  if (!val) {
    proxy.$emit('close', false)
  }
})
watch(
  () => props.phoneQuickList,
  val => {
    quickList.value = val
  },
  { immediate: true }
)
</script>
<style lang="scss" scoped>
 :deep(.el-drawer__header ) {
  margin-bottom: 0 !important;
}
 :deep(.el-drawer__footer ) {
  padding: 0;
}
.phoneInput :deep(.el-input__wrapper ) {
  box-shadow: none !important;
}
.phoneInput :deep(.el-input__inner::placeholder ) {
  text-align: right;
}
.phoneInput :deep(.el-input__inner ) {
  text-align: right;
}
.phoneInput :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;
      cursor: pointer;
    }
  }
}
.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>
