<template>
  <div class="app-container" ref="layout">
    <div ref="topform" class="form">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px"
        class="form">
        <el-form-item label="执行时间" prop="startDate" label-width="110px">
          <el-date-picker v-model="intentionalTime" @change="pickerChangeFn" type="daterange" range-separator="至"
            start-placeholder="开始日期" style="width: 300px" value-format="yyyy-MM-dd" clearable end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="医院" prop="hospitalAgencyId">
          <el-select v-model="queryParams.hospitalAgencyId" filterable placeholder="请选择医院" style="width: 200px"
            clearable>
            <!-- @clear="clearhospitalAgency" -->
            <el-option v-for="item in hospitalAgencylist" :key="item.id" :label="item.agencyName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="就诊方式" prop="visitMethod">
          <el-select v-model="queryParams.visitMethod" placeholder="请选择就诊方式" clearable>
            <el-option v-for="dict in dict.type.visit_method" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="就诊流水号" prop="visitSerialNumber">
          <el-input v-model="queryParams.visitSerialNumber" placeholder="请输入就诊流水号" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="患者姓名" prop="patientName">
          <el-input v-model="queryParams.patientName" placeholder="请输入患者姓名" clearable style="width: 200px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="任务名称" prop="manageRouteName">
          <el-input v-model="queryParams.manageRouteName" placeholder="请输入任务名称" clearable style="width: 200px"
            @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>
      </el-form>
    </div>
    <div ref="table">
      <el-table v-loading="loading" :data="taskExecuteRecordList" @selection-change="handleSelectionChange"
        :max-height="maxTableHeight" style="width: 100%">
        <el-table-column type="selection" align="center" />
        <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
        <!-- <el-table-column label="患者表id" align="center" prop="patientId" /> -->
        <el-table-column label="患者姓名" align="center" prop="patientName" width="80" />
        <el-table-column label="电话" align="center" prop="patientPhone" width="120" />
        <el-table-column label="就诊流水号" align="center" prop="visitSerialNumber" width="120"
          :show-overflow-tooltip="true" />
        <el-table-column label="医院" align="center" prop="hospitalAgencyName" width="105"
          :show-overflow-tooltip="true" />
        <el-table-column label="院区" align="center" prop="campusAgencyName" :show-overflow-tooltip="true" />
        <el-table-column label="科室名称" align="center" prop="departmentName" :show-overflow-tooltip="true" />
        <el-table-column label="病区名称" align="center" prop="wardName" />
        <!-- <el-table-column label="门诊/住院号" align="center" prop="patientName" />
        <el-table-column label="手术名称" align="center" prop="patientName" /> -->
        <el-table-column label="执行人姓名" align="center" prop="executePerson" />
        <el-table-column label="管理路径节点名称" align="center" prop="manageRouteNodeName">
          <template slot-scope="scope">
            <span>
              {{ scope.row.manageRouteName == "AFTER_DISCHARGE" ? "出院后" : "" }}
              {{ scope.row.manageRouteName == "AFTER_ADMISSION" ? "入院后" : "" }}
              {{ scope.row.manageRouteName == "AFTER_CONSULTATION" ? "就诊后" : "" }}
              {{ scope.row.manageRouteName == "AFTER_VISIT_DISCHARGE" ? "就诊/出院后" : "" }}
              {{ scope.row.manageRouteName == "PREOPERATIVE" ? "术前" : "" }}
              {{ scope.row.manageRouteName == "POSTOPERATIVE" ? "术后" : "" }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="管理任务名称" align="center" prop="manageRouteName" width="110">
        </el-table-column>
        <el-table-column label="入院时间" align="center" prop="admissionTime" width="100">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.admissionTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="出院时间" align="center" prop="dischargeTime" width="100">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.dischargeTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="就诊方式" align="center" prop="visitMethod">
          <template slot-scope="scope">
            <span>{{ scope.row.visitMethod == "OUTPATIENT_SERVICE" ? "门诊" : "" }}
              {{ scope.row.visitMethod == "BE_IN_HOSPITAL" ? "住院" : "" }}
            </span>
          </template></el-table-column>
        <el-table-column label="任务名称" align="center" prop="manageRouteName" />
        <el-table-column label="执行时间" align="center" prop="executeTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.executeTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="任务内容" align="center" prop="taskContent" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>
              {{ scope.row.taskContent == "PHONE_OUTBOUND" ? "电话外呼" : "" }}
              {{ scope.row.taskContent == "QUESTIONNAIRE_SCALE" ? "问卷量表" : "" }}
              {{ scope.row.taskContent == "PROPAGANDA_ARTICLE" ? "宣教文章" : "" }}
              {{ scope.row.taskContent == "TEXT_REMIND" ? "文字提醒" : "" }}
              {{ scope.row.taskContent == "ARTIFICIAL_FOLLOW_UP" ? "人工随访" : "" }}
            </span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="执行人姓名，手动执行时记录" align="center" prop="executePerson" /> -->
        <!-- <el-table-column label="任务执行方式，人工执行：MANUAL_EXECUTE，系统自动执行：SYSTEM_AUTOMATIC_EXECUTE" align="center" prop="executeType" /> -->
        <!-- <el-table-column
        label="任务推送方式"
        align="center"
        prop="executeRemark"
      /> -->
        <el-table-column label="操作" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-notebook-2" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:taskExecuteRecord:edit']">详情</el-button>
            <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:taskExecuteRecord:remove']"
          >删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" /> -->
    <myPagination v-show="total > 0" :total="total" :pageSize="queryParams.pageSize"
      :indexFromWrap="queryParams.pageNum" @updateCPage="updateCPage"></myPagination>
  </div>
</template>

<script>
import {
  listTaskExecuteRecord,
  delTaskExecuteRecord,
  addTaskExecuteRecord,
  updateTaskExecuteRecord,
} from "@/api/system/taskExecuteRecord";
import { getAge } from "@/utils/age";
import { selectAgencyList } from "@/api/manage/selectAgencyList";
export default {
  name: "TaskExecuteRecord",
  dicts: ["visit_method"],
  data() {
    return {
      maxTableHeight: undefined,
      intentionalTime: [],
      //医院list
      hospitalAgencylist: [],
      // 院区list
      // campusAgencylist: [],
      // //科室list
      // departmentlist: [],
      // //病区list
      // wardlist: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者管理任务执行记录表格数据
      taskExecuteRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        visitSerialNumber: null,
        patientId: null,
        visitMethod: null,
        manageRouteId: null,
        manageRouteNodeId: null,
        patientName: null,
        startDate: null,
        endDate: null,
        manageRouteName: null,
        manageRouteNodeName: null,
        taskContent: null,
        executeTime: null,
        executePerson: null,
        executeType: null,
        executeRemark: null,
        hospitalAgencyId: null,
        campusAgencyId: null,
        departmentId: null,
        wardId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      // rules: {
      //   patientId: [
      //     { required: true, message: "患者表id不能为空", trigger: "blur" },
      //   ],
      // },
    };
  },
  mounted() {
    this.getList();
    this.selectAgencyinfo();
    this.getMaxTableHeight()
    this.screenChange()
  },
  methods: {
    updateCPage(index, size) {
      this.queryParams.pageNum = index
      this.queryParams.pageSize = size
      this.getList();
    },
    // 获取表格最高高度
    getMaxTableHeight() {
      const windowInnerHeight = window.innerHeight // 屏幕可视高度
      const layoutDiv = this.$refs.layout
      const formDiv = this.$refs.topform
      this.maxTableHeight =
        windowInnerHeight - 134 -
        this.getBoxPadding(layoutDiv) -
        this.getBoxHeight(formDiv)
    },
    // 屏幕resize监听
    screenChange() {
      // 屏幕resize监听事件：一旦屏幕宽高发生变化，就会执行resize
      window.addEventListener('resize', this.getMaxTableHeight, true)
      // 将屏幕监听事件移除
      // 这步是必须的。离开页面时不移除，再返回，或者进入到别的有相同元素的页面会报错
      // 或者将这里的方法直接写在beforeDestroy函数中也可以
      this.$once('hook:beforeDestroy', () => {
        window.removeEventListener('resize', this.getMaxTableHeight, true)
      })
    },
    pickerChangeFn() {
      if (this.intentionalTime == null) {
        this.intentionalTime = [];
        this.queryParams.startDate = null;
        this.queryParams.endDate = null;
      } else {
        this.queryParams.startDate = this.intentionalTime[0];
        this.queryParams.endDate = this.intentionalTime[1];
      }
    },
    // },
    //获取医院list
    selectAgencyinfo() {
      let query = {
        agencyStatus: "ON",
        nodeType: "HOSPITAL",
      };
      selectAgencyList(query).then((res) => {
        this.hospitalAgencylist = res.data;
      });
    },
    /** 查询患者管理任务执行记录列表 */
    getList() {
      this.loading = true;
      listTaskExecuteRecord(this.queryParams).then((response) => {
        this.taskExecuteRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        patientId: null,
        manageRouteId: null,
        manageRouteNodeId: null,
        patientName: null,
        manageRouteName: null,
        manageRouteNodeName: null,
        taskContent: null,
        executeTime: null,
        executePerson: null,
        executeType: null,
        executeRemark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.intentionalTime = [];
      this.queryParams.startDate = null;
      this.queryParams.endDate = null;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加患者管理任务执行记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row);
      this.$router.push({
        path: "/task/record",
        query: {
          path: "/task/taskExecuteRecord",
          patientId: row.id,
          manageRouteNodeId: row.manageRouteNodeId,
          taskContent: row.taskContent,
          taskNodeType: row.taskNodeType,
          templateId: row.templateId,
          cardNo: row.cardNo,
          patientName: row.patientName,
          patientPhone: row.patientPhone,
          sex: row.sex,
          routeHandleRemark: row.routeHandleRemark,
          nodeContent: row.nodeContent,
          birthDate: row.birthDate,
          familyMemberPhone: row.familyMemberPhone,
          address: row.address,
          patientSource: row.patientSource,
          createTime: row.createTime,
          age: row.birthDate ? getAge(row.birthDate) : "",
        },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTaskExecuteRecord(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTaskExecuteRecord(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // handleClick(tab, event) {
    //   this.intentionalTime = [];
    //   this.queryParams.startDate = null;
    //   this.queryParams.endDate = null;
    //   // this.signTime = []
    //   // this.queryParams.signTimeStart = null
    //   // this.queryParams.signTimeEnd = null
    //   this.getList();
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(
          '是否确认删除患者管理任务执行记录编号为"' + ids + '"的数据项？'
        )
        .then(function () {
          return delTaskExecuteRecord(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/taskExecuteRecord/export",
        {
          ...this.queryParams,
        },
        `taskExecuteRecord_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
/* // ::v-deep .el-scrollbar__bar {
//   height: 15px !important;
// }

// ::v-deep .el-scrollbar {
//   height: 15px !important;
//   bottom: 8vh !important;
// } */
</style>
