<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>
      <el-date-picker
          v-model="value1"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
      />
      <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="type" label="库存变更类型" show-overflow-tooltip>
          <template #default="{row}">
            {{ typeMapping[row.type] }}
          </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="beforeFragmentNumber" label="前库存" show-overflow-tooltip>
          <template #default="{row}">
            {{ row.beforeWholeNumber }}{{ row.packagingUnit }}
            <template v-if="row.changeFragmentNumber!=0">
              {{ row.beforeFragmentNumber }}{{ row.minPackagingUnit }}
            </template>
          </template>
        </el-table-column>

        <el-table-column prop="changeWholeNumber" label="变化库存" show-overflow-tooltip>
          <template #default="{row}">
            <el-icon v-if="row.changeWholeNumber<0" style="font-size: 9px">
              <Minus/>
            </el-icon>
            <el-icon v-if="row.changeWholeNumber>0" style="font-size: 9px">
              <Plus/>
            </el-icon>
            <span>
              {{ Math.abs(row.changeWholeNumber) }}{{ row.packagingUnit }}
              <template v-if="row.changeFragmentNumber!=0">
                {{ Math.abs(row.changeFragmentNumber) }}{{ row.minPackagingUnit }}
              </template>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="afterFragmentNumber" label="后库存" show-overflow-tooltip>
          <template #default="{row}">
            {{ row.afterWholeNumber }}{{ row.packagingUnit }}
            <template v-if="row.changeFragmentNumber!=0">
              {{ row.afterFragmentNumber }}{{ row.minPackagingUnit }}
            </template>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip>
          <template #default="{row}">
            {{ formatDate(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="uploadStatus" label="状态" show-overflow-tooltip>
          <template #default="{row}">
            {{ uploadStatus[row.uploadStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="fragmentNumber" label="上传状态信息" show-overflow-tooltip/>
        <el-table-column prop="uploadMessage" label="最近上传时间" show-overflow-tooltip>
          <template #default="{row}">
            {{ formatDate(row.uploadMessage) }}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" show-overflow-tooltip/>
      </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, Search} from "@element-plus/icons-vue";

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

// 搜索相关
let type = ref<number>()
let typeName = ref('')
const searchInfo = ref({
  socialType: '',
  keyword: '',
})
const searchStatus = ref<any>({
  socialType: '',
  keyword: '',
})
const value1 = ref([])

// 分页相关
const currentPage = ref(1)// 当前页码
const pageSize = ref(20)// 每页显示的记录数
const total = ref(0)

// 定义 type 与 typeName
interface TypeMapping {
  [key: number]: string;
}

const socialTypeMapping: TypeMapping = {
  101: '调拨入库',
  102: '调拨出库',
  103: '盘盈',
  104: '盘损',
  105: '销毁',
  106: '其他入库',
  107: '其他出库',
  108: '初始化入库'
}
const typeMapping: TypeMapping = {
  1: "初始化",
  2: "采购入库",
  3: "盘盈入库",
  4: "发药出库",
  5: "领用出库",
  6: "报损出库",
  7: "盘亏出库",
  8: "退货出库",
  9: "零售退款入库"
}

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

const uploadStatus: UploadStatus = {
  0: '未上传',
  1: '已上传',
  2: '异常',
  3: "无需上报",
}

onMounted(() => {
  init()
})

let init = () => {
  const query = {
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    socialType: Number(searchInfo.value.socialType) || null,
    uploadStatus: searchStatus.value.uploadStatus || null,
    createTimeBefore: value1.value ? formatDate(value1.value[0]) : null,
    createTimeAfter: value1.value ? formatDate(value1.value[1]) : null,
  }
  post("social/upload/get3502List", {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/get3502List", {query: query}).then((res: any) => {
    tableData.value = res.list
    total.value = res.total_count
  })
}

let change_search = () => {
  init()
}
// 转成YYYY-MM-DD格式
const formatDate = (isoStr: any) => {
  if (!isoStr) {
    return ''
  }
  const date = new Date(isoStr);
  return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
}
</script>


<style scoped lang="scss">
@use "@/assets/scss/base.scss";
.container {
  display: flex;
  flex-direction: column;
  height: 100%;
  .top {
    height: 60px;
    .search {
      width: 300px;
    }
  }
  .content {
    flex: 1;
    min-height: 0;
  }
  .bottom {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
}

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