<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--器材目录-->
      <el-col :span="4" :xs="24">
        <div class="head-title">仪器类型目录</div>
        <div class="head-container">
          <el-tree
              :data="instrumentType"
              :props="{ label: 'info', children: 'children' }"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="treeRef"
              node-key="id"
              highlight-current
              default-expand-all
              @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--器材目录-->
      <el-col :span="20" :xs="24">
        <el-form
            :model="queryParams"
            ref="queryRef"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
          <el-row :gutter="24">
            <!-- <el-col :span="6"> -->
            <el-form-item label="仪器名" prop="searchKey" label-width="55">
              <el-input
                  v-model="queryParams.searchKey"
                  placeholder="名称/编码/"
                  clearable
                  style="width: 220px"
                  @keyup.enter="handleQuery"
              />
            </el-form-item>
            <!-- </el-col> -->
            <!-- <el-col :span="5"> -->
            <el-form-item label="状态" prop="statusEnum" label-width="50">
              <el-select v-model="queryParams.statusEnum" clearable>
                <el-option
                    v-for="status in statusFlagOptions"
                    :key="status.value"
                    :label="status.info"
                    :value="status.value"
                />
              </el-select>
            </el-form-item>
          </el-row>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                icon="Plus"
                @click="openAddInstrument()"
            >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="danger"
                plain
                icon="Remove"
                :disabled="multiple"
                @click="handleClose"
            >停用</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="success"
                plain
                icon="CirclePlus"
                :disabled="multiple"
                @click="handleStart"
            >启用</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                icon="Search"
                @click="getList"
            >查询</el-button
            >
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="Download"
              @click="handleExport"
              >导出Excel</el-button
            >
          </el-col> -->
        </el-row>

        <el-table
            v-loading="loading"
            :data="instrumentList"
            @selection-change="handleSelectionChange"
            width="90%"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
              label="仪器编码"
              align="center"
              key="instrumentCode"
              prop="instrumentCode"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="仪器名称"
              align="center"
              key="instrumentName"
              prop="instrumentName"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="主编码"
              align="center"
              key="instrumentMainCode"
              prop="instrumentMainCode"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="类型"
              align="center"
              key="instrumentTypeEnumText"
              prop="instrumentTypeEnumText"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="型号"
              align="center"
              key="instrumentModel"
              prop="instrumentModel"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="生产厂家"
              align="center"
              key="manufacturer"
              prop="manufacturer"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="序列号"
              align="center"
              key="serialNumber"
              prop="serialNumber"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="采购单位"
              align="center"
              key="purchasingCompany"
              prop="purchasingCompany"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="联系人"
              align="center"
              key="contactPerson"
              prop="contactPerson"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="采购日期"
              align="center"
              key="purchaseDate"
              prop="purchaseDate"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="原价"
              align="center"
              key="originalPrice"
              prop="originalPrice"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="成交价"
              align="center"
              key="transactionPrice"
              prop="transactionPrice"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="安装日期"
              align="center"
              key="installationDate"
              prop="installationDate"
              :show-overflow-tooltip="true"
          >
          <template #default="scope">
            <span>{{ parseTime(scope.row.installationDate )}}</span>
          </template>
          </el-table-column>>
          <el-table-column
              label="安装人"
              align="center"
              key="installationPerson"
              prop="installationPerson"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="维护人"
              align="center"
              key="maintenancePerson"
              prop="maintenancePerson"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="使用科室"
              align="center"
              key="orgId_dictText"
              prop="orgId_dictText"
              :show-overflow-tooltip="true"
          >
          </el-table-column>>
          <el-table-column
              label="鉴定人"
              align="center"
              key="identificationPerson"
              prop="identificationPerson"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="记录温度"
              align="center"
              key="recordedTemperature"
              prop="recordedTemperature"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="附件"
              align="center"
              key="accessories"
              prop="accessories"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="仪器状态"
              align="center"
              key="instrumentStatusEnum"
              prop="instrumentStatusEnum"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="报损日期"
              align="center"
              key="damageReportDate"
              prop="damageReportDate"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="可复检"
              align="center"
              key="recheckableInstrumentEnum"
              prop="recheckableInstrumentEnum"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="使用情况"
              align="center"
              key="usageStatusEnum"
              prop="usageStatusEnum"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="报废原因"
              align="center"
              key="decommissionReason"
              prop="decommissionReason"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="备注"
              align="center"
              key="remarks"
              prop="remarks"
              :show-overflow-tooltip="true"
          />

          <el-table-column
              label="操作"
              align="center"
              width="150"
              class-name="small-padding fixed-width"
              fixed="right"
          >
            <template #default="scope">
              <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="openEditInstrument(scope.row)"
              >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNo"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />
      </el-col>
    </el-row>
    <instrument-dialog
        ref="instrumentRef"
        :title="title"
        :item="currentData"
        :instrument-type-enum="instrumentTypeList"
        :statusFlagOptions="statusFlagOptions"
        :instrument-status-enum-option="instrumentStatusEnumOptions"
        @submit="getList()"
    />
  </div>
