<script>
import {packTwiceList} from '@/api/pda';
import dayjs from 'dayjs';
import ElImageViewer from 'element-ui/packages/image/src/image-viewer.vue';
import {listMedicalMechanism} from '@/api/medical/mechanism';

export default {
  name: 'pda-pack-twice',
  components: {
    ElImageViewer
  },
  data() {
    return {
      queryParams: {
        yfId: window.localStorage.getItem('pharmacyId'),
        pageNum: 1,
        pageSize: 10,
        date: [],
        yljgId: '',
        name: '',
        code: '',
      },
      // 遮罩层
      showSearch: true,
      // 显示搜索条件
      loading: true,
      dataset: [],
      total: 0,

      previewImages: [],
      listMedicalMechanismArr: [],
    };
  },
  created() {
    this.getListMedicalMechanism();
    this.resetQuery();
  },
  methods: {
    getListMedicalMechanism() {
      return listMedicalMechanism().then((res) => {
        this.listMedicalMechanismArr = res.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.queryParams.date = [
        dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
        dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
      ];
      this.handleQuery();
    },
    getList() {
      this.loading = true;
      const model = Object.assign({
        hospitalName: this.queryParams.yljgId ? this.listMedicalMechanismArr.find(item => item.id === this.queryParams.yljgId ).name : '',
      }, this.queryParams);
      packTwiceList(model).then((response) => {
        this.dataset = response.rows;
        this.total = response.total;
        this.loading = false;
      }, () => {
        this.loading = false;
      });
    },
    preview(urls) {
      if (typeof urls === 'string') {
        this.previewImages = urls.split(',').filter(Boolean);
      } else if (Array.isArray(urls)) {
        this.previewImages = urls;
      }
    },
    close() {
      this.previewImages = [];
    },
  },
};
</script>

<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <div class="query-box">
        <div class="query-box__left">
          <el-form-item label="医疗机构" prop="yljgId">
            <el-select
              style="width:160px"
              v-model="queryParams.yljgId"
              placeholder="请选择医疗机构"
              clearable
            >
              <el-option
                v-for="item in listMedicalMechanismArr"
                :key="item.id"
                :value="item.id"
                :label="item.name"
                :disabled="item.state == 1 ? false : true"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="操作时间" prop="date">
            <el-date-picker style="width:340px" v-model="queryParams.date"
                            type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss"
                            start-placeholder="开始时间" end-placeholder="结束时间"
                            size="small" clearable
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="操作人" prop="name">
            <el-input
              style="width:160px"
              v-model="queryParams.name"
              placeholder="请输入操作人"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="快捷查询" prop="code">
            <el-input
              style="width:160px"
              v-model="queryParams.code"
              placeholder="请输入患者姓名/处方号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </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>
        </div>
      </div>
    </el-form>

    <el-table v-loading="loading" :data="dataset" size="mini" border>
      <el-table-column type="index" width="55" align="center" label="序号"/>

      <el-table-column label="医疗机构" align="center" prop="hospitalName"/>
      <el-table-column label="门诊/住院" align="center" prop="preMzZy">
        <template slot-scope="scope">{{ scope.row.preMzZy == 1 ? '门诊' : '住院' }}</template>
      </el-table-column>
      <el-table-column label="处方号" align="center" prop="preNo"/>
      <el-table-column label="患者姓名" align="center" prop="name"/>
      <el-table-column label="性别" align="center" prop="sex"/>
      <el-table-column label="年龄" align="center" prop="age"/>
      <el-table-column label="处方用法" align="center" prop="prescriptionusage"/>
      <el-table-column label="是否代煎" align="center" prop="isBehalf">
        <template slot-scope="scope">{{ scope.row.isBehalf == 1 ? '是' : '否' }}</template>
      </el-table-column>
      <el-table-column label="配送方式" align="center" prop="expressExecutor"/>
      <el-table-column label="剂数" align="center" prop="number"/>
      <el-table-column label="包数" align="center" prop="packageNumber"/>
      <el-table-column label="包装量(ml)" align="center" prop="packageDose" min-width="110px"/>
      <el-table-column label="复核包数" align="center" prop="packageNumberReview"/>
      <el-table-column label="复核包装量(ml)" align="center" prop="packageDoseReview" min-width="110px"/>
      <el-table-column label="打包复核照片" align="center" prop="images" min-width="110px">
        <template slot-scope="scope">
          <el-button v-if="scope.row.images" type="text" @click="preview(scope.row.images)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="打包复核备注" align="center" prop="reviewNote" min-width="110px"/>
      <el-table-column label="操作时间" align="center" prop="operatingTime"/>
      <el-table-column label="操作人" align="center" prop="operator"/>
    </el-table>

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

    <el-image-viewer v-if="previewImages.length" :url-list="previewImages" :on-close="close"></el-image-viewer>
  </div>
</template>

<style scoped lang="scss">

</style>
