<template>
  <div>
    <el-form inline :model="searchForm" class="-mb-15px" label-width="0">
      <el-form-item>
        <el-select
          v-model="searchForm.productId"
          placeholder="选择产品"
          clearable
          filterable
          @change="changeProd"
        >
          <el-option
            v-for="item in opts.product"
            :key="item.productId"
            :label="item.productName"
            :value="item.productId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="searchForm.specsId"
          placeholder="选择规格"
          clearable
          filterable
          :disabled="!searchForm.productId"
        >
          <el-option
            v-for="item in opts.spec"
            :key="item.specsId"
            :label="item.specsName"
            :value="item.specsId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.warehouseId" placeholder="仓库" clearable filterable>
          <el-option
            v-for="item in opts.warehouse"
            :key="item.warehouseId"
            :label="item.warehouseName"
            :value="item.warehouseId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleSearch"> 搜索 </el-button>
        <el-button @click="resetQuery"> 重置 </el-button>
      </el-form-item>
    </el-form>

    <el-table class="mt-20px" :data="tableList" border>
      <el-table-column type="index" width="50px" />
      <el-table-column prop="productName" label="产品名称" />
      <el-table-column prop="specsName" label="规格" />
      <el-table-column label="变动类型">
        <template #default="{ row }">
          {{ ['', '入库', '出库'][row.changeType] }}
        </template>
      </el-table-column>
      <el-table-column prop="num" label="数量" />
      <!-- <el-table-column prop="money" label="金额" /> -->
      <el-table-column prop="changeTime" label="变动时间" :formatter="dateFormatter" />
      <el-table-column prop="changeUserName" label="变动人员" />
      <el-table-column prop="warehouseName" label="所属仓库" />
      <el-table-column prop="remark" label="备注" />
    </el-table>
    <Pagination
      v-model:limit="searchForm.pageSize"
      v-model:page="searchForm.pageNo"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="InventoryDetail">
import { dateFormatter } from '@/utils/formatTime'
import { getSimpleWarehouseList, getInventoryRecord } from '@/api/mall/warehouse'
import { getSimpleProductList } from '@/api/mall/product'

const searchForm = ref({
  pproductId: undefined,
  specsId: undefined,
  warehouseId: undefined,
  pageNo: 1,
  pageSize: 20
})

const opts = ref({
  product: [],
  spec: [],
  warehouse: []
})

const loading = ref(false)
const total = ref(0)
const tableList = ref([])

function resetQuery() {
  searchForm.value = {
    productId: undefined,
    specsId: undefined,
    warehouseId: undefined,
    pageNo: 1,
    pageSize: 20
  }
}

function getOptions() {
  getSimpleProductList().then((data) => {
    opts.value.product = data
  })
  getSimpleWarehouseList().then((data) => {
    opts.value.warehouse = data
  })
}

function handleSearch() {
  searchForm.value.pageNo = 1
  getList()
}

function changeProd(val) {
  if (val) {
    opts.value.spec = opts.value.product.find((it) => it.productId == val).productSpecList
  } else {
    opts.value.spec = []
  }
  searchForm.value.specsId = undefined
}

async function getList() {
  loading.value = true
  try {
    const data = await getInventoryRecord(searchForm.value)
    tableList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  getOptions()
  handleSearch()
})
</script>

<style lang="scss" scoped></style>
