<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="处方单编号" prop="prescribeCode">
        <el-input v-model="queryParams.prescribeCode" placeholder="请输入处方单编号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="患者姓名" prop="patientName">
        <el-input v-model="queryParams.patientName" placeholder="请输入患者姓名" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="处方类型" prop="prescribeType">
        <el-select v-model="queryParams.prescribeType" placeholder="请选择" clearable size="small">
          <el-option v-for="dict in prescribeTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="处方状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable size="small">
          <el-option v-for="dict in prescribeStatusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </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="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['store:prescribe:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table border v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="处方单编号" align="center" prop="prescribeCode" width="180" show-overflow-tooltip />
      <el-table-column label="药品订单编号" align="center" prop="orderCode" width="180" show-overflow-tooltip />
      <el-table-column label="处方类型" align="center" prop="prescribeType" width="90">
        <template slot-scope="scope"><dict-tag :options="prescribeTypeOptions" :value="scope.row.prescribeType" /></template>
      </el-table-column>
      <el-table-column label="患者姓名" align="center" prop="patientName" width="100" />
      <el-table-column label="患者年龄" align="center" prop="patientAge" width="80" />
      <el-table-column label="患者电话" align="center" prop="patientTel" width="120" />
      <el-table-column label="医生名称" align="center" prop="doctorName" width="100" />
      <el-table-column label="诊断" align="center" prop="diagnose" min-width="140" show-overflow-tooltip />
      <el-table-column label="处方状态" align="center" prop="status" width="90">
        <template slot-scope="scope"><dict-tag :options="prescribeStatusOptions" :value="scope.row.status" /></template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="160" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)" v-hasPermi="['store:prescribe:query']">查看</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" />
  </div>
</template>

<script>
import { getRequest } from "@/api/common";

export default {
  name: "Prescribe",
  data() {
    return {
      loading: true,
      exportLoading: false,
      ids: [],
      showSearch: true,
      total: 0,
      list: [],
      prescribeTypeOptions: [],
      prescribeStatusOptions: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prescribeCode: undefined,
        patientName: undefined,
        prescribeType: undefined,
        status: undefined
      }
    };
  },
  created() {
    this.getDicts("prescribe_type").then(res => { this.prescribeTypeOptions = res.data; });
    this.getDicts("prescribe_status").then(res => { this.prescribeStatusOptions = res.data; });
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getRequest("/store/prescribe/list", this.queryParams).then(res => {
        this.list = res.rows || [];
        this.total = res.total || 0;
        this.loading = false;
      }).catch(() => {
        this.list = [];
        this.total = 0;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.prescribeId);
    },
    handleDetail(row) {
      this.$router.push({ path: "/prescribeDetail", query: { prescribeId: row.prescribeId } });
    },
    handleExport() {
      this.$modal.confirm("确认导出处方数据吗？").then(() => {
        this.exportLoading = true;
        getRequest("/store/prescribe/export", this.queryParams).then(res => {
          this.download(res.msg);
          this.exportLoading = false;
        });
      }).catch(() => {});
    }
  }
};
</script>
