<template>
  <div class="top">
    <div>
      <el-date-picker
          v-model="dateList"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changeDate"
          unlink-panels
      >
      </el-date-picker>
    </div>
    <div style="display: flex;align-items: center;">
      <el-radio-group v-model="type" @change="changeTime">
        <el-radio-button label="商品" value="1"/>
        <el-radio-button label="流水" value="2"/>
      </el-radio-group>
      <el-dropdown placement="bottom-end">
         <span class="el-dropdown-link">
            <el-button type="primary" plain :icon="Setting" circle style="margin-left: 5px"/>
         </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>按批次维度统计</el-dropdown-item>
            <el-dropdown-item>按商品维度统计</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
  <el-table v-if="type=='1'" :data="list" style="width: 100%">
    <el-table-column prop="name" label="项目名" width="180"/>
    <el-table-column prop="wholeNumber" label="数量(大包装)" width="180"/>
    <el-table-column prop="fragmentNumber" label="数量(小包装)"/>
    <el-table-column prop="costPrice" label="成本"/>
    <el-table-column prop="sellingPrice" label="售价"/>
  </el-table>
  <el-table v-if="type=='2'" :data="list" style="width: 100%">
    <el-table-column prop="createTime" label="创建时间" width="180"/>
    <el-table-column prop="type" label="类型" width="180">
      <template #default="{row}">
        {{ typeMapping[row.type]||'-' }}
      </template>
    </el-table-column>
    <el-table-column prop="beforeWholeNumber" label="前库存">
      <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="变化库存">
      <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="afterWholeNumber" label="后库存">
      <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="socialType" label="库存类型">
      <template #default="{row}">
        {{ typeSocial[row.socialType] || '-' }}
      </template>
    </el-table-column>
    <el-table-column prop="remark" label="备注"/>
  </el-table>
</template>

<style scoped lang="scss">

</style>

<script setup lang="ts">
import {post} from "@/utils/request.ts";
import {onMounted, ref, defineProps} from "vue";
import {Minus, Plus, Setting} from "@element-plus/icons-vue";
import {API} from "@/assets/config/API.ts";

const props = defineProps({
  id: {
    type: Number,
    default: 0
  }
})
const dateList = ref<any>([])
const list = ref<any>([])
const formatDate = (dateString: any) => {
  const date = new Date(dateString);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
};

const changeDate = (date: any) => {
  dateList.value = date
  init()
}
const getDate = () => {
  const now = new Date();
  const endDate = formatDate(now); // 当前日期
  const startDate = new Date(now);
  startDate.setDate(now.getDate() - 30); // 前三十天
  dateList.value = [formatDate(startDate), endDate];
}
onMounted(() => {
  getDate()
  init()
})
const init = () => {
  if (!props.id) return
  post(API.Inventory.Goods.Statistics, {
    goodsId: props.id,
    startDate: formatDate(dateList.value[0]),
    endDate: formatDate(dateList.value[1])
  }).then(res => {
    list.value = res
  })
}
const type = ref('1')
const changeTime = () => {
  if (type.value == '1') {
    init()
  } else {
    post(API.Inventory.Goods.Log, {
      goodsId: props.id,
      startDate: formatDate(dateList.value[0]),
      endDate: formatDate(dateList.value[1])
    }).then(res => {
      list.value = res
    })
  }

}
interface TypeMapping {
  [key: number]: string;
}
const typeMapping: TypeMapping = {
  1: '初始化',
  2: '采购入库',
  3: '盘盈入库',
  4: '发药出库',
  5: '领用出库',
  6: '报损出库',
  7: '盘亏出库',
  8: '退货出库',
  9: '零售退款入库',

}
const typeSocial: TypeMapping = {
  101: '调拨入库',
  102: '调拨出库',
  103: '盘盈',
  104: '盘损',
  105: '销毁',
  106: '其他入库',
  107: '其他出库',
  108: '初始化入库'
}
</script>
<style scoped lang="scss">
.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
