<template>
  <ContentWrap>
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="88px"
    >
      <el-form-item label="专业所名称" prop="officeName">
        <el-input
          v-model="queryParams.officeName"
          class="!w-240px"
          clearable
          placeholder="请输入专业所名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="enabledFlag">
        <el-select
          v-model="queryParams.enabledFlag"
          class="!w-180px"
          clearable
          placeholder="请选择"
        >
          <el-option :value="true" label="启用" />
          <el-option :value="false" label="停用" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button v-hasPermi="['tjt:office:create']" plain type="primary" @click="openDialog('create')">
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column align="center" label="ID" prop="id" width="88" />
      <el-table-column align="center" label="专业所名称" min-width="180" prop="officeName" />
      <el-table-column align="center" label="专业所编码" min-width="140" prop="officeCode" />
      <el-table-column align="center" label="排序号" prop="sortNo" width="100" />
      <el-table-column align="center" label="是否启用" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.enabledFlag ? 'success' : 'info'">
            {{ scope.row.enabledFlag ? '启用' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备注" min-width="220" prop="remark" show-overflow-tooltip />
      <el-table-column align="center" label="创建时间" prop="createTime" width="180" />
      <el-table-column align="center" fixed="right" label="操作" width="160">
        <template #default="scope">
          <el-button v-hasPermi="['tjt:office:update']" link type="primary" @click="openDialog('update', scope.row.id)">
            编辑
          </el-button>
          <el-button v-hasPermi="['tjt:office:delete']" link type="danger" @click="handleDelete(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>

  <Dialog v-model="dialogVisible" :title="dialogTitle" width="520">
    <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="100px">
      <el-form-item label="专业所名称" prop="officeName">
        <el-input v-model="formData.officeName" maxlength="100" placeholder="请输入专业所名称" />
      </el-form-item>
      <el-form-item label="专业所编码" prop="officeCode">
        <el-input v-model="formData.officeCode" maxlength="50" placeholder="请输入专业所编码" />
      </el-form-item>
      <el-form-item label="排序号" prop="sortNo">
        <el-input-number v-model="formData.sortNo" :min="0" :precision="0" class="!w-1/1" controls-position="right" />
      </el-form-item>
      <el-form-item label="是否启用" prop="enabledFlag">
        <el-switch v-model="formData.enabledFlag" active-text="启用" inactive-text="停用" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          :rows="3"
          maxlength="255"
          placeholder="请输入备注"
          show-word-limit
          type="textarea"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button :loading="formLoading" type="primary" @click="submitForm">保存</el-button>
      <el-button @click="dialogVisible = false">取消</el-button>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import type { FormRules } from 'element-plus'
import * as OfficeApi from '@/api/tjt/office'

defineOptions({ name: 'TjtOffice' })

const message = useMessage()
const { t } = useI18n()

const loading = ref(false)
const formLoading = ref(false)
const total = ref(0)
const list = ref<OfficeApi.OfficeVO[]>([])
const queryFormRef = ref()
const formRef = ref()
const dialogVisible = ref(false)
const dialogTitle = ref('')
const formType = ref<'create' | 'update'>('create')

const queryParams = reactive<OfficeApi.OfficePageReqVO>({
  pageNo: 1,
  pageSize: 10,
  officeName: undefined,
  enabledFlag: undefined
})

const createFormData = (): OfficeApi.OfficeVO => ({
  officeName: '',
  officeCode: '',
  sortNo: 0,
  enabledFlag: true,
  remark: ''
})

const formData = ref<OfficeApi.OfficeVO>(createFormData())

const formRules = reactive<FormRules>({
  officeName: [{ required: true, message: '专业所名称不能为空', trigger: 'blur' }]
})

const getList = async () => {
  loading.value = true
  try {
    const data = await OfficeApi.getOfficePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

const resetForm = () => {
  formData.value = createFormData()
  formRef.value?.resetFields()
}

const openDialog = async (type: 'create' | 'update', id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = type === 'create' ? '新增专业所' : '编辑专业所'
  formType.value = type
  resetForm()
  if (!id) {
    return
  }
  formLoading.value = true
  try {
    formData.value = await OfficeApi.getOffice(id)
  } finally {
    formLoading.value = false
  }
}

const submitForm = async () => {
  if (!formRef.value) {
    return
  }
  const valid = await formRef.value.validate()
  if (!valid) {
    return
  }
  formLoading.value = true
  try {
    if (formType.value === 'create') {
      await OfficeApi.createOffice(formData.value)
      message.success(t('common.createSuccess'))
    } else {
      await OfficeApi.updateOffice(formData.value)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    await getList()
  } finally {
    formLoading.value = false
  }
}

const handleDelete = async (id: number) => {
  try {
    await message.delConfirm()
    await OfficeApi.deleteOffice(id)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

onMounted(() => {
  getList()
})

onActivated(() => {
  getList()
})
</script>
