<template>
  <el-config-provider :locale="zhCn">
    <el-container class="content">
      <div class="work-area">
        <fieldset class="search-area">
          <el-form ref="searchQueryFormRef" :model="searchQueryFormEntity" :label-position="labelPosition"
                   label-width="158px" style="max-width: 100%" status-icon>
            <div style="display: flex;justify-content: space-between;">
              <el-row>
                <el-form-item label="查询类型">
                  <el-select-v2 style="width: 196px" v-model="searchQueryFormEntity.queryType" placeholder="查询类型"
                                :options="queryTypeOptions"
                                @change="search()"></el-select-v2>
                </el-form-item>
                <el-form-item label="盘点结果">
                  <el-select-v2 style="width: 196px" v-model="searchQueryFormEntity.invResult" placeholder="盘点结果"
                                :options="addAllOptionOfOptions(invResultOptions, DEFAULT_TOTAL_OPTIONS)"
                                @change="search()"></el-select-v2>
                </el-form-item>
                <el-form-item label="箱号">
                  <el-input v-model="searchQueryFormEntity.vehicleId" @keyup.enter="search()" clearable/>
                </el-form-item>
                <el-form-item label="料号">
                  <el-input v-model="searchQueryFormEntity.goodsId" @keyup.enter="search()" clearable/>
                </el-form-item>
              </el-row>
              <div style="align-content: center;">
                <el-row>
                  <el-button type="primary" class="btn-search" @click="search()">查询</el-button>
                  <el-button type="warning" class="btn-search" @click="clearQuery()">清除输入</el-button>
                </el-row>
              </div>
            </div>
          </el-form>
        </fieldset>
        <div class="table-area">
          <el-table :data="tableData" stripe border v-loading="tableLoading" class="table-class"
                    :max-height="maxHeight" highlight-current-row @row-click="getCurrentRow"
                    :header-cell-style="{ 'text-align': 'center' }" :cell-style="{ 'text-align': 'center' }"
                    @sort-change="handleSortChange">
            <el-table-column width="65px" fixed="left">
              <template v-slot="scope">
                <el-radio :label="scope.row.inventoryId" v-model="inventoryId">&nbsp;</el-radio>
              </template>
            </el-table-column>
            <el-table-column prop="inventoryId" label="任务号" fixed="left" min-width="120px" sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="goodsId" label="料号" min-width="120px"
                             sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="vehicleId" label="箱号" min-width="120px" sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="stockNum" label="库存数量" min-width="120px" sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="confirmNum" label="确认数量"
                             min-width="120px" sortable="custom" show-overflow-tooltip/>
            <el-table-column prop="invStand" label="盘点站台" min-width="120px" sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="invUser" label="盘点人" min-width="120px"
                             sortable="custom" show-overflow-tooltip/>
            <el-table-column prop="invType" label="盘点类型" :formatter="invTypeFormat" min-width="120px" sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="invStatus" label="任务状态" :formatter="invStatusFormat" min-width="120px"
                             sortable="custom" show-overflow-tooltip/>
            <el-table-column prop="invResult" label="盘点结果" :formatter="invResultFormat" min-width="120px" sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="invCreateTime" label="创建时间" :formatter="timeFormat" min-width="120px"
                             sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="invConfirmTime" label="确认时间" :formatter="timeFormat" min-width="120px"
                             sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="invOrderId" label="任务组" min-width="120px"
                             sortable="custom"
                             show-overflow-tooltip/>
          </el-table>
          <br/>
          <el-pagination v-model:current-page="baseTableQuery.currentPage"
                         v-model:page-size="baseTableQuery.pageSize" :page-sizes="[10, 25, 50]" :small="false"
                         :disabled="false" :background="false" :default-page-size="10" @size-change="search"
                         @current-change="search" layout="total, sizes, prev, pager, next, jumper"
                         :total="baseTableQuery.total"/>
        </div>
      </div>
    </el-container>
  </el-config-provider>
</template>

<script setup>
import store from '@/store'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import {queryInventoryRecordByPage} from '@/api/taskQuery.js'
import {timeFormatter} from '@/utils/formatter.js'
import {ref, reactive, onMounted, nextTick, onBeforeUnmount} from 'vue'
import {ElMessage} from 'element-plus'
import {genTableRequest} from '@/utils/generator.js'
import {labelPosition} from '@/constant/form.js'
import {invResultOptions} from '@/constant/options.js'
import {addAllOptionOfOptions} from '@/utils/generator.js'

/**
 * 常量定义
 */
const STAND_ID = store.getters.getStandId
const USER_NAME = store.getters.getUserName
const DEFAULT_TOTAL_OPTIONS = 99
/**
 * 变量定义
 */
