<template>
  <!-- 药品 库存  -->
  <el-dialog :title="title" @close="openClose()" v-model="openPrice" :width="`${style.width}`">
    <el-form :model="queryParamsDrugArchives" ref="queryRef" label-width="68px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="药品名称" prop="medicineName">
            <el-input v-model="queryParamsDrugArchives.medicineName" placeholder="请输入药品名称" clearable
              @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="条码" prop="barcode">
            <el-input v-model="queryParamsDrugArchives.barcode" placeholder="请输入条码" clearable
              @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!--  -->
    <el-row>
      <el-col :span="24">
        <!-- 表格  -->
        <el-table v-loading="loading" :data="drugArchivesList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="编号" align="center" prop="filingId" />
          <el-table-column label="药品名称" align="center" prop="medicineName" />
          <el-table-column label="入库点" align="center" prop="entryPoint" />
          <el-table-column label="生产厂家" align="center" prop="manufacturer" />
          <el-table-column label="条码" align="center" prop="barcode" />
          <el-table-column label="药品规格" align="center" prop="specification" />
          <!--  -->
          <el-table-column label="盒数" align="center" prop="boxCount" />
          <el-table-column label="包数" align="center" prop="packCount" />
          <el-table-column label="零售价" align="center" prop="filingRetailPrice">
            <template #default="scope">
              {{ scope.row.filingRetailPrice }}
            </template>
          </el-table-column>
          <!--  -->
          <el-table-column label="是否允许拆分" align="center" prop="isSplit">
            <template #default="scope">
              <dict-tag :options="sys_yes_no" :value="scope.row.isSplit" />
            </template>
          </el-table-column>
          <el-table-column label="最小单位数量" align="center" prop="minNumber" />
          <el-table-column label="最小单位价格" align="center" prop="minNumberRetailPrice">
            <template #default="scope">
              {{ scope.row.minNumberRetailPrice }}
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="state">
            <template #default="scope">
              {{ scope.row.state == 0 ? "固态" : "液态" }}
            </template>
          </el-table-column>
          <el-table-column label="最小包装数量" align="center" prop="unitQuantity" />
          <el-table-column label="最小包装单位" align="center" prop="splitUnit" />
          <el-table-column label="包装单位" align="center" prop="packageUnit" />
          <!-- 有效期至 -->
          <el-table-column label="有效期至" align="center" prop="expiryDate" />
          <el-table-column label="批次" align="center" prop="batch" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="selectValue(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination v-show="total > 0" :total="total" v-model:page="queryParamsDrugArchives.pageNum"
          v-model:limit="queryParamsDrugArchives.pageSize" @pagination="getList" />
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script setup name="DrugArchives">
import {
  gainDrugPriceList,
  gainMedicineArchiveList,
  gainDrugInventoryList,
} from "@/api/healthcare/outpatientService/patientDiagnosis";
const { proxy } = getCurrentInstance();
// 医药包装单位
const { healthcare_packaging_unit } = proxy.useDict("healthcare_packaging_unit");
// 是否 系统
const { sys_yes_no } = proxy.useDict("sys_yes_no");

const drugArchivesList = ref([]);
const loading = ref(false);
const total = ref(0);
const props = defineProps({
  open: {
    type: Boolean,
    onUpdate: Function,
  },
  style: {
    type: Object,
    onUpdate: Function,
    // default: () => ({
    //   width: "100%",
    // })
  },
  // 药品 名称
  medicineName: {
    type: String,
    onUpdate: Function,
  },
  // 请求  参数
  queryParams: {
    type: Object,
    onUpdate: Function,
    default: () => ({
      pageNum: 1,
      pageSize: 10,
      medicineName: null,
    }),
  },
});

// 监听 父组件传来的数值  DrugPrice
watch(
  () => props.queryParams.medicineName,
  (newValue, oldValue) => {
    console.log("获取到的药品名称", newValue);
    queryParamsDrugArchives.value.medicineName = newValue;
    getList();
  }
);

// 数据 绑定
const openPrice = ref(props.open);

// 数据 绑定
const queryParamsDrugArchives = ref(props.queryParams);

