<template>
  <div class="container-wrapper">
    <div class="top">
      <el-dropdown>
        <el-button type="primary" :icon="Plus" @click="openAdd">开始盘点</el-button>
      </el-dropdown>
    </div>
    <div class="content_list">
      <el-table :data="tableData" @cell-click="editCheck">
        <el-table-column label="订单号" prop="code" width="250">
        </el-table-column>
        <el-table-column label="状态" prop="state" width="250"></el-table-column>
        <el-table-column label="创建时间" prop="createDatetime" width="250">
          <template #default="scope">
            {{ formatDate(scope.row.createDatetime) }}
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark"></el-table-column>
      </el-table>
    </div>
    <div class="bottom">
      <div class="page_btn_list">
        <el-pagination
            background
            layout="prev, pager, next"
            :page-size="pageSize"
            :current-page="page"
            :total="total"
            @current-change="changePage"
        />
      </div>
    </div>
  </div>
  <Mask :width="1200" :height="540" :is-show="showAdd" :top="100" @close="showAdd = false" title="新增盘点">
    <Add @close="showAdd = false;getCheck()"/>
  </Mask>
  <Mask :width="1200" :height="540" :is-show="showDetail" :top="100" @close="showDetail = false" title="查看盘点">
    <Detail :id="id" @close="showDetail = false;getCheck()"/>
  </Mask>
</template>
<script setup lang="ts">
import {onMounted, ref} from "vue";
import {post} from "@/utils/request.ts";
import Mask from "@/components/common/Mask.vue";
import Add from "@/components/inventory/check/Add.vue";
import Detail from "@/components/inventory/check/Detail.vue";
import {Plus} from "@element-plus/icons-vue";

const tableData = ref([])
const getCheck = () => {
  const query = {
    pageNum: 1,
    pageSize: 20
  }
  post("inventory/check/list", {query: query}).then(
      (res: any) => {
        tableData.value = res.list
      }
  )
}
const detailCheckRef = ref<any>('');
const showAdd = ref(false)
const showDetail = ref(false)
const id = ref('')
const openAdd = () => {
  showAdd.value = true
}
const editCheck = (row: any) => {
  id.value = row.id
  showDetail.value = true
}

onMounted(() => {
  getCheck()
})
//分页
let pageSize = ref(20)
let total = ref(0)
let page = ref(1)
let changePage = (value: number) => {
  page.value = value
  const query = {
    pageNum: value,
    pageSize: pageSize.value,
  }
  post("goods/goods/searchDetail", {query: query}).then((res: any) => {
    tableData.value = res.list
    total.value = res.total_count
  })
}
// 转成YYYY-MM-DD格式
const formatDate = (isoStr: any) => {
  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-wrapper {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 24px;

  .content_list {
    flex: 1;
    overflow: hidden;
    margin-top: base.$margin-base;
  }

  .bottom {
    height: 60px;
    background-color: #FFF;
    box-sizing: border-box;
    padding: 10px;
    position: relative;
    border-top: 1px solid #EEE;
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
}
</style>