<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch"> <el-form-item label="大队"
        prop="brigadeId">
        <el-select v-model="queryParams.brigadeId" placeholder="请选择大队" filterable clearable style="width: 200px">
          <el-option v-for="item in brigadeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="startDate">
        <el-date-picker clearable v-model="queryParams.startDate" type="date" placeholder="选择日期" value-format="YYYY-MM-DD"
          style="width: 200px" />
      </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="handleAdd"
          v-hasPermi="['blocked:daily:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['blocked:daily:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['blocked:daily:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="Upload" @click="handleImport"
          v-hasPermi="['blocked:daily:import']">导入</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dailyLuggageCheckInList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日期" align="center" prop="statDate" min-width="120" />
      <el-table-column label="班次" align="center" prop="shiftType" min-width="100" />
      <el-table-column label="当班大队" align="center" prop="brigadeName" min-width="120" />
      <el-table-column label="T1旅检过检行李数" align="center" prop="t1TravelLuggageCount" min-width="150" />
      <el-table-column label="T2旅检过检行李数" align="center" prop="t2TravelLuggageCount" min-width="150" />
      <el-table-column label="过检行李合计" align="center" prop="totalLuggageCount" min-width="120" />
      <el-table-column label="T1旅检查堵件数" align="center" prop="t1TravelBlockedCount" min-width="140" />
      <el-table-column label="T1旅检万分率" align="center" prop="t1TravelBlockRate" min-width="130" />
      <el-table-column label="T2旅检查堵件数" align="center" prop="t2TravelBlockedCount" min-width="140" />
      <el-table-column label="T2旅检万分率" align="center" prop="t2TravelBlockRate" min-width="130" />
      <el-table-column label="T1行检过检行李数" align="center" prop="t1WalkLuggageCount" min-width="150" />
      <el-table-column label="T2行检过检行李数" align="center" prop="t2WalkLuggageCount" min-width="150" />
      <el-table-column label="T1行检查堵件数" align="center" prop="t1WalkBlockedCount" min-width="140" />
      <el-table-column label="T1行检万分率" align="center" prop="t1WalkBlockRate" min-width="130" />
      <el-table-column label="T2行检查堵件数" align="center" prop="t2WalkBlockedCount" min-width="140" />
      <el-table-column label="T2行检万分率" align="center" prop="t2WalkBlockRate" min-width="130" />
      <el-table-column label="查堵合计件数" align="center" prop="totalBlockedCount" min-width="120" />
      <el-table-column label="当日查堵万分率" align="center" prop="dailyBlockRate" min-width="130" />
      <el-table-column label="T1复查图像总数" align="center" prop="t1ReviewImageTotal" min-width="140" />
      <el-table-column label="T1-AI标记总数" align="center" prop="t1AiMarkTotal" min-width="140" />
      <el-table-column label="T1-AI误判总数" align="center" prop="t1AiErrorTotal" min-width="140" />
      <el-table-column label="T1-AI漏判总数" align="center" prop="t1AiMissTotal" min-width="140" />
      <el-table-column label="T2复查图像总数" align="center" prop="t2ReviewImageTotal" min-width="140" />
      <el-table-column label="T2-AI标记总数" align="center" prop="t2AiMarkTotal" min-width="140" />
      <el-table-column label="T2-AI误判总数" align="center" prop="t2AiErrorTotal" min-width="140" />
      <el-table-column label="T2-AI漏判总数" align="center" prop="t2AiMissTotal" min-width="140" />
      <el-table-column label="其他(VP通道)数量" align="center" prop="otherVipCount" min-width="150" />
      <el-table-column label="AI复查图像总数" align="center" prop="aiReviewImageTotal" min-width="150" />
      <el-table-column label="AI标记图像总数" align="center" prop="aiMarkTotal" min-width="150" />
      <el-table-column label="AI漏判图像总数" align="center" prop="aiMissImageTotal" min-width="150" />
      <el-table-column label="AI误判图像总数" align="center" prop="aiErrorImageTotal" min-width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['blocked:daily:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['blocked:daily:remove']">删除</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" />

    <!-- 添加或修改每日行李查缉对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body @closed="reset" destroy-on-close>
      <el-form ref="dailyLuggageCheckInRef" :model="form" :rules="rules" label-width="140px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="日期" prop="statDate">
              <el-date-picker v-model="form.statDate" type="date" placeholder="选择日期" value-format="YYYY-MM-DD"
                style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班次" prop="shiftType">
              <el-select v-model="form.shiftType" placeholder="请选择班次" filterable style="width: 100%">
                <el-option label="早班" value="早班" />
                <el-option label="晚班" value="晚班" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="当班大队" prop="brigadeId">
          <el-select v-model="form.brigadeId" placeholder="请选择当班大队" filterable style="width: 100%">
            <el-option v-for="item in brigadeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="T1旅检过检行李数" prop="t1TravelLuggageCount">
              <el-input-number v-model="form.t1TravelLuggageCount" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="T2旅检过检行李数" prop="t2TravelLuggageCount">
              <el-input-number v-model="form.t2TravelLuggageCount" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="T1行检过检行李数" prop="t1WalkLuggageCount">
              <el-input-number v-model="form.t1WalkLuggageCount" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="T2行检过检行李数" prop="t2WalkLuggageCount">
              <el-input-number v-model="form.t2WalkLuggageCount" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <!--  <el-col :span="12">
          <el-form-item label="过检行李合计" prop="totalLuggageCount">
              <el-input-number v-model="form.totalLuggageCount" :min="0" style="width: 100%" />
            </el-form-item> 
          </el-col> -->
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="T1旅检查堵件数" prop="t1TravelBlockedCount">
              <el-input-number v-model="form.t1TravelBlockedCount" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="T2旅检查堵件数" prop="t2TravelBlockedCount">
              <el-input-number v-model="form.t2TravelBlockedCount" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <!--  <el-col :span="12">
            <el-form-item label="T1旅检万分率" prop="t1TravelBlockRate">
              <el-input-number v-model="form.t1TravelBlockRate" :min="0" :precision="2" style="width: 100%" />
            </el-form-item> 
          </el-col>-->
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="T1行检查堵件数" prop="t1WalkBlockedCount">
              <el-input-number v-model="form.t1WalkBlockedCount" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="T2行检查堵件数" prop="t2WalkBlockedCount">
              <el-input-number v-model="form.t2WalkBlockedCount" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <!--<el-col :span="12">
             <el-form-item label="T2旅检万分率" prop="t2TravelBlockRate">
              <el-input-number v-model="form.t2TravelBlockRate" :min="0" :precision="2" style="width: 100%" />
            </el-form-item> 
          </el-col>-->
        </el-row>


        <!--  <el-col :span="12">
          <el-form-item label="T1行检万分率" prop="t1WalkBlockRate">
              <el-input-number v-model="form.t1WalkBlockRate" :min="0" :precision="2" style="width: 100%" />
            </el-form-item> 
          </el-col>-->


        <!-- <el-col :span="12">
            <el-form-item label="T2行检万分率" prop="t2WalkBlockRate">
              <el-input-number v-model="form.t2WalkBlockRate" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col> -->


          <!--   <el-col :span="12">
           <el-form-item label="当日查堵万分率" prop="dailyBlockRate">
              <el-input-number v-model="form.dailyBlockRate" :min="0" :precision="2" style="width: 100%" />
            </el-form-item> 
          </el-col>-->
    
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="T1复查图像总数" prop="t1ReviewImageTotal">
              <el-input-number v-model="form.t1ReviewImageTotal" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="T1-AI标记总数" prop="t1AiMarkTotal">
              <el-input-number v-model="form.t1AiMarkTotal" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="T1-AI误判总数" prop="t1AiErrorTotal">
              <el-input-number v-model="form.t1AiErrorTotal" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="T1-AI漏判总数" prop="t1AiMissTotal">
              <el-input-number v-model="form.t1AiMissTotal" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="T2复查图像总数" prop="t2ReviewImageTotal">
              <el-input-number v-model="form.t2ReviewImageTotal" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="T2-AI标记总数" prop="t2AiMarkTotal">
              <el-input-number v-model="form.t2AiMarkTotal" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="T2-AI误判总数" prop="t2AiErrorTotal">
              <el-input-number v-model="form.t2AiErrorTotal" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="T2-AI漏判总数" prop="t2AiMissTotal">
              <el-input-number v-model="form.t2AiMissTotal" :min="0" :precision="0" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="其他(VP通道)数量" prop="otherVipCount">
          <el-input-number v-model="form.otherVipCount" :min="0" style="width: 100%" />
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog title="导入" v-model="upload.open" width="500px" append-to-body>
      <el-upload ref="uploadRef" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url"
        :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess"
        :auto-upload="false" drag>
        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <template #tip>
          <div class="el-upload__tip text-center">
            <span>仅允许导入xls、xlsx格式文件。</span>
            <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline"
              @click="importTemplate">下载模板</el-link>
          </div>
        </template>
      </el-upload>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, getCurrentInstance } from 'vue'
