<template>
  <div class="main-container">
    <el-card class="box-card !border-none" shadow="never">
      <div class="flex justify-between items-center">
        <span class="text-lg">{{ pageName }}</span>
        <el-button type="primary" @click="addEvent">
          {{ t('addDepartments') }}
        </el-button>
      </div>

      <el-card
        class="box-card !border-none my-[10px] table-search-wrap"
        shadow="never"
      >
        <el-form
          :inline="true"
          :model="departmentsTable.searchParam"
          ref="searchFormRef"
        >
          <el-form-item :label="t('departmentName')" prop="department_name">
            <el-input
              v-model="departmentsTable.searchParam.department_name"
              :placeholder="t('departmentNamePlaceholder')"
            />
          </el-form-item>

          <el-form-item
            :label="t('parentDepartmentId')"
            prop="parent_department_id"
          >
            <el-select
              class="w-[280px]"
              v-model="departmentsTable.searchParam.parent_department_id"
              clearable
              :placeholder="t('parentDepartmentIdPlaceholder')"
            >
              <el-option
                v-for="(item, index) in parentDepartmentIdList"
                :key="index"
                :label="item['department_name']"
                :value="item['id']"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="loadDepartmentsList()">{{
              t('search')
            }}</el-button>
            <el-button @click="resetForm(searchFormRef)">{{
              t('reset')
            }}</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <div class="mt-[10px]">
        <el-table
          :data="departmentsTable.data"
          size="large"
          v-loading="departmentsTable.loading"
        >
          <template #empty>
            <span>{{ !departmentsTable.loading ? t('emptyData') : '' }}</span>
          </template>
          <el-table-column
            prop="department_name"
            :label="t('departmentName')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="parent_department_id_name"
            :label="t('parentDepartmentId')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            :label="t('operation')"
            fixed="right"
            min-width="120"
          >
            <template #default="{ row }">
              <el-button type="primary" link @click="editEvent(row)">{{
                t('edit')
              }}</el-button>
              <el-button type="primary" link @click="deleteEvent(row.id)">{{
                t('delete')
              }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="mt-[16px] flex justify-end">
          <el-pagination
            v-model:current-page="departmentsTable.page"
            v-model:page-size="departmentsTable.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="departmentsTable.total"
            @size-change="loadDepartmentsList()"
            @current-change="loadDepartmentsList"
          />
        </div>
      </div>

      <edit ref="editDepartmentsDialog" @complete="loadDepartmentsList" />
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { t } from '@/lang'
import { useDictionary } from '@/app/api/dict'
import {
  getDepartmentsList,
  deleteDepartments,
  getWithDepartmentsList,
} from '@/app/api/departments'
import { img } from '@/utils/common'
import { ElMessageBox, FormInstance } from 'element-plus'
import Edit from '@/app/views/departments/components/departments-edit.vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title

let departmentsTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  searchParam: {
    department_name: '',
    parent_department_id: '',
  },
})

const searchFormRef = ref<FormInstance>()

// 选中数据
const selectData = ref<any[]>([])

// 字典数据

/**
 * 获取部门列表
 */
const loadDepartmentsList = (page: number = 1) => {
  departmentsTable.loading = true
  departmentsTable.page = page

  getDepartmentsList({
    page: departmentsTable.page,
    limit: departmentsTable.limit,
    ...departmentsTable.searchParam,
  })
    .then((res) => {
      departmentsTable.loading = false
      departmentsTable.data = res.data.data
      departmentsTable.total = res.data.total
    })
    .catch(() => {
      departmentsTable.loading = false
    })
}
loadDepartmentsList()

const editDepartmentsDialog: Record<string, any> | null = ref(null)

/**
 * 添加部门
 */
const addEvent = () => {
  editDepartmentsDialog.value.setFormData()
  editDepartmentsDialog.value.showDialog = true
}

/**
 * 编辑部门
 * @param data
 */
const editEvent = (data: any) => {
  editDepartmentsDialog.value.setFormData(data)
  editDepartmentsDialog.value.showDialog = true
}

/**
 * 删除部门
 */
const deleteEvent = (id: number) => {
  ElMessageBox.confirm(t('departmentsDeleteTips'), t('warning'), {
    confirmButtonText: t('confirm'),
    cancelButtonText: t('cancel'),
    type: 'warning',
  }).then(() => {
    deleteDepartments(id)
      .then(() => {
        loadDepartmentsList()
      })
      .catch(() => {})
  })
}

const parentDepartmentIdList = ref([])
const setParentDepartmentIdList = async () => {
  parentDepartmentIdList.value = await (await getWithDepartmentsList({})).data
}
setParentDepartmentIdList()

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  loadDepartmentsList()
}
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
  word-break: break-all;
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
