<template>
  <div class="app-container">
    <el-row>
      <el-col :span="4" v-if="user.roleName == '科室主任'">
        <dtreeDoctor v-model="queryParams.userId" @handleQuery="handleQuery"></dtreeDoctor>
      </el-col>
      <el-col :span="4" v-if="user.roleName == '超级管理员' || user.roleName == '管理员'">
        <dtree v-model="queryParams.deptId" @handleQuery="handleQuery"></dtree>
      </el-col>
      <el-col :span="user.roleName != '医生' ? 20 : 24">
        <el-form :model="queryParams" ref="queryForm" :inline="true">
          <el-form-item prop="physician" v-if="user.roleName == '超级管理员' || user.roleName == '管理员'">
            <el-input v-model="queryParams.physician" placeholder="请输入主治医生" clearable @keyup.enter.native="handleQuery" class="inp" />
          </el-form-item>
          <el-form-item prop="name" label="患者姓名">
            <el-input v-model="queryParams.name" placeholder="请输入患者姓名" clearable @keyup.enter.native="handleQuery" class="inp" />
          </el-form-item>
          <el-form-item label="出院日期">
            <el-date-picker v-model="dateRange" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" value-format="yyyy-MM-dd" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
          <el-button type="primary" icon="el-icon-download" @click="handleExport" style="float: right;">导出</el-button>
        </el-form>
        <el-table :data="response.rows" border height="calc(100vh - 240px)" v-loading="loading">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="患者姓名" align="center" prop="name" width="110" />
          <el-table-column label="性别" align="center" prop="state" width="70">
            <template slot-scope="scope">{{ scope.row.sex == 'F' ? '女' : '男' }}</template>
          </el-table-column>
          <el-table-column label="病历号" align="center" prop="blh" width="110" />
          <el-table-column label="入院时间" align="center" prop="admissionTime" width="160" />
          <el-table-column label="出院时间" align="center" prop="dischargeTime" width="160" />
          <el-table-column label="关注小程序" align="center" prop="state" width="100">
            <template slot-scope="scope">
              <el-tag type="danger" v-if="scope.row.isBindWx == 0">未关注</el-tag>
              <el-tag type="success" v-if="scope.row.isBindWx >= 1">已关注</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="出院随访(达成率)" align="center" prop="state" width="140">
            <template slot-scope="scope">
              <div class="pro">
                <el-progress class="pp" :text-inside="true" :stroke-width="22" :percentage="scope.row.record > 0 ? parseInt(((scope.row.recordRead / scope.row.record) * 100).toFixed(0)) : 0"></el-progress>
                <div class="num">{{ scope.row.record }}次</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="复诊提醒(达成率)" align="center" prop="state" width="140">
            <template slot-scope="scope">
              <div class="pro">
                <el-progress class="pp" :text-inside="true" :stroke-width="22" :percentage="scope.row.up > 0 ? parseInt(((scope.row.upRead / scope.row.up) * 100).toFixed(0)) : 0"></el-progress>
                <div class="num">{{ scope.row.up }}次</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="管床医生" align="center" prop="physician" width="110" />
          <el-table-column label="所属科室" align="center" prop="department" width="160" />
          <el-table-column label="操作" align="center" width="250" fixed="right">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-search" @click="op('detail', scope.row)">患者信息</el-button>
              <el-button size="mini" type="text" icon="el-icon-document" @click="op('listByUser', scope.row, 0)">复诊提醒</el-button>
              <el-button size="mini" type="text" icon="el-icon-document" @click="op('listByUser', scope.row, 1)">出院随访</el-button>
            </template>
          </el-table-column>
          <template slot="empty">
            <el-empty></el-empty>
          </template>
        </el-table>
        <pagination v-if="response.total > 0" :total="response.total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import detail from '@/views/work/visit/detail';
import listByUser from '@/views/work/follow/record/listByUser';
export default {
  name: 'Visit',
  data() {
    return {
      user: this.$store.state.user,
      dateRange: [],
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        physician: null,
        name: null,
        department: null,
        deptId: null,
        userId: null
      }
    };
  },
  created() {
    if (this.user.roleName == '科室主任') {
      this.queryParams.userId = this.user.id;
    }
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      if (this.dateRange) {
        this.queryParams.dateBegin = this.dateRange[0];
        this.queryParams.dateEnd = this.dateRange[1];
      }
      this.get({ url: '/work/visit/patientCard', data: this.queryParams, loading: 'false' }).then((response) => {
        this.response = response;
        this.loading = false;
      });
    },
    op(tag, row, type) {
      if (tag === 'detail') {
        this.iframe({ obj: detail, param: { id: row.id, patId: row.patId }, title: '查看患者信息', width: '75%', height: '93%' });
      }
      if (tag === 'listByUser') {
        this.iframe({ obj: listByUser, param: { patientId: row.patId, patientName: row.name, type: type }, title: row.name + '的' + (type == 0 ? '复诊提醒记录' : '出院随访记录'), width: '80%', height: '80%' });
      }
    },
    handleQuery(e) {
      if (e != undefined && e.deptId) {
        this.queryParams.userId = null;
        this.queryParams.deptId = e.deptId;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm('queryForm');
      this.dateRange = [];
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/work/visit/export', { ...this.queryParams }, '患者信息.xlsx');
    }
  }
};
</script>
