<!-- QuarantineList.vue -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="羊只" prop="sheepId">
        <el-input v-model="queryParams.sheepId" placeholder="请输入耳号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="检疫日期" style="width: 308px">
        <el-date-picker v-model="daterangeDatetime" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</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="openDialog()">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="quarantineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="羊只" align="center" prop="sheepNo" />
      <el-table-column label="检疫日期" align="center" prop="datetime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.datetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="羊只性别" align="center" prop="gender" />
      <el-table-column label="羊只类别" align="center" prop="sheepType" />
      <el-table-column label="月龄" align="center" prop="monthAge" />
      <el-table-column label="繁育状态" align="center" prop="breed" />
      <el-table-column label="检疫项目" align="center" prop="itemName" />
      <el-table-column label="样品类型" align="center" prop="sample" />
      <el-table-column label="采样员" align="center" prop="sampler" />
      <el-table-column label="检疫员" align="center" prop="quarOfficer" />
      <el-table-column label="检疫结果" align="center" prop="result">
        <template #default="scope">
          <dict-tag :options="quar_result" :value="scope.row.result" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="quar_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template #default="scope">
          <el-button link type="primary" @click="openDialog(scope.row)">检疫</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 弹窗组件 -->
    <add ref="dialogRef" @success="getList" />
  </div>
</template>

<script setup name="QuarantineList">
import { listQuarantine } from '@/api/biosafety/quarantine'
import add from './add.vue'
const { proxy } = getCurrentInstance()
const { quar_result, quar_status } = proxy.useDict('quar_result', 'quar_status')

const dialogRef = ref()
const quarantineList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const daterangeDatetime = ref([])

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sheepId: null,
  params: {}
})

function getList() {
  loading.value = true
  queryParams.params = {}
  if (daterangeDatetime.value && daterangeDatetime.value.length === 2) {
    queryParams.params.beginDatetime = daterangeDatetime.value[0]
    queryParams.params.endDatetime = daterangeDatetime.value[1]
  }
  listQuarantine(queryParams).then(res => {
    quarantineList.value = res.rows
    total.value = res.total
    loading.value = false
  })
}

function openDialog(row = null) {
  dialogRef.value.open(row)
}

function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm('queryRef')
  daterangeDatetime.value = []
  handleQuery()
}

getList()
</script>