<template>
  <div class="app-container">
    <div class="search-con">
      <div class="title">查询条件</div>
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="药品名称" style="width: 480px" prop="medicineId">
          <el-select v-model="queryParams.medicineId" width="500" placeholder="请选择药品名称" clearable>
            <el-option v-for="medicine in medicineList" :key="medicine.id" :label="medicine.shortNameBrandPackaging" :value="medicine.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div class="search-btn-con">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="Refresh" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <div class="main-con">
      <div class="title-con">
        <div class="title">基本信息</div>
      </div>
      <div class="content-con" v-loading="loading" height="calc(100% - 0.65rem)">
        <el-table v-loading="loading" :data="medicineStockInList" @selection-change="handleSelectionChange" height="calc(100% - 0.65rem)">
          <el-table-column type="index" label="序号" :index="indexMethod" width="50"></el-table-column>
          <el-table-column label="药品名称" align="center" prop="medicineName" />
          <el-table-column label="规格总数" align="center" prop="totalCount" />
          <el-table-column label="剩余数量" align="center" prop="leftCount" />
          <el-table-column label="规格单位" align="center" prop="unit">
            <template #default="scope">
              <dict-tag :options="medical_unit" :value="scope.row.unit" />
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          layout="total,sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup name="MedicineStockIn">
import { listMedicineRealtimeStock } from '@/api/health/medicineStockIn'
// eslint-disable-next-line no-unused-vars
import { listMedicineBasic, getMedicineBasic } from '@/api/health/medicineBasic'
import { require } from '@/utils/require'
const { proxy } = getCurrentInstance()
const { medical_unit } = proxy.useDict('medical_unit')

const medicineStockInList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const medicineList = ref([])
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    medicineId: null,
    quantity: null,
    productionDate: null,
    expiringDate: null,
    purchaseDate: null,
    purchasePrice: null,
    code: null,
    state: null,
    leftCount: null,
    usedCount: null,
    purchaseAddress: null
  },
  queryMedicineParams: {
    pageNum: 1,
    pageSize: 1000
  }
})

const { queryParams, form, queryMedicineParams } = toRefs(data)

/** 查询药品入库清单列表 */
function getList() {
  loading.value = true
  listMedicineRealtimeStock(queryParams.value).then((response) => {
    medicineStockInList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 查询药品管理列表 */
function getMedicineList() {
  listMedicineBasic(queryMedicineParams.value).then((response) => {
    medicineList.value = response.rows
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 分页
const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  getList()
}

//每页显示条数改变
const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  getList()
}
// 序号翻页递增
const indexMethod = (index) => {
  const nowPage = queryParams.value.pageNum //当前第几页，根据组件取值即可
  const nowLimit = queryParams.value.pageSize //当前每页显示几条，根据组件取值即可
  return index + 1 + (nowPage - 1) * nowLimit // 这里可以理解成一个公式
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

// 查看
const handleView = (row) => {
  title.value = '查看药品入库清单'
  form.value = row
  open.value = true
}

getList()
getMedicineList()
</script>
