<template>
  <div class="statistics">
    <div style="width:  200px;">
      <el-icon class="iconStyle">
        <PieChart />
      </el-icon>
      <span>商品种数</span>
      <span>{{ datas.totalRow }}</span>
    </div>
    <div style="width:  300px;">
      <el-icon class="iconStyle">
        <SortUp />
      </el-icon>
      <div>
        <div><span>增加数量：</span><span class="datastyle" @click="clickEvent('inSumTotal')">{{ dataAll.dataList.inSumTotal
            }}</span></div>
        <div style="display: flex;">
          <div>
            <span>手动增加：</span><span class="datastyle" @click="clickEvent('winInNum')">{{ dataAll.dataList.winInNum
            }}</span>
          </div>
          <span style="margin: 0 20px;color: #ccc;">|</span>
          <div>
            <span>退货：</span><span class="datastyle" @click="clickEvent('refundInNum')">{{ dataAll.dataList.refundInNum
            }}</span>
          </div>
        </div>
      </div>
    </div>
    <div style="width:  400px;">
      <el-icon class="iconStyle">
        <SortDown />
      </el-icon>
      <div>
        <div><span>减少数量：</span><span class="datastyle" @click="clickEvent('outSumTotal')">{{
          dataAll.dataList.outSumTotal }}</span></div>
        <div style="display: flex;">
          <div>
            <span>手动减少：</span><span class="datastyle" @click="clickEvent('lossOutNum')">{{ dataAll.dataList.lossOutNum
            }}</span>
          </div>
          <span style="margin: 0 20px;color: #ccc;">|</span>
          <div>
            <span>销售量：</span><span class="datastyle" @click="clickEvent('salesNum')">{{ dataAll.dataList.salesNum
            }}</span>
          </div>
          <span style="margin: 0 20px;color: #ccc;">|</span>
          <div>
            <span>报损：</span><span class="datastyle" @click="clickEvent('damageNum')">{{ dataAll.dataList.damageNum
            }}</span>
          </div>
        </div>
      </div>
    </div>
    <!-- 弹窗 -->
    <MyDialog ref="myDialogRef" :title="dataAll.dataTitle" width="70%" @confirm="myDialogRef.close()">
      <!-- 表格 -->
      <Table :list="dataAll.tableData"
        v-if="dataAll.dataType == 'inSumTotal' || dataAll.dataType == 'winInNum' || dataAll.dataType == 'outSumTotal' || dataAll.dataType == 'lossOutNum'">
      </Table>
      <TableTwo :list="dataAll.tableData" v-else-if="dataAll.dataType == 'refundInNum'"></TableTwo>
      <TableThree :list="dataAll.tableData" v-else-if="dataAll.dataType == 'salesNum'"></TableThree>

      <TableFour :list="dataAll.tableData" v-else-if="dataAll.dataType == 'damageNum'"></TableFour>
      <!-- 分页 -->
      <Paging :pagingConfig="dataAll.pagingConfig" @sizeChange="sizeChange" @currentChange="currentChange"></Paging>
    </MyDialog>
  </div>
</template>
<script setup>
import UserAPI from "@/api/product/index";
import MyDialog from "@/components/mycomponents/myDialog.vue";
import Table from './Table.vue'
import TableTwo from './TableTwo.vue'
import TableThree from './TableThree.vue'
import TableFour from './TableFour.vue'
import Paging from './Paging.vue'
import { onMounted, reactive } from "vue";
const props = defineProps({
  data: {
    type: Object,
  }
})
let dataAll = reactive({
  dataList: [],
  tableData: [],
  pagingConfig: {
    total: 0, // 总数
    pageSize: 10, // 每页数据数量
    pageNumber: 1, // 当前页码
  },
  dataType: "",
  dataTitle: ""
});
let datas = reactive({
  totalRow: 0
})
const myDialogRef = ref(null)
onMounted(() => {
  getPage();
})
async function clickEvent(key) {
  dataAll.dataType = key
  let obj = {}
  switch (key) {
    case 'inSumTotal':
      dataAll.dataTitle = '增加数据统计'
      obj = { page: dataAll.pagingConfig.pageNumber, size: dataAll.pagingConfig.pageSize, productId: "", inOutType: "in", inOutItem: "" }
      break;
    case 'winInNum':
      dataAll.dataTitle = '手动增加统计'
      obj = { page: dataAll.pagingConfig.pageNumber, size: dataAll.pagingConfig.pageSize, productId: "", inOutType: "in", inOutItem: "win-in" }
      break;
    case 'refundInNum':
      dataAll.dataTitle = '退货统计'
      obj = { page: dataAll.pagingConfig.pageNumber, size: dataAll.pagingConfig.pageSize, productId: "", inOutType: "in", inOutItem: "order-in" }
      break;
    case 'outSumTotal':
      dataAll.dataTitle = '减少数量统计'
      obj = { page: dataAll.pagingConfig.pageNumber, size: dataAll.pagingConfig.pageSize, productId: "", inOutType: "out", inOutItem: "" }
      break;
    case 'lossOutNum':
      dataAll.dataTitle = '手动减少统计'
      obj = { page: dataAll.pagingConfig.pageNumber, size: dataAll.pagingConfig.pageSize, productId: "", inOutType: "out", inOutItem: "loss-out" }
      break;
    case 'salesNum':
      dataAll.dataTitle = '销量统计'
      obj = { page: dataAll.pagingConfig.pageNumber, size: dataAll.pagingConfig.pageSize, productId: "", inOutType: "out", inOutItem: "order-out" }
      break;
    case 'damageNum':
      dataAll.dataTitle = '报损统计'
      obj = { page: dataAll.pagingConfig.pageNumber, size: dataAll.pagingConfig.pageSize, productId: "", inOutType: "out", inOutItem: "damage-out" }
      break;
  }
  dataAll.tableData = []

  let res = await UserAPI.stockFlow(obj);
  dataAll.tableData = res.records
  dataAll.tableData.forEach((item, index) => {
    if (item.imgUrls.length > 7) {
      item.imagesone = JSON.parse(item.imgUrls)[0]
    }
  })
  dataAll.pagingConfig.total = res.totalRow
  dataAll.pagingConfig.pageSize = res.pageSize
  dataAll.pagingConfig.pageNumber = res.pageNumber
  myDialogRef.value.open()
}
watch(() => props.data, (n) => {
  dataAll.dataList = n
})

async function getPage() {
  let res = await UserAPI.getPage();
  datas.totalRow = res.totalRow
  let ele = await UserAPI.statistics({ id: '', categoryId: '', type: '', name: '' });
  dataAll.dataList = ele
}
function sizeChange(val) {
  dataAll.pagingConfig.pageSize = val
  clickEvent(dataAll.dataType)
}
function currentChange(val) {
  dataAll.pagingConfig.pageNumber = val
  clickEvent(dataAll.dataType)
}
</script>


<style scoped lang="scss">
.statistics {
  border: 1px solid #e4e7ed;
  background-color: #fff;
  border-radius: 4px;
  margin-bottom: 10px;
  padding: 20px;
  display: flex;
  align-items: center;
  justify-content: space-around;

  >div {
    height: 80px;
    background-color: #f4f9ff;
    display: flex;
    align-items: center;
    justify-content: space-around;

    padding: 20px;

    .iconStyle {
      background-color: #d4e9fe;
      border-radius: 50%;
      padding: 6px;
      font-size: 36px;
      color: #3f9eff;
    }

    span {
      color: #666;
      font-size: 14px;
    }
  }
}

.datastyle {
  color: #4080ff !important;
  cursor: pointer;
  text-decoration: underline #4080ff;
}
</style>