<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="院区" prop="wardId">
        <el-select v-model="queryParams.wardId" filterable clearable style="width: 240px">
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for="item in wardIdList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="科室" prop="deptId">
        <TreeDept
          v-model="queryParams.deptId"
          clearable
          style="width: 240px"
          :options="deptList"
          :autoFetch="false"
          :props="{ emitPath: true }"
        />
      </el-form-item>
      <el-form-item label="随访状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="角色状态"
          clearable
          style="width: 240px"
        >
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for="dict in FOLLOWUP_STATUS.options"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="医生名称" prop="doctorId">
        <el-select
          v-model="queryParams.doctorId"
          placeholder="请输入医生名称"
          clearable
          filterable
          style="width: 240px"
        >
          <el-option
            v-for="item in doctorList"
            :key="item.id"
            :value="item.id"
            :label="item.doctorName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="随访时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        ></el-date-picker>
      </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>
    <div class="overview">
      <!-- <div class="overview-item">
        <div class="title-label">
          <span>诊后患者总计</span>
          <el-tooltip content="已添加至随访系统的患者总数" placement="top">
            <i class="el-icon-question" style="color: #909399"></i>
          </el-tooltip>
        </div>
        <div class="num">{{ viewData.patientCount ? viewData.patientCount : 0 }}</div>
      </div>
      <div class="overview-item">
        <div class="title-label">
          <span>随访覆盖患者总计</span>
          <el-tooltip content="已配置随访计划患者总数" placement="top">
            <i class="el-icon-question" style="color: #909399"></i>
          </el-tooltip>
        </div>
        <div class="num">
          {{ viewData.recoveredPatientCount ? viewData.recoveredPatientCount : 0 }}
        </div>
      </div>
      <div class="overview-item">
        <div class="title-label">
          <span>随访覆盖率</span>
          <el-tooltip content="已配置随访计划患者总数/已添加至随访系统的患者总数" placement="top">
            <i class="el-icon-question" style="color: #909399"></i>
          </el-tooltip>
        </div>
        <div class="num">
          {{ viewData.recoveredPercent ? viewData.recoveredPercent + "%" : 0 }}
        </div>
      </div>
      <div class="overview-item">
        <div class="title-label">
          <span>随访患者完成率</span>
          <el-tooltip content="完成随访计划的患者数/已配置随访计划的患者数" placement="top">
            <i class="el-icon-question" style="color: #909399"></i>
          </el-tooltip>
        </div>
        <div class="num">
          {{ viewData.patientCompletedPercent ? viewData.patientCompletedPercent + "%" : 0 }}
        </div>
      </div> -->
      <div class="overview-item">
        <div class="title-label">
          <span>随访计划完成率</span>
          <el-tooltip content="已完成随访计划数/随访计划总数总数" placement="top">
            <i class="el-icon-question" style="color: #909399"></i>
          </el-tooltip>
        </div>
        <div class="num">
          {{ viewData.taskCompletedPercent ? viewData.taskCompletedPercent + "%" : 0 }}
        </div>
      </div>
      <!-- <div class="overview-item">
        <div class="title-label">
          <span>诊后线上回流率</span>
          <el-tooltip content="通过随访小程序跳转至互联网医院小程序的患者数/已添加至随访系统的患者总数" placement="top">
            <i class="el-icon-question" style="color: #909399"></i>
          </el-tooltip>
        </div>
        <div class="num">
          --
        </div>
      </div> -->
    </div>

    <div style="display: flex; justify-content: space-between; padding-bottom: 8px">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      <ApiButton type="primary" plain icon="el-icon-download" size="mini" :api="exportRequest">
        导出Excel表
      </ApiButton>
    </div>

    <el-table v-loading="loading" :data="pageList" border row-key="id">
      <el-table-column
        align="center"
        label="序号"
        type="index"
        :index="(index) => (queryParams.pageNum - 1) * queryParams.pageSize + index + 1"
        width="120"
      />
      <el-table-column
        align="center"
        label="随访单编号"
        prop="id"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        align="center"
        label="患者名称"
        prop="patientName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        align="center"
        label="患者性别"
        prop="patientSex"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        align="center"
        label="联系电话"
        prop="patientPhone"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        align="center"
        label="院区名称"
        prop="wardName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        align="center"
        label="病种"
        prop="labelNames"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        align="center"
        label="科室"
        prop="deptName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column align="center" label="随访状态" width="100">
        <template slot-scope="scope">
          <my-tag :type="FOLLOWUP_STATUS.getTagByValue(scope.row.status)">
            {{
              FOLLOWUP_STATUS.getLabelByValue(scope.row.status)
            }}
          </my-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="最近任务时间" prop="nextTaskDate" width="180">
        <template slot-scope="scope">
          <span>
            {{ scope.row.nextTaskDate ? parseTime(scope.row.nextTaskDate, "{y}-{m}-{d}") : "" }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="任务状态" width="100">
        <template slot-scope="scope">
          <my-tag :type="FOLLOWUP_STATUS.getTagByValue(scope.row.nextTaskStatus)">
            {{ FOLLOWUP_STATUS.getLabelByValue(scope.row.nextTaskStatus) }}
          </my-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="责任医生"
        prop="doctorName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        align="center"
        label="随访周期"
        prop="periodNumber"
        :show-overflow-tooltip="true"
        width="150"
      >
        <template slot-scope="scope">
          {{ scope.row.periodNumber + PERIOD_UNIT.getLabelByValue(scope.row.periodUnit) }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleView(scope.row)">查看</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 { getDoctorAllList } from "@/api/hospital/index";
import {
  statisticalViewData,
  statisticalPageList,
  statisticalExport,
} from "@/api/common";
import { getAllHospital, getTreeHospDeptByPatient } from "@/api/admin";
import ApiButton from "@/components/ApiButton";
import TreeDept from "@/components/TreeDept";
import { FOLLOWUP_STATUS, PERIOD_UNIT } from '@/enums/index'

export default {
  name: "FollowUpStatistical",
  components: {
    ApiButton,
    TreeDept,
  },
  data() {
    return {
      FOLLOWUP_STATUS,
      PERIOD_UNIT,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        doctorId: undefined,
        status: "",
        wardId: "", // 院区ID
        deptId: [],
      },
      pageList: [],
      viewData: {},
      wardIdList: [], // 院区列表
      deptList: [], // 科室数据
      doctorList: [],
    };
  },
  created() {
    this.loadData();
  },
  activated() {
    this.loadData();
  },
  watch: {
    "queryParams.wardId"(val) {
      const wardId = val;
      if (wardId) {
        this.queryParams.deptId = [];
        this.getDeptList(wardId);
        this.getList(wardId);
        this.getViewData();
      } else {
        this.queryParams.deptId = [];
        this.pageList = [];
        this.getDeptList();
        this.getList();
        this.getViewData();
      }
    },
    "queryParams.deptId"(val) {
      const deptId = val[1] || val[0];
      if (deptId) {
        this.getList(deptId);
        this.getViewData();
      } else {
        this.pageList = [];
        this.getList();
        this.getViewData();
      }
    },
  },
  methods: {
    async getDoctorOptions() {
      const { data } = await getDoctorAllList();
      console.log("data=", data);
      this.doctorList = data
    },
    loadData() {
      this.loadWardIdList();
      this.getDoctorOptions();
      this.getDeptList();
      this.getViewData();
      this.getList();
    },
    /*获取院区列表*/
    async loadWardIdList() {
      const res = await getAllHospital();
      this.wardIdList = res.data;
    },
    /*获取科室数据*/
    async getDeptList(wardId) {
      const res = await getTreeHospDeptByPatient({ wardId: wardId });
      this.deptList = res.data || [];
    },
    exportRequest() {
      return statisticalExport(this.addDateRange(this.queryParams, this.dateRange)).then((res) => {
        this.$download.saveAs(res, "随访统计.xlsx");
      });
    },
    // 面板展示数据
    async getViewData() {
      const params = JSON.parse(JSON.stringify(this.queryParams));
      Reflect.deleteProperty(params, "pageNum");
      Reflect.deleteProperty(params, "pageSize");
      params.deptId =
        params.deptId?.length === 2
          ? params.deptId.slice(-1).toString()
          : (params.deptId?.length === 1 && params.deptId.slice(-1).toString()) || "";
      const res = await statisticalViewData(this.addDateRange(params, this.dateRange));
      this.viewData = res.data;
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      const params = JSON.parse(JSON.stringify(this.queryParams));
      params.deptId =
        params.deptId?.length === 2
          ? params.deptId.slice(-1).toString()
          : (params.deptId?.length === 1 && params.deptId.slice(-1).toString()) || "";
      statisticalPageList(this.addDateRange(params, this.dateRange))
        .then((res) => {
          const {
            data: { list, page },
          } = res;
          this.pageList = list || [];
          this.total = page?.total || 0;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getViewData();
      this.getDeptList(this.queryParams.wardId);
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 查看详情
    handleView({ id }) {
      this.$router.push({ name: "FollowUpDetail", params: { id } });
    },
  },
};
</script>
<style lang="scss" scoped>
.overview {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
  padding-bottom: 10px;
  .overview-item {
    display: flex;
    flex-direction: column;
    width: calc(16.6% - 10px);
    min-width: 250px;
    height: 130px;
    margin-right: 10px;
    margin-bottom: 10px;
    padding: 0 20px;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    background-color: #fff;
    .title-label {
      font-size: 16px;
      font-weight: 600;
      line-height: 40px;
    }
    .num {
      padding-top: 10px;
      font-size: 30px;
      font-weight: 600;
      text-align: center;
      color: #1890ff;
    }
  }
}
</style>
