<template>
  <div>
    <v-card class="pa-5">
      <v-timeline align="start" side="end">
        <v-timeline-item
          v-for="(val, i) in list"
          :key="i"
          :dot-color="val.color"
          :icon="val.icon"
        >
          <div>
            <h2 class="mt-n1 headline font-weight-regular">{{ val.title }}</h2>
            <div class="mb-4 desc">{{ val.desc }}</div>
            <component class="mt-10" :is="val.path" :ref="val.ref" :itemData="itemData"></component>
          </div>
        </v-timeline-item>
      </v-timeline>
      <div class="text-center mb">
        <v-btn class="half-button mr-3" color="primary" variant="outlined" @click="handleCancel">{{ $t('common.cancel') }}</v-btn>
        <v-btn class="half-button" color="primary" @click="recharge = true">{{ $t('common.release') }}</v-btn>
      </div>
    </v-card>

    <CtDialog :visible="recharge" :widthType="2" titleClass="text-h6" title="确认支付" @close="handleClose" @submit="handleSubmit">
      <Recharge ref="rechargeRef"></Recharge>
    </CtDialog>
  </div>
</template>

<script setup>
defineOptions({ name: 'enterprise-position-add'})
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { dealDictObjData } from '@/utils/position'
import { saveJobAdvertised, getJobDetails } from '@/api/position'
import baseInfo from './baseInfo.vue'
import jobRequirements from './jobRequirements.vue'
import Snackbar from '@/plugins/snackbar'
import { useI18n } from '@/hooks/web/useI18n'
import { useUserStore } from '@/store/user'

const { t } = useI18n()
const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const baseInfoRef = ref()
const jobRequirementsRef = ref()
const itemData = ref({})

// 充值
const rechargeRef = ref()
const handleClose = () => {
  recharge.value = false
  rechargeRef.value.formItems.options.find(e => e.key === 'count').value = 100
}

const handleSubmit = () => {
  recharge.value = false
  Snackbar.warning('此功能还在开发中')
}

const list = [
  {
    color: '#00897B',
    icon: 'mdi-numeric-1',
    title: t('position.positionInformation'),
    desc: '',
    path: baseInfo,
    ref: baseInfoRef
  },
  {
    color: 'indigo-lighten-2',
    icon: 'mdi-numeric-2',
    title: t('position.jobRequirements'),
    desc: t('position.requirementDesc'),
    path: jobRequirements,
    ref: jobRequirementsRef
  }
]

// 余额充值
const recharge = ref(false)

// 取消
const handleCancel = () => {
  itemData.value = {}
  router.push('/recruit/enterprise/position')
  // 新增职位发布需更新账户信息
  if (route.query && !route.query?.id) {
    setTimeout(async () => {
      await userStore.getEnterpriseUserAccountInfo()
    }, 2000)
  }
}

// 发布
const handleSave = async () => {
  const baseInfo = await baseInfoRef.value[0].getQuery()
  const requirement = await jobRequirementsRef.value[0].getQuery()
  if (!baseInfo || !requirement) return Snackbar.warning('请将信息填写完整')
  const query = Object.assign(baseInfo, requirement)
  // 有id则为编辑
  if (route.query && route.query.id) query.id = route.query.id
  try {
    await saveJobAdvertised(query)
    Snackbar.success(route.query.id ? t('common.editSuccessMsg') : t('common.publishSuccessMsg'))
    handleCancel()
  } catch (error) {
    // 余额不足展示充值窗口
    if (error === '用户余额不足') recharge.value = true
  }
  
}

// 获取编辑的职位详情
const getPositionDetail = async (id) => {
  const data = await getJobDetails({ id })
  if (!data && !Object.keys(data).length) return
  itemData.value = {...data, ...dealDictObjData({}, data)}
}

// 有id为编辑
if (route.query && route.query.id) {
  if (route.query.id) getPositionDetail(route.query.id)
}
</script>

<style scoped lang="scss">
.desc {
  font-size: 13px;
  color: var(--color-666);
}
</style>

<style lang="scss" scoped>
.mb {
  margin-bottom: 100px;
}
</style>