</template>

<script setup name="instrument">
import {
  getInstrumentList,
  getInstrumentOne, getInstrumentInit, editInstrumentStatus,
} from "./components/instrument.js";
import instrumentDialog from "./components/instrumentDialog.vue";
import { nextTick } from "vue";
import {parseTime} from "../../../utils/his.js";

const { proxy } = getCurrentInstance();

const instrumentList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]); // 存储选择的行数据
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const instrumentType = ref(undefined);
const instrumentTypeList = ref(undefined);
const statusFlagOptions = ref(undefined);
const instrumentStatusEnumOptions = ref(undefined);

const currentData = ref({});

const viewData = ref({});
const currentCategoryEnum = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    searchKey: undefined, // 品名/商品名/英文品名/编码/拼音
    typeEnum: undefined, // 类型
    statusEnum: undefined, // 状态（

  },
  rules: {},
});

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

/** 通过条件过滤节点  */
const filterNode = (value, data) => {
  if (!value) return true;
  return data.label.indexOf(value) !== -1;
};

/** 样本分类查询下拉树结构 */
function getTreatmentList() {
  getInstrumentInit().then((response) => {
    console.log(response, "response器材目录分类查询下拉树结构");
    instrumentType.value =  JSON.parse(JSON.stringify(response.data.instrumentTypeList)).sort((a, b) => {
      return parseInt(a.value) - parseInt(b.value);
    });
    instrumentType.value.push({ info: "全部", value: "" });
    instrumentTypeList.value = response.data.instrumentTypeList.sort((a, b) => {
      return parseInt(a.value) - parseInt(b.value);
    });
    statusFlagOptions.value = response.data.statusFlagOptions;
    instrumentStatusEnumOptions.value = response.data.instrumentStatusEnumList;

  });
}
/** 查询器材目录列表 */
function getList() {
  loading.value = true;
  getInstrumentList(queryParams.value).then((res) => {
    loading.value = false;
    instrumentList.value = res.data.records;
    total.value = res.data.total;
  });
}
/** 节点单击事件 */
function handleNodeClick(data) {
  queryParams.value.instrumentTypeEnum = data.value;
  currentCategoryEnum.value = data.value;
  handleQuery();
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

/** 启用按钮操作 */
function handleStart() {
  const startIds = ids.value;
  //   selectedData
  proxy.$modal
      .confirm("是否确定启用数据！")
      .then(function () {
        return editInstrumentStatus(startIds,'启用');
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("启用成功");
      })
      .catch(() => {});
}
/** 停用按钮操作 */
function handleClose() {
  const stopIds = ids.value;
  proxy.$modal
      .confirm("是否确认停用数据！")
      .then(function () {
        return  editInstrumentStatus(stopIds,'停用');
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("停用成功");
      })
      .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
      "system/user/export",
      {
        ...queryParams.value,
      },
      `user_${new Date().getTime()}.xlsx`
  );
}

/** 选择条数  */
function handleSelectionChange(selection) {
  console.log(selection, "selection");
  // selectedData.value = selection.map((item) => ({ ...item })); // 存储选择的行数据
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 打开新增弹窗 */
function openAddInstrument() {
  // if (!currentCategoryEnum.value) {
  //   return proxy.$modal.msgError("请选择器材目录分类");
  // }
  title.value = "新增";
  nextTick(() => {
    proxy.$refs.instrumentRef.show();
  });
}
/** 打开编辑弹窗 */
function openEditInstrument(row) {
  currentData.value = {};
  console.log("打开编辑弹窗");
  getInstrumentOne(row.id).then((response) => {
    console.log(response, "currentDataform");
    currentData.value = response.data;
    title.value = "编辑";
    nextTick(() => {
      proxy.$refs["instrumentRef"].edit();
    });
    getList();
  });
}
getTreatmentList();
getList();
</script>
<style scoped>
.el-form--inline .el-form-item {
  display: inline-flex;
  vertical-align: middle;
  margin-right: 10px !important;
}
.el-select{
  width: 150px!important;
}
</style>