let maxHeight = ref(window.innerHeight * 0.55)
let tableLoading = ref(false)
let tableData = ref([])
let baseTableQuery = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0,
  sortBy: new Map([['invCreateTime', true]]),// 按照成品号顺序排序
  standId: STAND_ID,
  userName: USER_NAME
})
let searchQueryFormEntity = reactive({
  vehicleId: '',
  goodsId: '',
  invResult: DEFAULT_TOTAL_OPTIONS,
  queryType: 1
})
let searchQueryFormRef = ref()
let inventoryId = ''
const queryTypeOptions = [
  {
    label: '未关闭',
    value: 1
  },
  {
    label: '已关闭',
    value: 2
  }
]
/**
 * 系统方法
 */
onMounted(() => {
  nextTick(() => {
    window.addEventListener('resize', resizeHeight)
    search()
  })
})
onBeforeUnmount(() => {
  nextTick(() => {
    window.removeEventListener('resize', resizeHeight)
  })
})
const resizeHeight = () => {
  maxHeight.value = window.innerHeight * 0.55
}
/**
 * 自定义方法
 */
// 查询
const search = () => {
  tableLoading.value = true
  let request = genTableRequest(baseTableQuery)
  // 设定查询参数
  request.vehicleId = searchQueryFormEntity.vehicleId.trim()
  request.goodsId = searchQueryFormEntity.goodsId.trim()
  request.invResult = searchQueryFormEntity.invResult === DEFAULT_TOTAL_OPTIONS ? null : searchQueryFormEntity.invResult
  request.queryType = searchQueryFormEntity.queryType// 查询类型
  queryInventoryRecordByPage(request).then((res) => {
    const response = res.data
    if (response.code === 0) {
      const data = response.data
      if (data != null) {
        tableData.value = data.lists
        baseTableQuery.total = data.total
      } else {
        tableData.value = []
        baseTableQuery.total = 0
      }
    } else {
      ElMessage.error(response.message)
    }
  }).catch(err => {
    console.log(err)
    ElMessage.error('查询数据异常。')
  }).finally(() => {
    tableLoading.value = false
  })
}
const clearQuery = () => {
  searchQueryFormEntity.vehicleId = ''
  searchQueryFormEntity.goodsId = ''
}
const handleSortChange = (data) => {
  if (baseTableQuery.sortBy.has(data.prop)) {
    baseTableQuery.sortBy.delete(data.prop)
  }
  baseTableQuery.sortBy.set(data.prop, data.order.toLowerCase() === 'ascending')
  search()
}
const getCurrentRow = (row) => {
  inventoryId = row.inventoryId
}
const timeFormat = (row, column, cellValue, index) => {
  return timeFormatter(cellValue)
}
// 盘点任务状态format
const invStatusFormat = (row, column, cellValue, index) => {
  switch (cellValue) {
    case 0:
      return '初始化'
    case 1:
      return '已解析'
    case 2:
      return '已确认'
    default:
      return '未知状态'
  }
}
// 盘点类型format
const invTypeFormat = (row, column, cellValue, index) => {
  if (cellValue === 1) {
    return '明盘'
  } else if (cellValue === 2) {
    return '盲盘'
  } else {
    return '未知类型'
  }
}
// 盘点结果format
const invResultFormat = (row, column, cellValue, index) => {
  switch (cellValue) {
    case -99:
      return '未盘'
    case -1:
      return '盘亏'
    case 0:
      return '正常'
    case 1:
      return '盘盈'
    default:
      return '未知结果'
  }
}
</script>
<style scoped>
.content {
  display: flex;
  width: 100%;
}

.work-area {
  width: 100%;
  /* padding: 5px; */
}

.search-area {
  margin: auto;
  min-height: fit-content;
  max-height: 40%;
  margin-bottom: 10px;
  min-width: inherit;
  border: solid 1px;
  border-radius: 10px;
  box-shadow: 0px 15px 10px -15px #000;
  overflow: auto;
  padding: 10px;
}

.table-area {
  margin: auto;
  min-height: fit-content;
  max-height: 60%;
  margin-bottom: 10px;
  min-width: inherit;
  border: solid 1px;
  border-radius: 10px;
  box-shadow: 0px 15px 10px -15px #000;
  overflow: auto;
  padding: 10px;
}

.el-form-item {
  margin: 5px 5px 5px 5px;
}

.el-form-item .el-input {
  width: 196px;
}

.el-form-item .el-input-number {
  width: 196px;
}

.table-class {
  margin: 5px 5px 5px 5px;
  width: inherit;
}

.el-pagination {
  padding-left: 5px;
}

.my-autocomplete li {
  width: 196px;
  line-height: normal;
  padding: 7px;
}

.my-autocomplete li .name {
  text-overflow: ellipsis;
  overflow: hidden;
}

.my-autocomplete li .addr {
  font-size: 12px;
  color: #b4b4b4;
}

.my-autocomplete li .highlighted .addr {
  color: #ddd;
}

.my-autocomplete li .goods_id {
  color: brown;
}

.my-autocomplete li .goods_name {
  color: cornflowerblue;
}

.btn-search {
  height: 30px;
  width: 80px;
  margin: auto 5px 5px auto;
  color: black;
}
</style>