<template>
  <div class="app-container">
    <!-- 顶部筛选 -->
    <div class="top-filter">
      <el-form :inline="true" :model="filterForm" class="demo-form-inline">
        <el-form-item label="医院名称:">
          <el-select v-model="filterForm.hospitalName" placeholder="请选择" size="mini">
            <el-option :label="item.name?item.name:'全部'" :value="item.name" v-for="item in hospitalList" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作时间:">
          <el-date-picker
            v-model="filterForm.date"
            type="daterange"
            size="mini"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="操作人:">
          <el-select v-model="filterForm.name" placeholder="请选择" size="mini">
            <el-option label="全部" value></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快捷查询:">
          <el-input v-model="filterForm.code" placeholder="请输入患者姓名/处方号" size="mini"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="handleSearch">搜索</el-button>
          <el-button size="mini" @click="handleClear">清空</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格展示 -->
    <div class="content">
      <el-table :data="tableData" style="width: 100%" border v-loading="loading">
        <el-table-column label="序号" width="50" align="center">
          <template slot-scope="{scope, $index}">
            <span>{{ $index+1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="hospitalName" label="医疗机构" min-width="180" align="center"></el-table-column>
        <el-table-column prop="preNo" label="处方号" min-width="200" align="center"></el-table-column>
        <el-table-column prop="name" label="患者" min-width="120" align="center"></el-table-column>
        <el-table-column prop="sex" label="性别" min-width="80" align="center"></el-table-column>
        <el-table-column prop="age" label="年龄" min-width="80" align="center"></el-table-column>
        <el-table-column prop="contactNumber" label="电话" min-width="140" align="center"></el-table-column>
        <el-table-column prop="packageDose" label="包装量" min-width="120" align="center"></el-table-column>
        <el-table-column prop="packageNumber" label="包数" min-width="120" align="center"></el-table-column>
        <el-table-column prop="deviceName" label="设备名称" min-width="160" align="center"></el-table-column>
        <el-table-column prop="startTime" label="开始时间" min-width="160" align="center"></el-table-column>
        <el-table-column prop="endTime" label="结束时间" min-width="160" align="center"></el-table-column>
        <el-table-column label="打包照片" min-width="300">
          <template slot-scope="scope">
            <img :src="item" alt="" width="80" height="80" class="img" v-for="(item,index) in scope.row.imagesParse" :key="index" @click="toPreview(item)">
          </template>
        </el-table-column>
        <el-table-column prop="packingNote" label="打包备注" min-width="140" align="center"></el-table-column>
        <el-table-column prop="operatingTime" label="操作时间" min-width="160" align="center"></el-table-column>
        <el-table-column prop="operator" label="操作人" min-width="140" align="center"></el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="filterForm.pageNum"
          background
          :page-size="filterForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="filterForm.total"
        ></el-pagination>
      </div>
    </div>

    <el-dialog title="预览图片" :visible.sync="showImg" width="60%" :before-close="handleClose">
      <img :src="imgUrl" alt="" style="width: 100%;">
    </el-dialog>
  </div>
</template>
<script>
import dayjs from "dayjs";
import { getPackList } from "@/api/decoct/pack";
import { getHospitalList } from "@/api/decoct/recipe";
export default {
  name: "Pack",
  components: {

  },
  data() {
    const now = dayjs().format("YYYY-MM-DD");
    return {
      filterForm: {
        pageNum: 1,
        total: 0,
        pageSize: 10,
        hospitalName: '',
        date: [now, now],
        startTime: now,
        endTime: now,
        name: '',
        code: ''
      },
      tableData: [],
      showImg: false, // 预览图片弹窗
      imgUrl: '',
      loading: false,
      hospitalList: []
    };
  },
  mounted() {
    this.getHospitalList()
    this.getList()
  },
  methods: {
    // 清空
    handleClear() {
      const now = dayjs().format("YYYY-MM-DD");
      this.filterForm = {
        pageNum: 1,
        total: 0,
        pageSize: 10,
        hospitalName: '',
        date: [now, now],
        startTime: now,
        endTime: now,
        name: '',
        code: ''
      }
    },
    // 搜索
    handleSearch() {
      this.getList()
    },
    // 详情按钮点击
    // handleDetail(scope) {

    // },
    // 分页改变
    handleCurrentChange(e) {
      this.filterForm.pageNum = e;
      this.getList()
    },
    handleClose() {
      this.showImg = false
      this.imgUrl = ''
    },
    // 预览图片
    toPreview(item) {
      this.showImg = true
      this.imgUrl = item
    },
    getList() {
      this.loading = true;
      if (
        this.filterForm.date &&
        this.filterForm.date.length
      ) {
        this.filterForm.startTime = this.filterForm.date[0];
        this.filterForm.endTime = this.filterForm.date[1];
      } else {
        this.filterForm.startTime = "";
        this.filterForm.endTime = "";
      }
      getPackList(this.filterForm).then((response) => {
        this.tableData = response.rows.map(item => {
          if(item.images) this.$set(item, 'imagesParse', item.images.split(','))
          return item
        })
        this.filterForm.total = response.total;
        this.loading = false;
      });
    },
    getHospitalList() {
      getHospitalList().then((response) => {
        this.hospitalList = response.data
        this.hospitalList.unshift({
          id: 0,
          name: ''
        })
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.page {
  text-align: center;
  margin-top: 20px;
}
.img{
  margin: 0 10px 10px 0;
}
</style>
