<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="sourceLocationId"
        label-width="80px"
      >
        <el-select
          v-model="queryParams.sourceLocationId"
          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="purposeLocationId"
        label-width="100px"
      >
        <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="制单日期：">
        <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>

    <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
          v-if="viewStatus == 'view'"
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
        >
          导出
        </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="supplyBusNo"
        label="单据号"
        align="center"
        prop="supplyBusNo"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="name"
        label="药品"
        align="center"
        prop="name"
        width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="statusEnum_enumText"
        label="是否调拨"
        align="center"
        prop="statusEnum_enumText"
        width="90"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="lotNumber"
        label="生产批号"
        align="center"
        prop="lotNumber"
        width="120px"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="sourceLocationName"
        label="源仓库"
        align="center"
        prop="sourceLocationName"
        width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="sourceLocationStoreName"
        label="源货位"
        align="center"
        prop="sourceLocationStoreName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="purposeLocationName"
        label="目的仓库"
        align="center"
        prop="purposeLocationName"
        width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="purposeLocationStoreName"
        label="目的货位"
        align="center"
        prop="purposeLocationStoreName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="itemQuantity"
        label="调拨数量"
        align="center"
        prop="itemQuantity"
        width="80"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="unitCode_dictText"
        label="计量单位"
        align="center"
        prop="unitCode_dictText"
        width="80"
        :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="applicantId_dictText"
        label="制单人"
        align="center"
        prop="applicantId_dictText"
        :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="applyTime"
        label="申请日期"
        align="center"
        prop="applyTime"
        width="160"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</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"
    />
  </div>
</template>

<script setup name="transferManagentDetails">
import {getPharmacyCabinetList, getReportTransferPage,} from "./statisticalManagent";

const { proxy } = getCurrentInstance();
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 locationIdList = ref([]);
const route = useRoute();

const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    searchKey: undefined, 
    sourceLocationId:undefined,
    purposeLocationId:undefined,
    occurrenceTimeSTime:undefined,
    occurrenceTimeETime:undefined,
  },
  rules: {},
});

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

// 监听路由变化
watch(
  () => route.query,
  (newQuery) => {
    // 如果路由中包含指定参数，则使用这些参数进行查询
    if (newQuery.purposeLocationId || newQuery.occurrenceTimeSTime || newQuery.occurrenceTimeETime) {
      // 设置查询参数
      if (newQuery.purposeLocationId) {
        queryParams.value.purposeLocationId = newQuery.purposeLocationId;
      }
      if (newQuery.sourceLocationId) {
        queryParams.value.sourceLocationId = newQuery.sourceLocationId;
      }
      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 getPharmacyCabinetLists() {
  getPharmacyCabinetList().then((response) => {
    locationIdList.value = response.data
  });
}
/** 查询调拨管理项目列表 */
function getList() {
  loading.value = true;
  getReportTransferPage(queryParams.value).then((res) => {
    loading.value = false;
    purchaseinventoryList.value = res.data.records
    
    if(purchaseinventoryList.value&& purchaseinventoryList.value.length>0){
      purchaseinventoryList.value.map((k,index)=>{
        k.statusEnum_enumText = k.statusEnum_enumText=='同意'?'已调拨':''
      })
    }
    total.value = res.data.total;
  });
}

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

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

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

/** 导出按钮操作 */
function handleExport() {
  proxy.$download.downloadGet("report-manage/transfer/excel-out", {
    ...queryParams.value
  }, `dict_${new Date().getTime()}.xlsx`);
}

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

</style>