<template>
  <div class="app-container">
    <el-form
      ref="queryRef"
      :model="queryParams"
      :inline="true"
      label-width="100px"
    >
      <el-form-item
        label="药品名称："
        prop="searchKey"
      >
        <el-input
          v-model="queryParams.searchKey"
          placeholder="编码/单据号/药品名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="单据号：" prop="busNo">
      <el-input
        v-model="queryParams.busNo"
        placeholder="单据号："
        clearable
        style="width: 200px;"
        @keyup.enter="handleQuery"
      />
    </el-form-item>
     <el-form-item label="药品名称：" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="项目编号/项目品名"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编码：" prop="medicationDefId">
        <el-input
          v-model="queryParams.medicationDefId"
          placeholder="编码："
          clearable
          style="width: 200px;"
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="审核日期：">
        <el-date-picker
          v-model="approvalTime"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 300px"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <!-- <el-form-item label="科室：" prop="department">
        <el-select
          v-model="queryParams.department"
          placeholder=""
          clearable
          style="width: 150px"
        >
          <el-option
            v-for="chrgitmLv_enumText in chrgitmLv_enumTextOptions"
            :key="chrgitmLv_enumText.value"
            :label="chrgitmLv_enumText.info"
            :value="chrgitmLv_enumText.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item
        label="存放仓库："
        prop="purposeLocationId"
      >
        <el-select
          v-model="queryParams.purposeLocationId"
          placeholder=""
          clearable
          style="width: 150px"
        >
          <el-option
            v-for="supplyStatus in locationIdList"
            :key="supplyStatus.value"
            :label="supplyStatus.name"
            :value="supplyStatus.id"
          />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="项目类型：" prop="categoryType">
        <el-select
          v-model="queryParams.categoryType"
          placeholder=""
          clearable
          style="width: 150px"
        >
          <el-option
            v-for="categoryCode in item_category_code"
            :key="categoryCode.value"
            :label="categoryCode.label"
            :value="categoryCode.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item
        label="供应商："
        prop="supplierId"
      >
        <el-select
          v-model="queryParams.supplierId"
          placeholder=""
          clearable
          style="width: 150px"
        >
          <el-option
            v-for="supplierList in supplierListOptions"
            :key="supplierList.value"
            :label="supplierList.label"
            :value="supplierList.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-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
        >
          导出
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="purchaseinventoryList"
      height="calc(100vh - 300px)"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
      />
      <!-- <el-table-column
        label="药品名称"
        align="center"
        key="name"
        prop="name"
        :show-overflow-tooltip="true"
        width="110"
      /> -->
      <el-table-column
        key="supplyBusno"
        label="单据号"
        align="center"
        prop="supplyBusno"
        width="200"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <el-button
            type="primary"
            link
            @click="showDetail(scope.row.supplyBusno)"
          >
            {{ scope.row.supplyBusno }}
          </el-button>
        </template>
      </el-table-column>
      <!-- itemTable -->
      <el-table-column
        key="name"
        label="药品名称"
        align="center"
        prop="name"
        width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="busNo"
        label="编码"
        align="center"
        prop="busNo"
        width="140"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="药品类型"
        align="center"
        key="itemTableText"
        prop="itemTableText"
        width="140"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        key="lotNumber"
        label="生产批号"
        align="center"
        prop="lotNumber"
        width="140px"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="locationName"
        label="存放仓库"
        align="center"
        prop="locationName"
        width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="locationStoreName"
        label="货位"
        align="center"
        prop="locationStoreName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="unitCode_dictText"
        label="计量单位"
        align="center"
        prop="unitCode_dictText"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="quantity"
        label="采购数量"
        align="center"
        prop="quantity"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="price"
        label="采购单价"
        align="center"
        prop="price"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="totalPrice"
        label="采购金额"
        align="center"
        prop="totalPrice"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="salePrice"
        label="售价"
        align="center"
        prop="salePrice"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="totalSalePrice"
        label="售价金额"
        align="center"
        prop="totalSalePrice"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="采购科室"
        align="center"
        key="department"
        prop="department"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        key="supplier"
        label="供应商"
        align="center"
        prop="supplier"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="approverId_dictText"
        label="审核人"
        align="center"
        prop="approverId_dictText"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="occurrenceTime"
        label="制单日期"
        align="center"
        prop="occurrenceTime"
        width="160"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.occurrenceTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        key="approvalTime"
        label="审核日期"
        align="center"
        prop="approvalTime"
        width="160"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.approvalTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
    <el-dialog
      v-model="dialogVisible"
      title="单据详情"
      width="1700px"
      destroy-on-close
    >
      <el-row>
        <el-col :span="24">
          <el-descriptions :column="4">
            <el-descriptions-item label="单据编号：">
              {{ documentDetails[0].busNo }}
            </el-descriptions-item>
            <el-descriptions-item label="制单日期：">
              {{ documentDetails[0].applyTime }}
            </el-descriptions-item>
            <el-descriptions-item label="供应商：">
              {{ documentDetails[0].supplierName }}
            </el-descriptions-item>
            <el-descriptions-item label="制单人：">
              {{ documentDetails[0].practitionerName }}
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
      </el-row>
      <el-table
        :data="documentDetails"
        border
        max-height="600"
      >
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="60"
        />
        <el-table-column
          label="项目名"
          align="center"
          prop="itemName"
        />
        <el-table-column
          label="规格"
          align="center"
          prop="totalVolume"
        />
        <el-table-column
          label="厂家/产地"
          align="center"
          prop="manufacturerText"
          width="240"
        />
        <el-table-column
          label="仓库"
          align="center"
          prop="purposeLocationName"
        />
        <el-table-column
          label="采购数量"
          align="right"
          prop="totalQuantity"
          header-align="center"
        >
          <template #default="scope">
            <span>{{ scope.row.totalQuantity + scope.row.unitCode_dictText }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="单价"
          align="right"
          prop="price"
          header-align="center"
        >
          <template #default="scope">
            <span>{{ scope.row.price.toFixed(2) + ' 元' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="合计金额"
          align="right"
          prop="totalPrice"
          header-align="center"
        >
          <template #default="scope">
            <span>{{ scope.row.totalPrice.toFixed(2) + ' 元' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="批号"
          align="center"
          prop="lotNumber"
        />
        <el-table-column
          label="生产日期"
          align="center"
          prop="startTime"
          width="130"
        >
          <template #default="scope">
            <span>{{ formatDateStr(scope.row.startTime, 'YYYY-MM-DD') }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="有效期至"
          align="center"
          prop="endTime"
          width="130"
        >
          <template #default="scope">
            <span>{{ formatDateStr(scope.row.endTime, 'YYYY-MM-DD') }}</span>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">
            关 闭
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PurchaseDocumentDetsils">
import {
  getDocumentDetails,
  getInboundInit,
  getPharmacyCabinetList,
  getReportInboundPCage,
} from './statisticalManagent';
import {formatDateStr} from '@/utils/index';

const { proxy } = getCurrentInstance();
const route = useRoute();

const purchaseinventoryList = ref([]);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const dialogVisible = ref(false);
const total = ref(0);
const approvalTime = ref([]);
const supplierListOptions = ref([]);
const locationIdList = ref([]);
const documentDetails = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    searchKey: undefined,
    busNo: undefined,
    name: undefined,
    medicationDefId: undefined,
    // approvalTime:undefined,
    department: undefined,
    purposeLocationId: undefined,
    categoryType: undefined,
    supplierId: undefined,
    occurrenceTimeSTime: undefined,
    occurrenceTimeETime: undefined,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

function getPharmacyCabinetLists() {
  getPharmacyCabinetList().then((response) => {
    locationIdList.value = response.data;
  });
  getInboundInit().then((response) => {
    supplierListOptions.value = response.data.supplierListOptions;
  });
}
/** 导出按钮操作 */
function handleExport() {
  proxy.$download.downloadGet(
    'report-manage/inbound/excel-out',
    {
      ...queryParams.value,
    },
    `dict_${new Date().getTime()}.xlsx`
  );
}
/** 查询调拨管理项目列表 */
function getList() {
  loading.value = true;
  getReportInboundPCage(queryParams.value).then((res) => {
    loading.value = false;
    purchaseinventoryList.value = res.data.records;
    total.value = res.data.total;
  });
}

// 查看单据详情
function showDetail(busNo) {
  getDocumentDetails({ busNo: busNo }).then((res) => {
    dialogVisible.value = true;
    documentDetails.value = res.data;
  });
}

// 监听路由变化
watch(
  () => route.query,
  (newQuery) => {
    // 如果路由中包含指定参数，则使用这些参数进行查询
    if (
      newQuery.purposeLocationId ||
      newQuery.occurrenceTimeSTime ||
      newQuery.occurrenceTimeETime
    ) {
      // 设置查询参数
      if (newQuery.purposeLocationId) {
        queryParams.value.purposeLocationId = newQuery.purposeLocationId;
      }
      if (newQuery.occurrenceTimeSTime) {
        queryParams.value.occurrenceTimeSTime = newQuery.occurrenceTimeSTime + ' 00:00:00';
      }
      if (newQuery.occurrenceTimeETime) {
        queryParams.value.occurrenceTimeETime = newQuery.occurrenceTimeETime + ' 23:59:59';
      }

      // 执行查询
      getList();
    }
  },
  { immediate: true }
);

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

/** 清空条件按钮操作 */
function handleClear() {
  // 清空查询条件
  queryParams.value.occurrenceTimeSTime = '';
  queryParams.value.occurrenceTimeETime = '';
  approvalTime.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>
