<!-- 基本信息 -->
<template>
  <div>
    <div class="topTip">丰富详尽的企业介绍能提高求职者对贵企业的关注和了解，有助于达到更好的招聘效果</div>
    <CtForm ref="CtFormRef" :items="formItems" style="width: 900px;margin: 0 auto">
      <template #name="{ item }">
        <div v-show="!item.show" class="text-right" style="width: 80px; line-height: 40px;">
          <v-icon :color="statusInfo.color" size="20">{{ statusInfo.mdi }}</v-icon>
          <span :style="{'color': statusInfo.color,'font-size': '14px'}">{{ statusInfo.label }}</span>
        </div>
      </template>
      <template #industryId="{ item }">
        <v-menu :close-delay="1" :open-delay="0" v-bind="$attrs" :close-on-content-click="true">
          <template v-slot:activator="{  props }">
            <TextInput
              v-model="item.value"
              :item="item"
              v-bind="props"
            ></TextInput>
          </template>
          <industryTypeCard :limit="1" :select="[query.industryId].filter(Boolean)" @handleClickIndustry="handleIndustry"></industryTypeCard>
        </v-menu>
      </template>
      <template #prepare="{ item }">
        <v-checkbox v-model="item.value" label="筹建中（如果贵企业正在筹建，请勾选）" color="primary"></v-checkbox>
      </template>
    </CtForm>
    <div class="text-center">
      <v-btn color="primary" class="buttons mt-3 mb-10" @click.stop="handleSave">{{ $t('common.save') }}</v-btn>
    </div>
  </div>
</template>

<script setup>
defineOptions({name: 'informationSettingsComponents-basicInfo'})
import { ref, reactive, computed } from 'vue'
import { getEnterpriseBaseInfo, updateEnterpriseBaseInfo } from '@/api/enterprise'
import { getEnterpriseAuth } from '@/api/recruit/enterprise/information'
import { getDict } from '@/hooks/web/useDictionaries'
import { useI18n } from '@/hooks/web/useI18n'
import industryTypeCard from '@/components/industryTypeCard'
import Snackbar from '@/plugins/snackbar'

const { t } = useI18n()
const CtFormRef = ref()
const query = reactive({})
const formItems = ref({
  options: [
    {
      type: 'text',
      key: 'name',
      value: '',
      label: '企业全称 *',
      slotName: 'name',
      rules: [v => !!v || '请输入企业全称']
    },
    {
      type: 'text',
      key: 'anotherName',
      value: '',
      col: 6,
      flexStyle: 'mr-3',
      label: '企业别名 *',
      rules: [v => !!v || '请输入企业别名']
    },
    {
      type: 'text',
      key: 'website',
      value: '',
      col: 6,
      label: '企业官网'
    },
    {
      type: 'text',
      key: 'contact',
      value: '',
      col: 6,
      flexStyle: 'mr-3',
      label: '联系人'
    },
    {
      type: 'phoneNumber',
      key: 'phone',
      value: '',
      col: 6,
      label: '联系电话'
    },
    {
      slotName: 'industryId',
      key: 'industryId',
      value: null,
      label: '所在行业 *',
      outlined: true,
      clearable: false,
      itemText: 'label',
      itemValue: 'value',
      col: 6,
      noParam: true,
      flexStyle: 'mr-3',
      rules: [v => !!v || '请选择所在行业']
    },
    {
      type: 'autocomplete',
      key: 'financingStatus',
      value: null,
      label: '融资阶段 *',
      outlined: true,
      clearable: false,
      itemText: 'label',
      itemValue: 'value',
      col: 6,
      dictTypeName: 'menduner_financing_status',
      rules: [v => !!v || '请选择融资阶段'],
      items: []
    },
    {
      type: 'autocomplete',
      key: 'scale',
      value: null,
      label: '企业规模 *',
      outlined: true,
      clearable: false,
      itemText: 'label',
      itemValue: 'value',
      col: 6,
      flexStyle: 'mr-3',
      dictTypeName: 'menduner_scale',
      rules: [v => !!v || '请选择企业规模'],
      items: []
    },
    {
      type: 'text',
      key: 'workTime',
      value: null,
      col: 6,
      class: 'mb-3',
      label: '上班时间(示例：上午09:00 - 下午17:00)'
    },
    {
      type: 'datePicker',
      key: 'openTime',
      dateType: 'date', // 时间类型 year month date time
      value: null,
      label: '开业时间 *',
      col: 6,
      flexStyle: 'mr-3',
      outlined: true,
      rules: [v => !!v || '请选择开业时间']
      // options: {},
    },
    {
      slotName: 'prepare',
      key: 'prepare',
      value: true,
      col: 6
    },
    {
      type: 'textarea',
      key: 'introduce',
      value: null,
      counter: 2000,
      rows: 6,
      label: '企业介绍 *',
      outlined: true,
      rules: [v => !!v || '请输入企业介绍']
    },
  ]
})

const setValue = (key, value) => {
  formItems.value.options.find(e => e.key === key).value = value
}

// 行业列表
const industryList = ref([])
getDict('menduner_industry_type', {}, 'industryList').then(({ data }) => {
  data = data?.length && data || []
  industryList.value = data
})

// 获取企业实名信息
const authInfo = ref({})
const statusList = [
  { label: '未认证', color: '#fb8c00', value: null, mdi: 'mdi-shield-remove' },
  { label: '审核中', color: '#fb8c00', value: '0', mdi: 'mdi-shield-half-full' },
  { label: '已认证', color: '#00897B', value: '1', mdi: 'mdi-shield-check' },
  { label: '已驳回', color: '#fe574a', value: '2', mdi: 'mdi-shield-off' }
]
const getAuthInfo = async () => {
  const data = await getEnterpriseAuth()
  if (!data) return
  authInfo.value = data
}
getAuthInfo()

const statusInfo = computed(() => {
  const obj = (authInfo.value && Object.keys(authInfo.value).length) ? statusList.find(e => e.value === authInfo.value.status) : statusList[0]
  return obj
})

// 获取基本信息
const getBaseInfo = async () => {
  const data = await getEnterpriseBaseInfo()
  if (!data) return
  query.id = data.id
  formItems.value.options.forEach(item => {
    if (item.dictTypeName) {
      getDict(item.dictTypeName).then(({ data }) => {
        data = data?.length && data || []
        item.items = data
      })
    }
    query.industryId = data.industryId
    if (item.key === 'industryId') {
      item.value = industryList.value.find(e => e.id === data[item.key])?.nameCn
    } else item.value = data[item.key]
  })
}
getBaseInfo()

// 所在行业
const handleIndustry = (list, arr) => {
  if (!list.length) return
  query.industryId = list[0]
  const str = arr.map(e => e.nameCn).join('、')
  setValue('industryId', str)
}

const handleSave = async () => {
  const { valid } = await CtFormRef.value.formRef.validate()
  if (!valid) return
  formItems.value.options.forEach(e => {
    if (e.noParam) return
    query[e.key] = e.value
  })
  await updateEnterpriseBaseInfo(query)
  Snackbar.success(t('common.saveMsg'))
  getBaseInfo()
}

defineExpose({
  getAuthInfo
})
</script>

<style lang="scss" scoped>
.topTip {
  background-color: #f7f8fa;
  color: #2f3640;
  padding: 12px 20px;
  margin: 10px 0 40px;
  font-size: 14px;
}
</style>
