<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="处方类型" prop="prescribeType">
        <el-select v-model="queryParams.prescribeType" placeholder="请选择处方类型" clearable>
          <el-option label="西药" :value="1"></el-option>
          <el-option label="中药" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="处方单号" prop="prescribeCode">
        <el-input v-model="queryParams.prescribeCode" placeholder="请输入处方单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="订单编号" prop="orderCode">
        <el-input v-model="queryParams.orderCode" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="患者姓名" prop="patientName">
        <el-input v-model="queryParams.patientName" placeholder="请输入患者姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--      <el-form-item label="患者电话" prop="patientTel">-->
      <!--        <el-input v-model="queryParams.patientTel" placeholder="请输入患者电话" clearable @keyup.enter.native="handleQuery" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="处方状态" prop="status">-->
      <!--        <el-select v-model="queryParams.status" placeholder="请选择处方状态" clearable>-->
      <!--          <el-option label="未开" :value="0"></el-option>-->
      <!--          <el-option label="已开" :value="1"></el-option>-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!-- <el-form-item label="医生姓名" prop="doctorName">
        <el-input v-model="queryParams.doctorName" placeholder="请输入医生姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="创建时间" prop="dateRange">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          @change="handleDateRangeChange"
        ></el-date-picker>
      </el-form-item> -->
      <el-form-item label="审核时间" prop="auditRange">
        <el-date-picker v-model="auditRange" type="daterange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="yyyy-MM-dd" @change="handleAuditDateRangeChange"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['his:prescribe:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['his:prescribe:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['his:prescribe:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['his:prescribe:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-download" size="mini" @click="handleMessageFeedbackExport"
          v-hasPermi="['his:prescribe:messageFeedbackExport']">导出医疗信息反馈单</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prescribeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="处方ID" align="center" prop="prescribeId" />
      <el-table-column label="处方单号" align="center" prop="prescribeCode" />
      <el-table-column label="处方类型" align="center" prop="prescribeType">
        <template slot-scope="scope">
          <dict-tag :options="prescribeTypeOptions" :value="scope.row.prescribeType" />
        </template>
      </el-table-column>
      <el-table-column label="患者姓名" align="center" prop="patientName" />
      <el-table-column label="医生姓名" align="center" prop="doctorName" />
      <el-table-column label="审核医师" align="center" prop="doctorDrugName" />
      <el-table-column label="拒方原因" :show-overflow-tooltip="true" align="center" prop="auditReason" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">开方</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body @close="handleDialogClose">
      <el-tabs v-model="activeTab" type="border-card">
        <!-- Tab 1: 基本信息 -->
        <el-tab-pane label="基本信息" name="basic">
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="处方类型" prop="prescribeType">
              <el-radio-group v-model="form.prescribeType">
                <el-radio :label="1">西药</el-radio>
                <el-radio :label="2">中药</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="订单ID" prop="inquiryOrderId">
              <el-input v-model="form.inquiryOrderId" placeholder="请输入订单ID" disabled />
            </el-form-item>
            <el-form-item label="店铺订单ID" prop="storeOrderId">
              <el-input v-model="form.storeOrderId" placeholder="请输入店铺订单ID" disabled />
            </el-form-item>
            <el-form-item label="患者姓名" prop="patientName">
              <el-input v-model="form.patientName" placeholder="请输入患者姓名" />
            </el-form-item>
            <el-form-item label="患者年龄" prop="patientAge">
              <el-input v-model="form.patientAge" placeholder="请输入患者年龄" />
            </el-form-item>
            <el-form-item label="患者性别" prop="patientGender">
              <el-select v-model="form.patientGender" placeholder="请选择患者性别">
                <el-option label="男" value="1"></el-option>
                <el-option label="女" value="2"></el-option>
              </el-select>
            </el-form-item>
            <!--            <el-form-item label="患者电话" prop="patientTel">-->
            <!--              <el-input v-model="form.patientTel" placeholder="请输入患者电话" disabled/>-->
            <!--            </el-form-item>-->
            <el-form-item label="体重" prop="weight">
              <el-input v-model="form.weight" placeholder="请输入体重" />
            </el-form-item>
            <el-form-item label="是否有过敏史" prop="isHistoryAllergic">
              <el-radio-group v-model="form.isHistoryAllergic">
                <el-radio :label="'是'">是</el-radio>
                <el-radio :label="'否'">否</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="过敏史" prop="historyAllergic">
              <el-input v-model="form.historyAllergic" placeholder="请输入过敏史" />
            </el-form-item>
            <el-form-item label="诊断" prop="diagnose">
              <el-input v-model="form.diagnose" placeholder="请输入诊断" />
            </el-form-item>
            <el-form-item label="医嘱" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <!-- Tab 2: 处方药品信息 -->
        <el-tab-pane label="处方药品信息" name="drug">
          <div class="drug-container">
            <!-- 药品操作按钮 -->
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddDrug"
                  :disabled="!form.prescribeId">新增药品</el-button>
              </el-col>
            </el-row>
            <!-- 药品列表表格 -->
            <el-table v-loading="drugLoading" :data="drugList" border max-height="400">
              <el-table-column label="药品名称" align="center" prop="drugName" />
              <el-table-column label="规格" align="center" prop="drugSpec" width="100" />
              <el-table-column label="使用方法" align="center" prop="usageMethod" width="100" />
              <el-table-column label="频次" align="center" prop="usageFrequencyUnit" width="80" />
              <el-table-column label="每次用药" align="center" width="120">
                <template slot-scope="scope">
                  {{ scope.row.usagePerUseCount }}{{ scope.row.usagePerUseUnit }}
                </template>
              </el-table-column>
              <el-table-column label="天数" align="center" prop="usageDays" width="60" />
              <el-table-column label="单价" align="center" prop="drugPrice" width="80" />
              <el-table-column label="数量" align="center" width="100">
                <template slot-scope="scope">
                  {{ scope.row.drugNum }}{{ scope.row.drugUnit }}
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
                <template slot-scope="scope">
                  <el-button size="mini" type="text" icon="el-icon-edit"
                    @click="handleUpdateDrug(scope.row)">编辑</el-button>
                  <el-button size="mini" type="text" icon="el-icon-delete"
                    @click="handleDeleteDrug(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 药品分页 -->
            <pagination v-show="drugTotal > 0" :total="drugTotal" :page.sync="drugQueryParams.pageNum"
              :limit.sync="drugQueryParams.pageSize" @pagination="getDrugList" />
          </div>
        </el-tab-pane>
        <!-- 定位在右下角的按钮 -->
        <el-button type="primary" size="mini" style="position: absolute; bottom: 5px; right: 20px;"
          @click="openCollectionInfo">
          查看采集信息
        </el-button>
      </el-tabs>

      <!-- 查看采集信息的弹窗 -->
      <el-dialog :visible.sync="collectionDialogVisible" title="用户信息采集详情" width="800px" append-to-body>

        <collectionDetail :userId="form.userId" :prescribeId="form.prescribeId" v-if="collectionDialogVisible" />
        <span slot="footer" class="dialog-footer">
          <el-button @click="collectionDialogVisible = false">关 闭</el-button>
        </span>
      </el-dialog>


      <span slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button type="primary" @click="confirmPrescribe">确认处方</el-button>
      </span>
    </el-dialog>
    <!-- 药品新增/编辑对话框 -->
    <el-dialog :title="drugTitle" :visible.sync="drugOpen" width="700px" append-to-body>
      <el-form ref="drugForm" :model="drugForm" :rules="drugRules" label-width="120px">
        <el-form-item label="药品名称" prop="drugName">
          <el-input v-model="drugForm.drugName" placeholder="请输入药品名称" />
        </el-form-item>
        <el-form-item label="规格" prop="drugSpec">
          <el-input v-model="drugForm.drugSpec" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="使用方法" prop="usageMethod">
          <el-input v-model="drugForm.usageMethod" placeholder="请输入使用方法" />
        </el-form-item>
        <el-form-item label="药品频次" prop="usageFrequencyUnit">
          <el-input v-model="drugForm.usageFrequencyUnit" placeholder="请输入药品频次,如:每日3次" />
        </el-form-item>
        <el-form-item label="每次用药数量" prop="usagePerUseCount">
          <el-input v-model="drugForm.usagePerUseCount" placeholder="请输入每次用药数量" />
        </el-form-item>
        <!-- <el-form-item label="每次用药单位" prop="usagePerUseUnit">
          <el-input v-model="drugForm.usagePerUseUnit" placeholder="请输入每次用药单位,如:片、粒、ml" />
        </el-form-item>
        <el-form-item label="天数" prop="usageDays">
          <el-input v-model="drugForm.usageDays" placeholder="请输入天数" />
        </el-form-item>
        <el-form-item label="药品单价" prop="drugPrice">
          <el-input v-model="drugForm.drugPrice" placeholder="请输入药品单价" type="number" />
        </el-form-item>
        <el-form-item label="药品数量" prop="drugNum">
          <el-input v-model="drugForm.drugNum" placeholder="请输入药品数量" type="number" />
        </el-form-item>
        <el-form-item label="药品数量单位" prop="drugUnit">
          <el-input v-model="drugForm.drugUnit" placeholder="请输入药品数量单位,如:盒、瓶" />
        </el-form-item>
        <el-form-item label="药品说明书" prop="instructions">
          <el-input v-model="drugForm.instructions" type="textarea" :rows="3" placeholder="请输入药品说明书" />
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="drugOpen = false">取 消</el-button>
        <el-button type="primary" @click="submitDrugForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  listPrescribe,
  getPrescribe,
  delPrescribe,
  addPrescribe,
  updatePrescribe,
  exportPrescribe,
  exportMessageFeedback,
  confirmPrescribe
} from '@/api/his/prescribe'
import { listPrescribeDrug, addPrescribeDrug, updatePrescribeDrug, deletePrescribeDrug } from "@/api/his/prescribeDrug";
import collectionDetail from "@/views/components/collection/collectionDetail.vue";
export default {
  name: "Prescribe",
  components: { collectionDetail },
  data() {
    return {
      collectionDialogVisible: false, // 控制采集信息弹窗显示
      // 原有数据保持不变
      loading: false,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      prescribeList: [],
      title: "",
      open: false,
      dateRange: [],
      auditRange: [],
      doctorConfirmOptions: [
        { dictValue: '0', dictLabel: "未确认" },
        { dictValue: '1', dictLabel: "已确认" }
      ],
      prescribeTypeOptions: [
        { dictValue: '1', dictLabel: "西药" },
        { dictValue: '2', dictLabel: "中药" }
      ],
      statusOptions: [
        { dictValue: '0', dictLabel: "未开" },
        { dictValue: '1', dictLabel: "已开" }
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prescribeType: undefined,
        prescribeCode: undefined,
        orderCode: undefined,
        patientName: undefined,
        patientTel: undefined,
        status: undefined,
        doctorName: undefined,
        sTime: undefined,
        eTime: undefined,
        auditETime: undefined,
        auditSTime: undefined,
        status: 2,//拒绝
      },
      currentConfirm: null,
      form: {},
      rules: {
        prescribeType: [{ required: true, message: "处方类型不能为空", trigger: "change" }],
        patientName: [{ required: true, message: "患者姓名不能为空", trigger: "blur" }],
        patientAge: [{ required: true, message: "患者年龄不能为空", trigger: "blur" }],
        patientGender: [{ required: true, message: "患者性别不能为空", trigger: "change" }],
        diagnose: [{ required: true, message: "诊断不能为空", trigger: "blur" }],
        doctorId: [{ required: true, message: "医生ID不能为空", trigger: "blur" }],
        status: [{ required: true, message: "处方状态不能为空", trigger: "change" }]
      },
      // 新增:Tab相关
      activeTab: 'basic',
      // 新增:药品相关数据
      drugLoading: false,
      drugList: [],
      drugTotal: 0,
      drugOpen: false,
      drugTitle: "",
      drugForm: {},
      drugQueryParams: {
        pageNum: 1,
        pageSize: 10,
        prescribeId: undefined
      },
      drugRules: {
        drugName: [{ required: true, message: "药品名称不能为空", trigger: "blur" }],
        drugSpec: [{ required: true, message: "规格不能为空", trigger: "blur" }],
        usageMethod: [{ required: true, message: "使用方法不能为空", trigger: "blur" }],
        usageFrequencyUnit: [{ required: true, message: "药品频次不能为空", trigger: "blur" }],
        usagePerUseCount: [{ required: true, message: "每次用药数量不能为空", trigger: "blur" }],
        usagePerUseUnit: [{ required: true, message: "每次用药单位不能为空", trigger: "blur" }],
        usageDays: [{ required: true, message: "天数不能为空", trigger: "blur" }],
        drugPrice: [{ required: true, message: "药品单价不能为空", trigger: "blur" }],
        drugNum: [{ required: true, message: "药品数量不能为空", trigger: "blur" }],
        drugUnit: [{ required: true, message: "药品数量单位不能为空", trigger: "blur" }],
        instructions: [{ required: true, message: "药品说明书不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  watch: {
    // 监听Tab切换,当切换到药品Tab时加载药品列表
    activeTab(newVal) {
      if (newVal === 'drug' && this.form.prescribeId) {
        this.getDrugList();
      }
    }
  },
  methods: {
    /** 查询处方列表 */
    getList() {
      this.loading = true;
      let userId = this.$store.state.user.userId;
      let query = this.addDateRange(this.queryParams, this.dateRange, 'Create');
      query.doctorId = userId.toString().replaceAll("D-", "").toString();
      listPrescribe(query).then(response => {
        this.prescribeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询药品列表 */
    getDrugList() {
      if (!this.form.prescribeId) {
        this.$message.warning("请先保存处方基本信息");
        return;
      }
      this.drugLoading = true;
      this.drugQueryParams.prescribeId = this.form.prescribeId;
      listPrescribeDrug(this.drugQueryParams).then(response => {
        this.drugList = response.rows;
        this.drugTotal = response.total;
        this.drugLoading = false;
      });
    },
    // 日期范围处理
    addDateRange(params, dateRange, type) {
      let obj = { ...params };
      if (dateRange != null && dateRange.length > 0) {
        if (type === 'Create') {
          obj.sTime = dateRange[0];
          obj.eTime = dateRange[1];
        } else if (type === 'Audit') {
          obj.auditSTime = dateRange[0];
          obj.auditETime = dateRange[1];
        }
      }
      return obj;
    },
    handleDateRangeChange(val) {
      this.queryParams.sTime = val ? val[0] : undefined;
      this.queryParams.eTime = val ? val[1] : undefined;
    },
    handleAuditDateRangeChange(val) {
      this.queryParams.auditSTime = val ? val[0] : undefined;
      this.queryParams.auditETime = val ? val[1] : undefined;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.auditRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 选择条目触发事件 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.prescribeId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增处方";
      this.activeTab = 'basic';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const prescribeId = row.prescribeId || this.ids[0];
      this.currentConfirm = row.doctorConfirm;

      getPrescribe(prescribeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "开方";
        this.activeTab = 'basic';
        // 如果有prescribeId,则可以查询药品列表
        if (this.form.prescribeId) {
          this.drugQueryParams.prescribeId = this.form.prescribeId;
        }
      })
    },
    confirmPrescribe() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          confirmPrescribe(this.form).then(response => {
            if (response.code === 200) {
              this.submitForm();
              this.$message.success("确认成功！");
              this.open = false;
            } else {
              this.$message.error(response.message);
            }
          }).finally(() => {
            this.getList();
          })
        }

      })


    },
    /** 提交按钮操作 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.prescribeId != undefined) {
            updatePrescribe(this.form).then(response => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPrescribe(this.form).then(response => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal.confirm('是否确认导出所有处方数据项?').then(() => {
        this.exportLoading = true;
        return exportPrescribe(this.queryParams);
      }).then(response => {
        this.$download.excel(response, '处方数据');
        this.exportLoading = false;
      }).catch(() => { });
    },
    /** 导出医疗信息反馈单 */
    handleMessageFeedbackExport() {
      if (this.ids.length === 0) {
        this.$modal.msgError("请至少选择一条记录");
        return;
      }
      this.$modal.confirm('是否确认导出所选处方的医疗信息反馈单?').then(() => {
        this.exportLoading = true;
        return exportMessageFeedback(this.ids);
      }).then(response => {
        this.$download.excel(response, '医疗信息反馈单');
        this.exportLoading = false;
      }).catch(() => { });
    },
    /** 对话框关闭事件 */
    handleDialogClose() {
      // 重置药品相关数据
      this.drugList = [];
      this.currentConfirm = null;
      this.drugTotal = 0;
      this.drugQueryParams.pageNum = 1;
      this.activeTab = 'basic';
    },
    // ==================== 药品相关方法 ====================
    /** 新增药品按钮操作 */
    handleAddDrug() {
      if (!this.form.prescribeId) {
        this.$modal.msgWarning("请先保存处方基本信息后再添加药品");
        return;
      }
      this.resetDrugForm();
      this.drugOpen = true;
      this.drugTitle = "新增药品";
    },
    /** 修改药品按钮操作 */
    handleUpdateDrug(row) {
      this.resetDrugForm();
      this.drugForm = { ...row };
      this.drugOpen = true;
      this.drugTitle = "修改药品";
    },
    /** 提交药品表单 */
    submitDrugForm() {
      this.$refs["drugForm"].validate(valid => {
        if (valid) {
          this.drugForm.prescribeId = this.form.prescribeId;
          if (this.drugForm.drugId != undefined) {
            // 修改
            updatePrescribeDrug(this.drugForm).then(response => {
              this.$message.success("修改成功");
              this.drugOpen = false;
              // 确保刷新完成
              this.getDrugList();
            }).catch(error => {
              // 发生错误时也要确保能正常操作
              console.error('修改药品失败:', error);
            });
          } else {
            // 新增
            addPrescribeDrug(this.drugForm).then(response => {
              this.$message.success("新增成功");
              this.drugOpen = false;
              // 新增成功后回到第一页并刷新
              this.drugQueryParams.pageNum = 1;
              this.getDrugList();
            }).catch(error => {
              console.error('新增药品失败:', error);
            });
          }
        }
      });
    },
    /** 删除药品按钮操作 */
    handleDeleteDrug(row) {
      this.$confirm('是否确认删除药品"' + row.drugName + '"?').then(() => {
        return deletePrescribeDrug(row.drugId);
      }).then(() => {
        this.$message.success("删除成功");

        // 处理删除后的分页逻辑
        // 如果当前页只有一条数据,且不是第一页,则页码减1
        if (this.drugList.length === 1 && this.drugQueryParams.pageNum > 1) {
          this.drugQueryParams.pageNum--;
        }

        // 刷新药品列表
        this.getDrugList();
      }).catch(error => {
        if (error !== 'cancel') {
          console.error('删除药品失败:', error);
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        prescribeId: undefined,
        prescribeCode: undefined,
        prescribeType: 1,
        inquiryOrderId: undefined,
        storeOrderId: undefined,
        orderCode: undefined,
        patientId: undefined,
        patientName: undefined,
        patientAge: undefined,
        patientGender: "1",
        patientTel: undefined,
        weight: undefined,
        isHistoryAllergic: "否",
        historyAllergic: undefined,
        diagnose: undefined,
        doctorId: undefined,
        doctorName: undefined,
        status: 0,
        remark: undefined
      };
      this.resetForm("form");
    },
    // 药品表单重置
    resetDrugForm() {
      this.drugForm = {
        drugId: undefined,
        prescribeId: undefined,
        drugName: undefined,
        drugSpec: undefined,
        usageMethod: undefined,
        usageFrequencyUnit: undefined,
        usagePerUseCount: undefined,
        usagePerUseUnit: undefined,
        usageDays: undefined,
        drugPrice: undefined,
        drugNum: undefined,
        drugUnit: undefined,
        instructions: undefined
      };
      this.resetForm("drugForm");
    },
    /** 患者信息采集详情页面 */
    openCollectionInfo() {
      if (!this.form.userId) {
        this.$message.warning("患者ID未填写！");
        return;
      }
      this.collectionDialogVisible = true;
    },
  },
};
</script>

<style scoped>
.drug-container {
  padding: 10px;
}

.detail-container {
  padding: 20px;
}

.detail-item {
  margin-bottom: 10px;
  display: flex;
}

.detail-item .label {
  min-width: 100px;
  font-weight: bold;
}

.id-card-info {
  display: flex;
  flex-direction: column;
}

.id-card-image {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.id-card-detail {
  margin-top: 20px;
}
</style>
