<template>
  <div class="comp-content">
    <el-drawer
      v-model="visible"
      title="字典列表"
      :size="600"
      :close-on-click-modal="false"
      @close="closeFun"
    >
      <div class="content">
        <div class="add-btn">
          <el-button :icon="Plus" type="primary" @click="openDetailFun({})">新增字典</el-button>
        </div>
        <div class="table">
          <BaseTable
            ref="BaseTableRef"
            tableName="DICT_CLASS_LIST"
            hidePagination
            :actionList="actionList"
          />
        </div>
      </div>
      <template #footer>
        <div>
          <el-button @click="closeFun">关闭</el-button>
        </div>
      </template>
    </el-drawer>
    <DictDetailDia
      v-model="addDiaVisible"
      :classData="data"
      :data="detailData"
      :zIndex="9999"
      @submit="submitFun"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import DictDetailDia from './dictDetail.vue';
import { getDictionaryDataApi, deleteDictionaryDataApi } from '@/api/system/systemData';
import { getAllDictDataFun } from '@/utils/common';
import BaseTable from '@/components/common/table/baseTable.vue';

interface Props {
  modelValue: boolean;
  data: any;
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  data: {},
});

const actionList = ref<any>([
  {
    title: '编辑',
    type: 'primary',
    click: (row: any) => {
      openDetailFun(row);
    },
  },
  {
    title: '删除',
    type: 'danger',
    needConfirm: true,
    confirmTip: '确定删除字典吗？',
    click: (row: any) => {
      delFun(row);
    },
  },
]);

const emit = defineEmits(['update:modelValue']);
const BaseTableRef = ref<any>();
const visible = ref(false);
const tableData = ref<any>([]);
const addDiaVisible = ref(false);
const detailData = ref<any>({});

watch(
  () => props.modelValue,
  (val: boolean) => {
    visible.value = val;
    if (val) {
      getListFun();
    }
  }
);

const getListFun = () => {
  const params = {
    dictClass: props.data.className,
  };
  getDictionaryDataApi(params).then((res: any) => {
    if (res.code === 200) {
      BaseTableRef.value?.setDataFun(res.data);
      tableData.value = res.data;
    }
  });
};

const openDetailFun = (data: any) => {
  detailData.value = data;
  addDiaVisible.value = true;
};

const submitFun = () => {
  getListFun();
};

const delFun = (data: any) => {
  const params = {
    uuid: data.uuid,
  };
  deleteDictionaryDataApi(params).then((res: any) => {
    if (res.code === 200) {
      getListFun();
      getAllDictDataFun(true);
      ElMessage.success('操作成功');
    }
  });
};

const closeFun = () => {
  emit('update:modelValue', false);
};
</script>

<style lang="scss" scoped>
.content {
  height: 100%;
  display: flex;
  flex-direction: column;
  .add-btn {
    padding-bottom: var(--padding-normal);
  }
  .table {
    flex: 1;
  }
}
</style>
