<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <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_type"
            :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>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:taskExecuteRecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:taskExecuteRecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:taskExecuteRecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:taskExecuteRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="taskExecuteRecordList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" 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" />
      <el-table-column label="电话" align="center" prop="patientPhone" />
      <el-table-column
        label="就诊流水号"
        align="center"
        prop="visitSerialNumber"
      />
      <el-table-column label="医院" align="center" prop="hospitalAgencyName" />
      <el-table-column label="院区" align="center" prop="campusAgencyName" />
      <el-table-column label="科室名称" align="center" prop="departmentName" />
      <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"
      >
      </el-table-column>
      <el-table-column
        label="入院时间"
        align="center"
        prop="admissionTime"
        width="180"
      >
        <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="180"
      >
        <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_HOSPITALIZED" ? "住院" : "" }}
          </span>
        </template></el-table-column
      >
      <el-table-column label="任务名称" align="center" prop="manageRouteName" />
      <el-table-column
        label="执行时间"
        align="center"
        prop="executeTime"
        width="180"
      >
        <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">
        <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"
        class-name="small-padding fixed-width"
        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>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </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_type"],
  data() {
    return {
      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" },
      //   ],
      // },
    };
  },
  created() {
    this.getList();
    this.selectAgencyinfo();
  },
  methods: {
    // pickerChangeFn(value){ // 日期选择器改变事件 ~ 解决日期选择器清空 值不清空的问题
    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: {
          patientId: row.id,
          taskContent: row.taskContent,
          taskType: row.taskType,
          templateId: row.templateId,
          cardNo: row.cardNo,
          patientName: row.patientName,
          patientPhone: row.patientPhone,
          sex: row.sex,
          routeHandleRemark: row.routeHandleRemark,
          textRemindContent: row.textRemindContent,
          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>
