<!-- 选择环境记录单弹窗 -->
<script lang="ts" setup name="EnvironmentalDialog">
import { ElMessage } from 'element-plus'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import type { IEnvironmental, IListQuery, IRoomNumber } from '@/views/business/lab/environmental/environmentalList_interface'
import { getEnvironmentRecordList } from '@/api/business/lab/environmentRecord'
import { getTypeSelect } from '@/api/system/price'

const emits = defineEmits(['add'])
const dialogFormVisible = ref(false) // 控制弹窗显隐
// 查询参数
const listQuery = ref<IListQuery>({
  environmentCode: '', // 记录单编号
  roomNumber: '', // 房间号
  beginTime: '', // 记录开始时间
  endTime: '', // 记录结束时间
  offset: 1, // 当前页
  limit: 20, // 多少条
})
const loadingTable = ref(false) // loading
const list = ref<IEnvironmental[]>([]) // 表格参数
const total = ref(0)
const columns = ref<TableColumn[]>([
  { text: '记录单号', value: 'environmentCode', align: 'center', width: '160', fixed: true },
  { text: '房间号', value: 'roomNumber', align: 'center', fixed: true },
  { text: '记录人', value: 'recorder', align: 'center' },
  { text: '记录时间', value: 'recordTime', width: '180', align: 'center' },
  { text: '温度(℃)', value: 'temperature', align: 'center' },
  { text: '湿度(%)', value: 'humidity', align: 'center' },
  { text: '输出电压(V)', value: 'outputVoltage', align: 'center' },
  { text: '零地电压(V)', value: 'zeroGroundVoltage', align: 'center' },
])
const roomNumberList = ref<IRoomNumber[]>([]) // 房间号
const checkoutList = ref<string[]>([]) // 多选选中参数

// 查询列表
const fetchData = (isNowPage: boolean) => {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  getEnvironmentRecordList(listQuery.value).then((res) => {
    list.value = res.data.rows
    total.value = res.data.total
  })
  loadingTable.value = false
}
fetchData(true)

// 搜索
const search = () => {
  fetchData(true)
}
// 重置
const clearList = () => {
  listQuery.value = {
    environmentCode: '', // 记录单编号
    roomNumber: '', // 房间号
    beginTime: '', // 记录开始时间
    endTime: '', // 记录结束时间
    offset: 1, // 当前页
    limit: 20, // 多少条
  }
  fetchData(true)
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  fetchData(true)
}
// 点击保存
const submitForm = () => {
  if (!checkoutList.value.length) {
    ElMessage.warning('请选择环境记录单')
  }
  else {
    emits('add', checkoutList.value[0])
    dialogFormVisible.value = false
  }
}

// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  clearList()
}

// 多选选中
const handleSelectionChange = (val: any) => {
  checkoutList.value = val
}

// 获取字典值
async function getDict() {
  // 获取房间号
  getTypeSelect('roomNumber').then((res) => {
    roomNumberList.value = res.data
  })
}

// 初始化
const initDialog = () => {
  dialogFormVisible.value = true
  getDict()
  fetchData(true)
}
defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-model="dialogFormVisible" title="选择环境记录单" width="60%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="clearList">
      <search-item>
        <el-input
          v-model.trim="listQuery.environmentCode"
          placeholder="编号"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.roomNumber"
          placeholder="房间号"
          clearable
        />
      </search-item>
    </search-area>
    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table
        :data="list"
        :total="total"
        :columns="columns"
        :query="listQuery"
        is-showmulti-select
        :list-loading="loadingTable"
        :is-multi="false" @change="changePage"
        @multi-select="handleSelectionChange"
      >
        <!-- 序号 -->
        <template #preColumns>
          <el-table-column label="#" width="55" align="center" fixed>
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="submitForm">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
:deep(.el-radio__label) {
  display: none;
}
</style>