import { listDailyLuggageCheckIn, getDailyLuggageCheckIn, delDailyLuggageCheckIn, addDailyLuggageCheckIn, updateDailyLuggageCheckIn, exportDailyLuggageCheckIn, downloadTemplate } from '@/api/blockingData/dailyLuggageCheckInList'
import { listDept } from '@/api/system/dept'
import { getToken } from '@/utils/auth'
import { UploadFilled } from '@element-plus/icons-vue'

const { proxy } = getCurrentInstance()

// 遮罩层
const loading = ref(true)
// 选中数组
const ids = ref([])
// 非单个禁用
const single = ref(true)
// 非多个禁用
const multiple = ref(true)
// 显示搜索条件
const showSearch = ref(true)
// 总条数
const total = ref(0)
// 每日行李查缉表格数据
const dailyLuggageCheckInList = ref([])
// 弹出层标题
const title = ref('')
// 是否显示弹出层
const open = ref(false)

const upload = reactive({
  open: false,
  isUploading: false,
  headers: { Authorization: 'Bearer ' + getToken() },
  url: import.meta.env.VITE_APP_BASE_API + '/blocked/daily/importData'
})

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  statDate: null,
  shiftType: null
})

// 表单参数
const form = reactive({})

// 表单校验
const rules = {
  statDate: [{ required: true, message: '日期不能为空', trigger: 'change' }],
  shiftType: [{ required: true, message: '班次不能为空', trigger: 'change' }],
  brigadeId: [{ required: true, message: '当班大队不能为空', trigger: 'change' }],
  t1TravelLuggageCount: [{ required: true, message: 'T1旅检过检行李数不能为空', trigger: 'change' }],
  t2TravelLuggageCount: [{ required: true, message: 'T2旅检过检行李数不能为空', trigger: 'change' }],
  t1WalkLuggageCount: [{ required: true, message: 'T1行检过检行李数不能为空', trigger: 'change' }],
  t2WalkLuggageCount: [{ required: true, message: 'T2行检过检行李数不能为空', trigger: 'change' }],
  t1TravelBlockedCount: [{ required: true, message: 'T1旅检查堵件数不能为空', trigger: 'change' }],
  t2TravelBlockedCount: [{ required: true, message: 'T2旅检查堵件数不能为空', trigger: 'change' }],
  t1WalkBlockedCount: [{ required: true, message: 'T1行检查堵件数不能为空', trigger: 'change' }],
  t2WalkBlockedCount: [{ required: true, message: 'T2行检查堵件数不能为空', trigger: 'change' }],
  t1ReviewImageTotal: [{ required: true, message: 'T1复查图像总数不能为空', trigger: 'change' }],
  t1AiMarkTotal: [{ required: true, message: 'T1-AI标记总数不能为空', trigger: 'change' }],
  t1AiErrorTotal: [{ required: true, message: 'T1-AI误判总数不能为空', trigger: 'change' }],
  t1AiMissTotal: [{ required: true, message: 'T1-AI漏判总数不能为空', trigger: 'change' }],
  t2ReviewImageTotal: [{ required: true, message: 'T2复查图像总数不能为空', trigger: 'change' }],
  t2AiMarkTotal: [{ required: true, message: 'T2-AI标记总数不能为空', trigger: 'change' }],
  t2AiErrorTotal: [{ required: true, message: 'T2-AI误判总数不能为空', trigger: 'change' }],
  t2AiMissTotal: [{ required: true, message: 'T2-AI漏判总数不能为空', trigger: 'change' }]
  // otherVipCount 不设置必填校验
}

