<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="开单时间：">
        <el-date-picker
          v-model="occurrenceTime"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 300px"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="药品查询：" prop="searchKey">
        <el-input
          v-model="queryParams.searchKey"
          placeholder="输入名称/拼音/编码后回车查询"
          clearable
          style="width: 220px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统计类型：">
        <el-select
          v-model="inventoryScope"
          placeholder=""
          clearable
          style="width: 150px"
          disabled
        >
          <el-option
            v-for="supplyStatus in inventoryOptions"
            :key="supplyStatus.value"
            :label="supplyStatus.label"
            :value="supplyStatus.value"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- v-hasPermi="['system:user:import']" -->
        <el-button
          type="primary"
          plain
          icon="Search"
          @click="handleQuery"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <!-- v-hasPermi="['system:user:export']" -->
        <el-button
          type="warning"
          plain
          icon="CircleClose"
          @click="handleClear"
          
          >重置</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="purchaseinventoryList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column
        label="药品编码"
        align="center"
        key="medicineNo"
        prop="medicineNo"
        :show-overflow-tooltip="true"
      />
       <el-table-column
        label="药品名称"
        align="center"
        key="medicineName"
        prop="medicineName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="待发数量"
        align="center"
        key="dispenseQuantity"
        prop="dispenseQuantity"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="单位"
        align="center"
        key="unitCode_dictText"
        prop="unitCode_dictText"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="处方类型"
        align="center"
        key="dispenseEnum_enumText"
        prop="dispenseEnum_enumText"
        :show-overflow-tooltip="true"
      />
       <el-table-column
        label="病人"
        align="center"
        key="patientName"
        prop="patientName"
        :show-overflow-tooltip="true"
      />
       <el-table-column
        label="处方号"
        align="center"
        key="prescriptionNo"
        prop="prescriptionNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="门诊号"
        align="center"
        key="outpatientNo"
        prop="outpatientNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="住院号"
        align="center"
        key="admissionNo"
        prop="admissionNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="开单时间"
        align="center"
        key="createTime"
        prop="createTime"
        :show-overflow-tooltip="true"
      />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="awaitingMedicineDetails">
import {
  getAwaitingPendingMedicationPageList,
  getPharmacyCabinetList,
} from "./statisticalManagent";
import useUserStore from "@/store/modules/user";
import { ref } from "vue";
const userStore = useUserStore();
const { proxy } = getCurrentInstance();
const inventoryOptions = ref([])
const totalAmount = ref(0);
const purchaseinventoryList = ref([]);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const occurrenceTime = ref([]);
const inventoryScope = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    searchKey: undefined, 
    // startTime:undefined, 
    // endTime: undefined,
    // inventoryScope: undefined,
    createTimeSTime:undefined,
    createTimeETime:undefined,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);
watch(
  () => purchaseinventoryList.value,
  (newVlaue) => {
    if(newVlaue&&newVlaue.length>0){
        handleTotalAmount()
    }
  },
  { immediate: true }
);
/**计算合计金额 */
function handleTotalAmount() {
  totalAmount.value = purchaseinventoryList.value.reduce(
    (accumulator, currentRow) => {
      return (accumulator + (Number(currentRow.totalPrice) || 0))
    },
    0
  );
}

function getPharmacyCabinetLists() {
  // 统计类型
  inventoryOptions.value = [
    {value:1,label:'待发药明细账'},
  ]
  inventoryScope.value = 1
}
/** 查询调拨管理项目列表 */
function getList() {
  loading.value = true;
  getAwaitingPendingMedicationPageList(queryParams.value).then((res) => {
    loading.value = false;
    purchaseinventoryList.value = res.data.records
    total.value = res.data.total;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.createTimeSTime =
    occurrenceTime.value && occurrenceTime.value.length == 2
      ? occurrenceTime.value[0] + " 00:00:00"
      : "";
  queryParams.value.createTimeETime =
    occurrenceTime.value && occurrenceTime.value.length == 2
      ? occurrenceTime.value[1] + " 23:59:59"
      : "";
  queryParams.value.pageNo = 1;
  getList();
}

/** 清空条件按钮操作 */
function handleClear() {
  queryParams.value.createTimeSTime = ""
  queryParams.value.createTimeETime = ""
  occurrenceTime.value = ""
  // inventoryScope.value = ""
  // 清空查询条件
  proxy.resetForm("queryRef");
  getList();
}

/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


getList();
getPharmacyCabinetLists()
</script>
<style scoped>
.custom-tree-node {
  display: flex;
  align-items: center;
}

.title {
  font-weight: bold;
  font-size: large;
  margin-bottom: 10px;
}
</style>