<template>
  <div class="container-wrapper">
    <div class="top">
      <div class="search">
        <div class="left">
          <el-form :inline="true" :model="searchModel">
            <el-form-item>
              <el-input v-model="searchModel.code" placeholder="请输入单号" style="width: 180px;height: 42px"
                        :prefix-icon="Search"></el-input>
            </el-form-item>
            <el-form-item>
            </el-form-item>
          </el-form>
        </div>
        <div class="right">
          <div class="btn" @click="resetSearch">
            <el-icon style="margin-right: 10px">
              <Refresh/>
            </el-icon>
            重置
          </div>
          <div class="btn" @click="getCheck">
            <el-icon style="margin-right: 10px">
              <Search/>
            </el-icon>
            搜索
          </div>
        </div>
      </div>
      <div class="addBtn">
        <span @click="openAdd">开始盘点</span>
      </div>
    </div>
    <div class="content_list">
      <el-scrollbar>
        <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>
      </el-scrollbar>
    </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>
  <Add ref="addRef" @close="getCheck()"/>
  <Detail ref="detailRef" @close="getCheck()"/>
</template>
<script setup lang="ts">
import {nextTick, 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, Refresh, Search} from "@element-plus/icons-vue";

const tableData = ref([])
const getCheck = () => {
  const query = {
    pageNum: page.value,
    pageSize: 20
  }
  post("inventory/check/list", {query: query}).then(
      (res: any) => {
        tableData.value = res.list
        total.value = res.total_count
      }
  )
}
const detailCheckRef = ref<any>('');
const showAdd = ref(false)
const showDetail = ref(false)
const id = ref('')
const addRef = ref<any>('');
const openAdd = () => {
  nextTick(() => {
    addRef.value?.init()
  })
}
const detailRef = ref<any>('');
const editCheck = (row: any) => {
  nextTick(() => {
    detailRef.value?.detail(row.id)
  })
}

onMounted(() => {
  getCheck()
})
//分页
let pageSize = ref(20)
let total = ref(0)
let page = ref(1)
let changePage = (value: number) => {
  page.value = value
  getCheck()
}
// 转成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')}`;
}
const searchModel = ref({
  code: ''
})
const resetSearch = () => {
  searchModel.value = {
    code: ''
  }
}
</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;
    margin-top: base.$margin-base;
    min-height: 0;
  }

  .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;
  }
}

.top {
  height: 110px;
  background: #fff;
  display: flex;
  flex-direction: column;

  .search {
    display: flex;
    justify-content: space-between;

    .left {
      flex: 1;
      display: flex;
      justify-content: space-between;
      margin-right: 24px;

      .el-form-item {
        margin-right: 5px;
      }
    }

    .right {
      display: flex;

      .btn {
        width: 120px;
        height: 42px;
        background: #FFFFFF;
        border-radius: 6px;
        border: 1px solid #979797;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-left: 24px;
        cursor: pointer;

        &:hover {
          background: #4D6DE4;
          color: #fff;
          border: none;
        }
      }
    }

  }

  .addBtn {
    span {
      display: inline-block;
      width: 120px;
      height: 42px;
      background: #FFFFFF;
      border-radius: 6px;
      border: 1px solid #4D6DE4;
      margin-right: 24px;
      font-weight: 500;
      font-size: 16px;
      color: #4D6DE4;
      text-align: center;
      line-height: 42px;
      cursor: pointer;

      &:hover {
        background: #4D6DE4;
        color: #fff;
        border: none;
      }
    }
  }
}

:deep(.el-range-editor.el-input__wrapper) {
  height: 42px;
}

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