<template>
  <div class="app-container">
    <el-form
      ref="queryRef"
      :model="queryParams"
      :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
        key="medicineNo"
        label="药品编码"
        align="center"
        prop="medicineNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="medicineName"
        label="药品名称"
        align="center"
        prop="medicineName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="dispenseQuantity"
        label="待发数量"
        align="center"
        prop="dispenseQuantity"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="unitCode_dictText"
        label="单位"
        align="center"
        prop="unitCode_dictText"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="dispenseEnum_enumText"
        label="处方类型"
        align="center"
        prop="dispenseEnum_enumText"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="patientName"
        label="病人"
        align="center"
        prop="patientName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="prescriptionNo"
        label="处方号"
        align="center"
        prop="prescriptionNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="outpatientNo"
        label="门诊号"
        align="center"
        prop="outpatientNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="admissionNo"
        label="住院号"
        align="center"
        prop="admissionNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="createTime"
        label="开单时间"
        align="center"
        prop="createTime"
        :show-overflow-tooltip="true"
      />
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="awaitingMedicineDetails">
import {getAwaitingPendingMedicationPageList,} 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>