<template>
  <div class="container-wrapper">
    <div class="top">
      <div class="search">
        <div class="left">
          <el-form :inline="true" :model="searchModel">
            <el-form-item>
              <el-date-picker
                  v-model="selectedDate"
                  type="daterange"
                  range-separator="-"
                  @change="handleDateChange"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
              />
            </el-form-item>
          </el-form>
        </div>
        <div class="right">
          <div class="default-btn" @click="resetSearch">
            <span class="iconfont icon-RectangleCopy1"></span>
            重置
          </div>
          <div class="default-btn" @click="getCheck">
            <span class="iconfont icon-RectangleCopy"></span>
            搜索
          </div>
        </div>
      </div>
      <div class="addBtn">
        <span class="default-btn" @click="openAdd">开始盘点</span>
      </div>
    </div>
    <div class="content_list">
      <el-scrollbar>
        <el-table :data="tableData" @cell-click="editCheck" style="height: 100%" :header-cell-style="{ backgroundColor: '#F1F5FB' }">
          <el-table-column label="订单号" prop="code" width="250">
          </el-table-column>
          <el-table-column label="状态" prop="state" width="250">
            <template #default="scope">
              {{ scope.row.state == 1 ? '已完成' : scope.row.state == 0 ? '进行中' : '未知' }}
            </template>
          </el-table-column>
          <el-table-column label="品种" prop="kindCount" width="100">
            <template #default="scope">
              {{ scope.row.kindCount }}种
            </template>
          </el-table-column>
          <el-table-column label="盈亏数量" prop="changeTotalWholeCount" width="100">
            <template #default="scope">
              {{scope.row.changeTotalWholeCount > 0 ? '+' : ''}}{{ scope.row.changeTotalWholeCount }}
            </template>
          </el-table-column>
          <el-table-column label="盈亏金额(进价)" prop="totalPurchasePrice" width="200">
            <template #default="scope">
              {{scope.row.totalPurchasePrice > 0 ? '+' : ''}}{{ scope.row.totalPurchasePrice }}元
            </template>
          </el-table-column>
          <el-table-column label="盈亏金额(售价)" prop="totalSalePrice" width="200">
            <template #default="scope">
              {{scope.row.totalSalePrice > 0 ? '+' : ''}}{{ scope.row.totalSalePrice }}元
            </template>
          </el-table-column>
          <el-table-column label="盘点人" prop="checkUserName" width="100">
            <template #default="scope">
              {{ scope.row.checkUserName }}
            </template>
          </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";
import {formatDateArray, getEndOfDay} from "@/utils/dateUtils.ts";

const tableData = ref([])
const getCheck = () => {
  const query = {
    pageNum: page.value,
    pageSize: pageSize.value,
    ...searchModel.value,
  }
  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 selectedDate = ref<any>([])
const handleDateChange = (date: any[]) => {
  selectedDate.value = formatDateArray(date)
  if (selectedDate.value[0] == selectedDate.value[1]) {
    selectedDate.value[1] = getEndOfDay(selectedDate.value[1]); // 输出今天 23:59
  }
  searchModel.value.beginTime = selectedDate.value[0]
  searchModel.value.endTime = selectedDate.value[1]
}

const searchModel = ref({
  beginTime: null,
  endTime: null,
})
const resetSearch = () => {
  searchModel.value = {
    beginTime: null,
    endTime: null,
  }
}
</script>
<style scoped lang="scss">
@use "../../assets/scss/base.scss";

.container-wrapper {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 24px 24px 0;
  border-radius: 8px;

  .content_list {
    flex: 1;
    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 {
  background: #fff;
  display: flex;
  flex-direction: column;
  margin-bottom: 24px;

  .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 {
      &:first-child{
        margin-left: 0;
      }
    }
  }
}
:deep(.el-input__wrapper) {
  height: 42px;
}
:deep(.el-range-editor.el-input__wrapper) {
  height: 42px;
}

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