<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="测试名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入测试名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="选择状态" clearable style="width: 240px">
          <el-option label="全部" :value="''" />
          <el-option label="启用" :value="1" />
          <el-option label="禁用" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="search" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">
          {{ $t('btn.edit') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col>

      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="testModuleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="测试名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template #default="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button size="small" type="primary" icon="Edit" @click="handleUpdate(scope.row)">
            {{ $t('btn.edit') }}
          </el-button>
          <el-button size="small" type="danger" icon="Delete" @click="handleDelete(scope.row)">
            {{ $t('btn.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改测试模块对话框 -->
    <el-dialog v-model="open" title="{{ $t('testModule.title') }}" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="测试名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入测试名称" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import { ElMessageBox, ElMessage } from 'element-plus'
import { listTestModule, getTestModule, delTestModule, addTestModule, updateTestModule } from '@/api/testModule'
import Pagination from '@/components/Pagination'
import RightToolbar from '@/components/RightToolbar'
import DictTag from '@/components/DictTag'

const { t } = useI18n()

// 表格数据
const testModuleList = ref([])
const total = ref(0)
const loading = ref(false)
const columns = ref({})
const showSearch = ref(true)

// 表单引用
const formRef = ref(null)
const queryRef = ref(null)

// 表单参数
const form = reactive({
  id: null,
  name: '',
  status: '1'
})

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  name: '',
  status: ''
})

// 表单校验
const rules = reactive({
  name: [
    { required: true, message: '请输入测试名称', trigger: 'blur' }
  ]
})

// 状态字典
const statusOptions = ref([])

// 弹窗状态
const open = ref(false)

// 选中的行
const ids = ref([])
const single = ref(true)
const multiple = ref(true)

// 初始化
onMounted(() => {
  getList()
})

// 获取列表
const getList = async () => {
  loading.value = true
  try {
    const { data } = await listTestModule(queryParams)
    testModuleList.value = data.rows
    total.value = data.total
  } finally {
    loading.value = false
  }
}

// 查询按钮
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置按钮
const resetQuery = () => {
  Object.assign(queryParams, {
    name: '',
    status: ''
  })
  handleQuery()
}

// 新增按钮
const handleAdd = () => {
  open.value = true
  resetForm()
}

// 修改按钮
const handleUpdate = (row) => {
  resetForm()
  if (row) {
    form.id = row.id
    getTestModule(row.id).then(response => {
      Object.assign(form, response.data)
      open.value = true
    })
  } else {
    const id = ids.value[0]
    getTestModule(id).then(response => {
      Object.assign(form, response.data)
      open.value = true
    })
  }
}

// 删除按钮
const handleDelete = (row) => {
  const ids = row ? [row.id] : ids.value
  ElMessageBox.confirm(
    '确定要删除选中的数据吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    return delTestModule(ids)
  }).then(() => {
    ElMessage.success('删除成功')
    getList()
  }).catch(() => {})
}

// 选择行变化
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

// 表单重置
const resetForm = () => {
  Object.assign(form, {
    id: null,
    name: '',
    status: '1'
  })
}

// 表单提交
const submitForm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      if (form.id) {
        updateTestModule(form).then(response => {
          ElMessage.success('修改成功')
          open.value = false
          getList()
        })
      } else {
        addTestModule(form).then(response => {
          ElMessage.success('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

// 取消按钮
const cancel = () => {
  open.value = false
  resetForm()
}
</script>

<style scoped>
/* 可以添加自定义样式 */
</style>
