<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="deptId">
        <TreeDept
          v-model="queryParams.deptId"
          clearable
          style="width: 240px"
          :options="deptList"
          :autoFetch="false"
          :props="{ emitPath: false, checkStrictly: false }"
          @change="onChangeDept"
        />
      </el-form-item>
      <el-form-item label="医生名称" prop="doctorName">
        <el-select
          v-model="queryParams.doctorName"
          placeholder="请输入医生名称"
          clearable
          filterable
          style="width: 240px"
        >
          <el-option
            v-for="item in doctorList"
            :key="item.doctorId"
            :value="item.userName"
            :label="item.userName"
          ></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 ? viewData + '%' : viewData }}</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"
        :min-width="120"
      />
      <el-table-column
        align="center"
        label="随访单编号"
        prop="task.id"
        :show-overflow-tooltip="true"
        :min-width="150"
      />
      <el-table-column
        align="center"
        label="患者名称"
        prop="patient.name"
        :show-overflow-tooltip="true"
        :min-width="150"
      />
      <el-table-column
        align="center"
        label="患者性别"
        prop="patient.sex"
        :show-overflow-tooltip="true"
        :min-width="150"
      />
      <el-table-column
        align="center"
        label="联系电话"
        prop="patient.phoneNumber"
        :show-overflow-tooltip="true"
        :min-width="150"
      />
      <el-table-column
        align="center"
        label="科室"
        prop="followDocument.deptName"
        :show-overflow-tooltip="true"
        :min-width="150"
      />
      <el-table-column
        align="center"
        label="责任医生"
        prop="followDocument.doctorName"
        :show-overflow-tooltip="true"
        :min-width="150"
      />
      <el-table-column
        align="center"
        label="任务次数"
        prop="task.batchNo"
        :show-overflow-tooltip="true"
        :min-width="150"
      >
        <template #default="{row}">
          {{ row.task.batchNo ? `第${row.task.batchNo}次任务` : '--' }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="任务开始时间"
        prop="task.taskBeginTime"
        :show-overflow-tooltip="true"
        :min-width="150"
      />
      <el-table-column
        align="center"
        label="任务结束时间"
        prop="task.taskEndTime"
        :show-overflow-tooltip="true"
        :min-width="150"
      />
      <el-table-column align="center" label="任务状态" :min-width="100">
        <template slot-scope="scope">
          <span
            :style="{
              color: ![undefined, null].includes(scope.row.task.status)
                ? followItemStatusEnum.enum[scope.row.task.status].color
                : '',
            }"
          >
            {{
              scope.row.task.status !== undefined
                ? followItemStatusEnum.getLabel(scope.row.task.status)
                : ""
            }}
          </span>
        </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 { followUpTaskViewData, followUpTaskStaticalListApi }from "@/api/common/statisticalManage/followupTaskStatistical";
import { getAllHospital, getTreeHospDeptByPatient } from "@/api/admin";
import ApiButton from "@/components/ApiButton";
import TreeDept from "@/components/TreeDept";

export default {
  name: "FollowUpStatistical",
  enums: ["followItemStatusEnum", "periodUnitEnum"],
  components: {
    ApiButton,
    TreeDept,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        doctorId: undefined,
        status: "",
        wardId: "", // 院区ID
        deptId: "",
        doctorName: "",
      },
      pageList: [],
      viewData: null,
      deptList: [], // 科室数据
      doctorList: [],
    };
  },
  created() {
    this.loadData();
  },
  activated() {
    this.loadData();
  },
  methods: {
    async getDoctorOptions() {
      const { data } = await getDoctorAllList({
        deptId: this.queryParams.deptId
      });
      console.log("data=", data);
      this.doctorList = data.map((item) => {
        const { doctorId, userInfo = {} } = item;
        return {
          doctorId,
          ...userInfo,
        };
      });
    },
    loadData() {
      this.getDoctorOptions();
      this.getDeptList();
      this.getViewData();
      this.getList();
    },
    onChangeDept(val) {
      console.log('val=', val, this.queryParams)
      this.queryParams.doctorId = undefined
      this.queryParams.doctorName = undefined
      this.getDoctorOptions()
    },
    /*获取科室数据*/
    async getDeptList(wardId) {
      const res = await getTreeHospDeptByPatient({ wardId: wardId });
      this.deptList = res.data || [];
    },
    // 面板展示数据
    async getViewData() {
      const params = JSON.parse(JSON.stringify(this.queryParams));
      Reflect.deleteProperty(params, "pageNum");
      Reflect.deleteProperty(params, "pageSize");
      const res = await followUpTaskViewData(this.addDateRange(params, this.dateRange));
      this.viewData = res.data;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      const params = JSON.parse(JSON.stringify(this.queryParams));
      console.log('params=', params)
      params.beginTime = this.dateRange?.[0]
      params.endTime = this.dateRange?.[1]
          followUpTaskStaticalListApi(params)
        .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();
    }
  },
};
</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(25% - 10px);
    min-width: 300px;
    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>