// 大队选项
const brigadeOptions = ref([])

/** 查询每日行李查缉列表 */
function getList() {
  loading.value = true
  listDailyLuggageCheckIn(queryParams).then(response => {
    dailyLuggageCheckInList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 获取部门列表 */
function getDeptList() {
  listDept({}).then(response => {
    const deptList = response.data || []
    brigadeOptions.value = deptList.filter(item => item.deptType === 'BRIGADE' && [311, 314, 315].includes(item.deptId)).map(item => ({
      value: item.deptId,
      label: item.deptName
    }))
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  Object.assign(form, {
    id: null,
    statDate: null,
    shiftType: null,
    brigadeId: null,
    brigadeName: null,
    t1TravelLuggageCount: 0,
    t2TravelLuggageCount: 0,
    t1WalkLuggageCount: 0,
    t2WalkLuggageCount: 0,
    totalLuggageCount: 0,
    t1TravelBlockedCount: 0,
    t1TravelBlockRate: 0,
    t2TravelBlockedCount: 0,
    t2TravelBlockRate: 0,
    t1WalkBlockedCount: 0,
    t1WalkBlockRate: 0,
    t2WalkBlockedCount: 0,
    t2WalkBlockRate: 0,
    totalBlockedCount: 0,
    dailyBlockRate: 0,
    t1ReviewImageTotal: 0,
    t1AiMarkTotal: 0,
    t1AiErrorTotal: 0,
    t1AiMissTotal: 0,
    t2ReviewImageTotal: 0,
    t2AiMarkTotal: 0,
    t2AiErrorTotal: 0,
    t2AiMissTotal: 0,
    otherVipCount: 0,
    aiReviewImageTotal: 0,
    aiMarkTotal: 0,
    aiMissImageTotal: 0,
    aiErrorImageTotal: 0
  })
  proxy.resetForm('dailyLuggageCheckInRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  // 设置默认当前日期
  form.statDate = new Date().toISOString().split('T')[0]
  open.value = true
  title.value = '添加每日行李查缉'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value[0]
  getDailyLuggageCheckIn(id).then(response => {
    Object.assign(form, response.data)
    open.value = true
    title.value = '修改每日行李查缉'
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs.dailyLuggageCheckInRef.validate(valid => {
    if (valid) {
      // 获取brigadeName
      const selectedBrigade = brigadeOptions.value.find(item => item.value === form.brigadeId)
      if (selectedBrigade) {
        form.brigadeName = selectedBrigade.label
      }

      if (form.id != null) {
        updateDailyLuggageCheckIn(form).then(response => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          reset()
          getList()
        })
      } else {
        addDailyLuggageCheckIn(form).then(response => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          reset()
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value
  proxy.$modal.confirm('是否确认删除每日行李查缉编号为"' + id + '"的数据项？').then(function () {
    return delDailyLuggageCheckIn(id)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('/blocked/daily/export', {
    ...queryParams
  }, `每日行李过检行李查堵_${new Date().getTime()}.xlsx`)
}

/** 导入按钮操作 */
function handleImport() {
  upload.open = true
}

/** 下载模板操作 */
function importTemplate() {
  proxy.download('blocked/daily/importTemplate', {}, `每日行李过检行李查堵模板_${new Date().getTime()}.xlsx`)
}

/** 文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
  upload.isUploading = true
}

/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
  upload.open = false
  upload.isUploading = false
  proxy.$refs.uploadRef.handleRemove(file)
  proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true })
  getList()
}

/** 提交上传文件 */
function submitFileForm() {
  proxy.$refs.uploadRef.submit()
}

onMounted(() => {
  getList()
  getDeptList()
})
</script>

<style lang="less" scoped>
.app-container {
  padding: 20px;
}
</style>
