<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="随访队列" prop="sourceId">
        <el-select
          v-model="queryParams.param.sourceId"
          placeholder="请选择"
          clearable
        >
          <el-option label="筛查" :value="0" />
          <el-option label="录入" :value="1" />
          <el-option label="HIS" :value="2" />
        </el-select>
      </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">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="listDat"
      @selection-change="handleSelectionChange"
      max-height="600"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" width="100" />
      <el-table-column
        label="性别"
        align="center"
        prop="gender"
        show-overflow-tooltip
        width="100"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.gender == 0">男</span>
          <span v-if="scope.row.gender == 1">女</span>
        </template>
      </el-table-column>

      <el-table-column
        label="出生日期"
        align="center"
        prop="birthDate"
        show-overflow-tooltip
        width="150"
      >
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}
        </template>
      </el-table-column>
      <el-table-column
        label="民族"
        align="center"
        prop="ethnicity"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        label="受教育年限"
        align="center"
        prop="educationYears"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        label="手机号码"
        align="center"
        prop="phone"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column
        label="证件类型"
        align="center"
        prop="idCardType"
        show-overflow-tooltip
        width="200"
      >
        <template slot-scope="scope">
          {{ idCardTypeValue[scope.row.idCardType] }}
        </template>
      </el-table-column>
      <el-table-column
        label="证件号码"
        align="center"
        prop="idCard"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        fixed="right"
        label="随访队列"
        align="center"
        prop="queueList"
        show-overflow-tooltip
        width="180"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.queueList && scope.row.queueList.length">
            {{ scope.row.queueList.map((i) => i.queueName).join(",") }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="创建人/创建时间"
        align="center"
        width="130"
        fixed="right"
      >
        <template slot-scope="scope">
          <div>{{ scope.row.createBy }}</div>
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >队列管理</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handlePatient(scope.row)"
            >患者档案</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-notebook-2"
            @click="handleMedical(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"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog
      class="popup"
      :title="title"
      :visible.sync="open"
      width="780px"
      append-to-body
    >
      <el-form
        class="formStep"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="90px"
      >
        <el-form-item label="随访队列" prop="gender">
          <el-select v-model="form.queueIdList" multiple placeholder="请选择">
            <el-option
              v-for="item in followupList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryPatient, followupQuery, updPatient } from "@/api/followupFile";
export default {
  name: "Notice",
  data() {
    return {
      fileList: [],
      idCardType: [
        {
          label: "身份证",
          value: 0,
        },
        {
          label: "护照或外国人永居证",
          value: 1,
        },
        {
          label: "港澳居民来往内地通行",
          value: 2,
        },
        {
          label: "台湾居民来往大陆通行证",
          value: 3,
        },
      ],
      idCardTypeValue: {
        0: "身份证",
        1: "护照或外国人永居证",
        2: "港澳居民来往内地通行",
        3: "台湾居民来往大陆通行证",
      },
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      listDat: [{}], // 公告表格数据
      title: "", // 弹出层标题
      open: false, // 是否显示弹出层
      importOpen: false, // 导入弹窗
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      formDisabled: false,
      importform: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "患者姓名不能为空", trigger: "blur" },
        ],
      },
      followupList: [], // 随访队列
    };
  },
  created() {
    this.getList();
    this.getFollowupQuery();
  },
  methods: {
    // 获取随访队列信息
    getFollowupQuery() {
      followupQuery({
        pageNum: -1,
        param: {},
      }).then((res) => {
        this.followupList = res.data.list;
      });
    },

    /** 查询公告列表 */
    getList() {
      this.loading = true;
      queryPatient(this.queryParams).then((res) => {
        this.listDat = res.data.list;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "队列管理";
      let queueList = row.queueList.map((item) => item.queueId);
      this.form = JSON.parse(
        JSON.stringify({
          ...row,
          patientId: row.id,
          queueIdList: queueList,
        })
      );
    },
    /** 跳转患者档案 */
    handlePatient(row) {},
    /** 诊疗档案 */
    handleMedical(row) {},
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updPatient(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const idList = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除当前选择的患者数据？")
        .then(function () {
          return patientDel({ idList: idList });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/user/export",
        {
          ...this.queryParams.params,
        },
        `患者档案.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
  },
};
</script>
<style scoped src="@/assets/styles/common.css"></style>
<style scoped>
.form-item-age {
  display: flex;
  align-items: center;
}
.form-item-age span {
  margin: 0 10px;
}
.form-item-age >>> .el-input {
  width: 100px;
}
</style>
<!-- >>> .el-input__inner {
  padding: 0 15px !important;
} -->
