<template>
  <div class="container">
    <div class="tip">
      <el-select
          v-model="searchInfo.socialType"
          placeholder="变更类型"
          clearable style="width: 240px;" @clear="init()">
        <el-option
            v-for="(item,key) in socialTypeMapping"
            :key="key"
            :label="item"
            :value="key"
        />
      </el-select>
      <el-select
          v-model="searchStatus.uploadStatus"
          placeholder="上传状态"
          clearable style="width: 240px;margin: 0 10px" @clear="init()">
        <el-option
            v-for="(item,key) in uploadStatus"
            :key="key"
            :label="item"
            :value="key"
        />
      </el-select>
      <div class="default-btn" @click="change_search" style="margin-left: 24px">
        <span class="iconfont icon-RectangleCopy"></span>
        搜索
      </div>
    </div>
    <div class="content">
      <el-table :data="tableData" style="width: 100%;height: 100%">
        <el-table-column prop="name" label="商品名称" show-overflow-tooltip/>
        <el-table-column prop="number" label="进货，退货数量" show-overflow-tooltip>
          <template #default="{row}">
            <el-icon v-if="row.number<0" style="font-size: 9px">
              <Minus/>
            </el-icon>
            <el-icon v-if="row.number>0" style="font-size: 9px">
              <Plus/>
            </el-icon>
            <span>
              <template v-if="row.number!=0">
                {{ Math.abs(row.number) }}{{ row.packagingUnit }}
              </template>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="socialType" label="医保库存变更类型" show-overflow-tooltip>
          <template #default="{row}">
            {{ socialTypeMapping[row.socialType] }}
          </template>
        </el-table-column>
        <el-table-column prop="uploadStatus" label="状态" show-overflow-tooltip>
          <template #default="{row}">
            {{ uploadStatus[row.uploadStatus as keyof typeof uploadStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="uploadMessage" label="上传状态信息" show-overflow-tooltip/>
        <el-table-column prop="uploadDatetime" label="最近上传时间" show-overflow-tooltip>
          <template #default="{row}">
            {{ formatDate(row.uploadDatetime) }}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="bottom">
      <el-pagination
          background
          layout="prev, pager, next"
          :page-size="pageSize"
          :current-page="currentPage"
          :total="total"
          @current-change="changePage"
      />
    </div>
  </div>
</template>
<script setup lang="ts">
import {ref, computed, onMounted} from 'vue'
import {ElInput, ElTable, ElTableColumn, ElPagination} from 'element-plus'
import {post} from "@/utils/request.ts";
import {Minus, Plus} from "@element-plus/icons-vue";
import {formatDate} from "@/utils/dateUtils.ts";

// 模拟数据
const tableData = ref([])

// 搜索相关
const searchInfo = ref<any>({})
const searchStatus = ref<any>({})

interface TypeMapping {
  [key: number]: string;
}

// 定义 type 与 typeName 的映射关系
const socialTypeMapping: TypeMapping = {
  101: '调拨入库',
  102: '调拨出库',
  103: '盘盈',
  104: '盘损',
  105: '销毁',
  106: '其他入库',
  107: '其他出库',
  108: '初始化入库'
}
const uploadStatus = {
  0: '未上传',
  1: '已上传',
  2: '异常',
  3: '无需上报',
}

// 分页相关
const currentPage = ref(1)
const pageSize = ref(20)
const total = ref(0)
onMounted(() => {
  init()
})
let init = () => {
  const query = {
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    socialType: searchInfo.value.socialType || null,
    uploadStatus: searchStatus.value.uploadStatus || null,
  }
  post("social/upload/get3503List", {query: query}).then((res: any) => {
    tableData.value = res.list
    total.value = res.total_count
  })
}
let changePage = (value: number) => {
  currentPage.value = value
  const query = {
    pageNum: value,
    pageSize: pageSize.value,
  }
  post("social/upload/get3503List", {query: query}).then((res: any) => {
    tableData.value = res.list
    total.value = res.total_count
  })
}
let change_search = () => {
  init()
}
</script>

<style scoped lang="scss">
@use "@/assets/scss/base.scss";

.container {
  height: 100%;
  display: flex;
  flex-direction: column;

  .top {
    height: 60px;
  }

  .content {
    flex: 1;
    min-height: 0;
  }

  .bottom {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: flex-end;

  }
}

:deep(.el-select__wrapper) {
  height: 42px;
}
</style>