<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent
      size="large">
      <el-row>
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="queryParams.materialCode" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="queryParams.materialName" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="物料类型" prop="category">
          <!-- <el-select v-model="queryParams.category" placeholder="请选择" clearable style="width:120px" @change="handleQuery">
            <el-option v-for="dict in options.materialTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue">
              <dict-tag :options="options.materialTypeOptions" :value="dict.dictValue" />
            </el-option>
          </el-select> -->
          <el-radio-group v-model="queryParams.category" @change="handleQuery">
            <el-radio-button>全部</el-radio-button>
            <el-radio-button v-for="dict in options.materialTypeOptions" :label="dict.dictValue">
              {{ dict.dictLabel }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="规格型号 / 图号" prop="materialSpec">
          <el-input v-model="queryParams.materialSpec" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="库区信息" prop="regionId">
          <el-select v-model="queryParams.regionId" placeholder="请选择" clearable @change="handleQuery">
            <el-option v-for="dict in regionList" :key="dict.regionId" :label="dict.regionName" :value="dict.regionId">
              <span v-if="dict.regionName">{{ dict.regionName }}</span>
              <span v-else>（编码：{{ dict.regionCode }}）</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键件标识" prop="keyFlag">
          <!-- <el-select v-model="queryParams.keyFlag" placeholder="请选择" clearable style="width:120px" @change="handleQuery">
            <el-option v-for="dict in options.keyFlagOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue">
              <dict-tag :options="options.keyFlagOptions" :value="dict.dictValue" />
            </el-option>
          </el-select> -->
          <el-radio-group v-model="queryParams.keyFlag" @change="handleQuery">
            <el-radio-button>全部</el-radio-button>
            <el-radio-button v-for="dict in options.keyFlagOptions" :label="dict.dictValue">
              {{ dict.dictLabel }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="BN / SN码" prop="bnSnCode">
          <el-input v-model="queryParams.bnSnCode" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
          <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['business:stkbalancedtl:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="success" :disabled="single" v-hasPermi="['business:stkbalancedtl:edit']" plain icon="edit"
          @click="handleUpdate">
          {{ $t('btn.edit') }}
        </el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['business:stkbalancedtl:delete']" plain icon="delete"
          @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col> -->

      <el-col :span="1.5">
        <el-button plain icon="Printer">打印</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button type="info" plain icon="Upload" @click="handleImport"
            v-hasPermi="['business:basbin:import']">导入</el-button>
        </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport">
          {{ $t('btn.export') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>
    <!-- 数据区域 -->
    <el-table :data="dataList" v-loading="loading" ref="table" highlight-current-row @sort-change="sortChange"
      @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="regionName" label="库区" align="center" v-if="columns.showColumn('regionName')">
        <template #default="scope">
          <span v-if="scope.row.regionName">{{ scope.row.regionName }}</span>
          <span v-else>编码：{{ scope.row.regionCode }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="materialCode" label="物料编码" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('materialCode')" />
      <el-table-column prop="materialName" label="物料名称" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('materialName')" />
      <el-table-column prop="materialSpec" label="规格型号 / 图号" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('materialSpec')" />
      <el-table-column prop="category" label="物料类型" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('category')">
        <template #default="scope">
          <dict-tag :options="options.materialTypeOptions" :value="scope.row.category" />
        </template>
      </el-table-column>
      <el-table-column prop="keyFlag" label="关键件" align="center" v-if="columns.showColumn('keyFlag')">
        <template #default="scope">
          <el-icon v-if="scope.row.keyFlag" size="large" color="red">
            <StarFilled />
          </el-icon>
          <el-icon v-else />
        </template>
      </el-table-column>
      <el-table-column prop="bnSnCode" label="BN / SN码" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('bnSnCode')" />
      <el-table-column prop="sumQty" label="库存数量" align="center" v-if="columns.showColumn('sumQty')" />
      <!-- <el-table-column prop="status" label="库存状态" align="center" v-if="columns.showColumn('status')">
        <template #default="scope">
          <dict-tag :options="stkStatusOptions" :value="scope.row.status" />
        </template>
      </el-table-column> -->
    </el-table>
    <pagination class="mt10" background :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>
<script setup name="stkbalancestat">
import { StatStkBalanceDtl } from '@/api/business/stkbalancedtl';
import { listBasRegion } from '@/api/business/basregion';
const loading = ref(false)
const showSearch = ref(true)
const { proxy } = getCurrentInstance()
const regionList = ref([])
const dataList = ref([])
const total = ref(0)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
})
const columns = ref([
  { visible: false, prop: 'regionName', label: '库区' },
  { visible: true, prop: 'materialCode', label: '物料编码' },
  { visible: true, prop: 'materialName', label: '物料名称' },
  { visible: true, prop: 'materialSpec', label: '规格型号/图号' },
  { visible: true, prop: 'category', label: '物料类型' },
  { visible: true, prop: 'bnSnCode', label: 'BN/SN码' },
  { visible: true, prop: 'keyFlag', label: '关键件' },
  { visible: true, prop: 'sumQty', label: '库存数量' },
  // { visible: true, prop: 'status', label: '库存状态' },
])

const state = reactive({
  options: {
    keyFlagOptions: [], // 关键件标识字典
    materialTypeOptions: [],// 物料类型字典
  }
})

const { options } = toRefs(state)
var dictParams = [
  { dictType: 'my_key_flag', columnName: 'keyFlagOptions' },
  { dictType: 'my_material_type', columnName: 'materialTypeOptions' },
]
proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.columnName] = element.list
  })
})
function getList() {
  loading.value = true
  StatStkBalanceDtl(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}
function getRegion() {
  const regionQuery = reactive({
    pageNum: 1,
    //获取所有的库区，这里限制获取前100个，应该可以获取全部
    pageSize: 100,
  })
  listBasRegion(regionQuery).then(res => {
    const { code, data } = res
    if (code == 200) {
      regionList.value = data.result
    }
  })
}
// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

getRegion()
handleQuery()
</script>
