<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="处⽅ID" prop="rpId">
        <el-input
          v-model="queryParams.rpId"
          placeholder="请输入处⽅单ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
         <el-select   v-model="queryParams.status" placeholder="请选择状态" clearable size="small" >
         <el-option
                v-for="item in prescribeStatusOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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-table  height="500" border 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="orderCode" />
      <el-table-column label="会员昵称" align="center" prop="userNickname" />
      <el-table-column label="会员手机号" align="center" prop="userPhone" />
      <el-table-column label="患者姓名" align="center" prop="patientName" />
      <el-table-column label="患者年龄" align="center" prop="patientAge" />
      <!-- <el-table-column label="患者电话" align="center" prop="patientTel" /> -->
      <el-table-column label="患者性别" align="center" prop="patientGender" />
      <el-table-column label="处⽅单ID" align="center" prop="rpId" />
      <el-table-column label="病情描述(主诉）" align="center" prop="chiefComplaint" />
      <el-table-column label="现病史" align="center" prop="nowIllness" />
      <el-table-column label="既往史" align="center" prop="historyIllness" />


      <!-- <el-table-column label="体重" align="center" prop="weight" /> -->
      <el-table-column label="是否有过敏史" align="center" prop="isHistoryAllergic" />
      <!-- <el-table-column label="过敏史" align="center" prop="historyAllergic" /> -->
      <el-table-column label="肝功能是否异常" align="center" prop="liverUnusual" />
      <el-table-column label="肾功能是否异常" align="center" prop="renalUnusual" />
      <el-table-column label="是否是备孕/怀孕/哺乳期" align="center" prop="lactationFlag" />
      <el-table-column label="复诊凭证" align="center" prop="recordPic" width="120">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.recordPic!=null"
            style="width: 50px; height: 50px"
            :src="parsePhoto(scope.row.recordPic)[0]"
            :preview-src-list="parsePhoto(scope.row.recordPic)">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="处方图片" align="center" width="120">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover"
          >
            <img slot="reference" :src="scope.row.rpUrl" width="100">
            <img :src="scope.row.rpUrl" style="max-width: 500px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="处⽅开具时间" align="center" prop="rpCreateTime" width="180">
      </el-table-column>
      <el-table-column label="诊断" align="center" prop="diagnose" />
      <el-table-column label="审核意见" align="center" prop="auditReason" />
      <el-table-column label="医生名称" align="center" prop="doctorName" />
      <el-table-column label="科室名称" align="center" prop="doctorId" />
      <el-table-column label="提交时间" align="center" prop="createTime" width="180">
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
          <template slot-scope="scope">
              <el-tag prop="status" v-for="(item, index) in prescribeStatusOptions"    v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleShowOrder(scope.row)"
            v-hasPermi="['store:prescribe:showOrder']"
          >查看订单</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="show.title" v-if="show.open" :visible.sync="show.open" width="1000px" append-to-body>
        <product-order  ref="order" />
    </el-dialog>
  </div>
</template>

<script>
import { listPrescribe, getPrescribe, delPrescribe, addPrescribe, updatePrescribe, exportPrescribe } from "@/api/hisStore/prescribe";

import productOrder from "../components/productOrder";
export default {
  components: { productOrder },
  name: "HisPrescribe",
  data() {
    return {
      show:{
        open:false,
        title:"订单详情"
      },
      prescribeStatusOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 处方表格数据
      prescribeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prescribeType: null,
        orderId: null,
        userId: null,
        patientId: null,
        rpId: null,
        pharmacyCode: null,
        pharmacyName: null,
        chiefComplaint: null,
        nowIllness: null,
        historyIllness: null,
        patientAge: null,
        patientName: null,
        weight: null,
        isHistoryAllergic: null,
        historyAllergic: null,
        liverUnusual: null,
        renalUnusual: null,
        lactationFlag: null,
        patientTel: null,
        patientGender: null,
        recordPic: null,
        rpUrl: null,
        rpCreateTime: null,
        diagnose: null,
        drugs: null,
        doctorName: null,
        doctorId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getDicts("prescribe_status").then((response) => {
      this.prescribeStatusOptions = response.data;
    });
    this.getList();
  },
  methods: {
    handleShowOrder(row){
      this.show.open=true;
      const orderId = row.orderId ;
      setTimeout(() => {
        this.$refs.order.getOrder(orderId);
      }, 500);
    },
    /** 查询处方列表 */
    getList() {
      this.loading = true;
      listPrescribe(this.queryParams).then(response => {
        this.prescribeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        prescribeId: null,
        prescribeType: null,
        orderId: null,
        userId: null,
        patientId: null,
        rpId: null,
        pharmacyCode: null,
        pharmacyName: null,
        chiefComplaint: null,
        nowIllness: null,
        historyIllness: null,
        patientAge: null,
        patientName: null,
        weight: null,
        isHistoryAllergic: null,
        historyAllergic: null,
        liverUnusual: null,
        renalUnusual: null,
        lactationFlag: null,
        patientTel: null,
        patientGender: null,
        recordPic: null,
        rpUrl: null,
        rpCreateTime: null,
        diagnose: null,
        drugs: null,
        doctorName: null,
        doctorId: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    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 = "添加处方";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const prescribeId = row.prescribeId || this.ids
      getPrescribe(prescribeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改处方";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.prescribeId != null) {
            updatePrescribe(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addPrescribe(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const prescribeIds = row.prescribeId || this.ids;
      this.$confirm('是否确认删除处方编号为"' + prescribeIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPrescribe(prescribeIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有处方数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPrescribe(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>