//

watch(
  () => openPrice.value,
  (newValue, oldValue) => {
    console.log("获取到的药品名称open", newValue, props.style.width, props.queryParams);
    openPrice.value = newValue;
    if (newValue) {
      getList();
    }
  }
);

// 选择 医生 对应 库房 对应 药品
import { getUserProfile } from "@/api/system/user.js";
import { getWarehouse } from "@/api/healthcare/warehouse";

/** 查询药品价格信息列表 */
function getList() {
  loading.value = true;
  console.log("查询药品库存信息列表获取id：", props.queryParams, props.medicineName);
  //
  const num = 0;
  // 获取 登录用户 信息
  getUserProfile().then((response) => {
    // 获取 库房 选择
    getWarehouse(response.data.userId).then((resp) => {
      queryParamsDrugArchives.value.storeroomId = resp.data.storeroomId
      // 分类
      if (num == 0) {
        gainDrugInventoryList(queryParamsDrugArchives.value).then((response) => {
          drugArchivesList.value = response.rows;
          // 进行 价格 的 处理 保留 两位小数
                    for (let i = 0; i < drugArchivesList.value.length; i++) {
            drugArchivesList.value[i].filingRetailPrice =
              drugArchivesList.value[i].filingRetailPrice?.toFixed(2) ?? "0.00";

            drugArchivesList.value[i].minNumberRetailPrice =
              drugArchivesList.value[i].minNumberRetailPrice?.toFixed(2) ?? "0.00";
          }
          total.value = response.total;
          loading.value = false;
          console.log("获取药品价格：", response); 
        });
      }
    });
  });

  //
  if (num == 1) {
    gainMedicineArchiveList(queryParamsDrugArchives.value).then((response) => {
      drugArchivesList.value = response.rows;
      // 进行 价格 的 处理 保留 两位小数
      // for (let i = 0; i < drugArchivesList.value.length; i++) {
      //   drugArchivesList.value[i].filingRetailPrice = drugArchivesList.value[
      //     i
      //   ].filingRetailPrice.toFixed(2);
      //   drugArchivesList.value[i].minNumberRetailPrice = drugArchivesList.value[
      //     i
      //   ].minNumberRetailPrice.toFixed(2);
      // }
      for (let i = 0; i < drugArchivesList.value.length; i++) {
        drugArchivesList.value[i].filingRetailPrice =
          drugArchivesList.value[i].filingRetailPrice?.toFixed(2) ?? "0.00";

        drugArchivesList.value[i].minNumberRetailPrice =
          drugArchivesList.value[i].minNumberRetailPrice?.toFixed(2) ?? "0.00";
      }
      total.value = response.total;
      loading.value = false;
      console.log("获取药品价格：", response);
    });
  }
}

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

// 重置按钮操作
function resetQuery() {
  queryParamsDrugArchives.value = ref({
    pageNum: 1,
    pageSize: 10,
    medicineName: null,
  });
  handleQuery();
}

/** 表格选择事件 */
const emit = defineEmits(["select-value", "input-medicine-name", "input-queryParams"]);

/** 选择按钮操作 */
function selectValue(row) {
  if (row != null) {
    console.log("获取患者信息", row);
    closeDialog();
    emit("select-value", row);
  }
}

// 更新 数据
const requestValueUpdate = () => {
  // 父 组件 的 方法
  props.onUpdate();
};

// Dialog 关闭的回调
function openClose() {
  console.log("Dialog 关闭的回调", openPrice.value);
}

// 方法 控制 弹框 的 显示
const openDialog = () => {
  openPrice.value = true;
};

// 方法 控制 弹框 的 隐藏
const closeDialog = () => {
  openPrice.value = false;
};
// 修改 请求 参数
const updateQueryParams = (value) => {
  console.log("修改 请求 参数", value);
  queryParamsDrugArchives.value = value;
};

// 可以 给 queryRef 组件 暴露 方法   父组件 可以 调用 子组件 的 方法
defineExpose({
  openDialog,
  closeDialog,
  updateQueryParams,
});

getList();
</script>
