<template>
  <div v-if="['select', 'radio', 'checkbox'].includes(formConfig.currentItem.componentCode)">
    <a-form-item>
      <template #label>
        <a-space>
          <span>字典类型:</span>
          <a @click="dictStand">字典维护</a>
          <a-tooltip>
            <template #title>刷新</template>
            <redo-outlined style="cursor: pointer" @click="refresh" />
          </a-tooltip>
        </a-space>
      </template>
      <a-select
        v-model:value="formConfig.currentItem.formItemSelect.dictTypeId"
        placeholder="请选择字典类型"
        style="width: 100%"
        allowClear
      >
        <a-select-option :value="dictItem.dictTypeId" v-for="dictItem in dictTypeList" :key="dictItem.dictTypeId">{{
          dictItem.dictTypeName
        }}</a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item label="字典取值:">
      <a-select v-model:value="formConfig.currentItem.formItemSelect.dictValueType" placeholder="请选择字典取值" style="width: 100%">
        <a-select-option :value="1">字典id</a-select-option>
        <a-select-option :value="2">字典code</a-select-option>
      </a-select>
    </a-form-item>

    <!-- 用户字典维护 -->
    <UserDict v-model:visible="showDictConfig" v-if="showDictConfig" />
  </div>
</template>

<script setup name="SelectAttr">
import { inject, ref } from 'vue';
import { useRouter } from 'vue-router';
import UserDict from '../user-dict/index.vue';
import { MetaUserDictApi } from '../../api/MetaUserDictApi';

const router = useRouter();

const formConfig = inject('formConfig');
// 字典类型列表
const dictTypeList = inject('dictTypeList');
const refreshDict = inject('refreshDict');

// 是否显示字典类型配置
const showDictConfig = ref(false);

// 字典维护
const dictStand = async () => {
  const res = await MetaUserDictApi.getDictUserTypeFlag();
  if (res) {
    // 用户
    showDictConfig.value = true;
  } else {
    // 系统
    const { href } = router.resolve({
      path: '/system/backend/dict'
    });
    window.open(href, '_blank');
  }
};

// 刷新字典
const refresh = () => {
  refreshDict(true);
};
</script>

<style></style>
