<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="noticeTitle">
        <el-input
          style="width: 300px"
          v-model="queryParams.param.keywords"
          placeholder="支持姓名、全拼、简拼、手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年龄范围" prop="createBy">
        <div class="form-item-age">
          <el-input
            v-model="queryParams.param.startAge"
            placeholder="最小年龄"
            clearable
            @keyup.enter.native="handleQuery"
          />
          <span>—</span>
          <el-input
            v-model="queryParams.param.endAge"
            placeholder="最大年龄"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </div>
      </el-form-item>
      <el-form-item label="建档组织" prop="tenantId">
        <el-select
          v-model="queryParams.param.tenantId"
          placeholder="请选择"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in tenantsListData"
            :label="item.name"
            :value="item.id"
            :key="index"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-form-item label="筛查时间" prop="doctorNo">
          <el-date-picker
            v-model="queryTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="listDat"
      @selection-change="handleSelectionChange"
      max-height="600"
      row-key="id"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        :reserve-selection="true"
      />
      <el-table-column
        label="单位"
        align="center"
        prop="SCWJ-DW"
        min-width="150"
        show-overflow-tooltip
        fixed
      />
      <el-table-column
        label="姓名"
        align="center"
        prop="SCWJ-NAME"
        min-width="100"
        show-overflow-tooltip
        fixed
      />
      <el-table-column
        label="性别"
        align="center"
        prop="SCWJ-SEX"
        min-width="100"
      />
      <el-table-column
        label="出生日期"
        align="center"
        prop="SCWJ-BIRTH"
        min-width="100"
      />
      <el-table-column
        label="年龄"
        align="center"
        prop="SCWJ-AGE"
        min-width="100"
      />
      <el-table-column
        label="联系方式"
        align="center"
        prop="SCWJ-PHONE"
        min-width="120"
      />
      <el-table-column
        label="身高（cm）"
        align="center"
        prop="SCWJ-HEIGHT"
        min-width="100"
      />
      <el-table-column
        label="体重（kg）"
        align="center"
        prop="SCWJ-WEIGHT"
        min-width="100"
      />
      <el-table-column
        label="BMI"
        align="center"
        prop="SCWJ-BMI"
        min-width="100"
      />
      <el-table-column
        show-overflow-tooltip
        label="体重自评结论"
        align="center"
        prop="SCWJ-JL"
        min-width="150"
      >
        <template slot-scope="scope">
          <span :class="`BIMTips${BMIVerdict[scope.row['SCWJ-JL']]}`">
            {{ scope.row["SCWJ-JL"] || "- - -" }}
          </span>
        </template>
      </el-table-column>

      <el-table-column
        label="BMI"
        align="center"
        prop="SCWJ-BMI"
        min-width="100"
      />
      <el-table-column
        label="上床睡觉时间"
        align="center"
        prop="SCWJ-SCSJ"
        min-width="100"
      />
      <el-table-column
        label="入睡时间"
        align="center"
        prop="SCWJ-RSSJ"
        min-width="100"
      />
      <el-table-column
        label="睡醒时间"
        align="center"
        prop="SCWJ-SXSJ"
        min-width="100"
      />
      <el-table-column
        label="起床时间"
        align="center"
        prop="SCWJ-QCSJ"
        min-width="100"
      />
      <el-table-column label="睡眠效率" align="center">
        <template slot-scope="scope">
          <span
            >{{ scope.row["SCWJ-SMXL"]
            }}<span v-if="scope.row['SCWJ-SMXL']">%</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="疾病风险"
        align="center"
        prop="SCWJ-XBS"
        min-width="200"
      />
      <el-table-column
        label="疾病风险-其他"
        align="center"
        prop="SCWJ-XBS-QT"
        min-width="150"
      />
      <el-table-column
        label="筛查结论"
        align="center"
        prop="SCREEN-JL"
        min-width="150"
      />
      <!-- <el-table-column
        label="失眠自评（SRSS）（分）"
        align="center"
        prop="SCWJ-RESULT"
        min-width="180"
      >
        <template slot-scope="scope">
          <span> {{ scope.row["SCWJ-RESULT"] }} </span>
        </template>
      </el-table-column>
      <el-table-column
        label="可接受的治疗方式"
        align="center"
        prop="SCWJ-ZLFS"
        min-width="200"
      />
      <el-table-column
        label="可接受的治疗周期（月）"
        align="center"
        prop="SCWJ-ZLZQ"
        min-width="200"
      /> -->
      <el-table-column
        fixed="right"
        label="筛查时间"
        align="center"
        min-width="140"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="组织"
        align="center"
        prop="tenantName"
        min-width="150"
        show-overflow-tooltip
        fixed="right"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { queryDetail } from "@/api/screening.js";
import { tenantsList } from "@/api/member";
export default {
  name: "Notice",
  dicts: ["sys_notice_status", "sys_notice_type"],
  data() {
    return {
      BMIVerdict: {
        体重过轻: "1",
        正常: "2",
        超重: "3",
        肥胖: "4",
      },
      queryTime: [], // 创建时间范围
      tenantsListData: [], // 组织列表
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      listDat: [], // 公告表格数据
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        param: {
          keywords: "", //关键字
          startAge: "", //开始年龄
          endAge: "", //结束年龄
          tenantId: "", //建档组织
          createBy: "", //建档人
          sourceId: "", //来源
        },
      },
    };
  },
  created() {
    // this.getLastWeekDays(); // 获取上一周的开始结束时间
    this.getList(); // 列表
    this.getTenantsList(); // 组织列表
  },
  methods: {
    // 获取上一周的开始结束时间
    getLastWeekDays() {
      let obj = {
        startTime: "",
        endTime: "",
      };
      obj.startTime = this.$moment(
        this.$moment()
          .week(this.$moment().week() - 1)
          .startOf("week")
          .add(1, "days")
          .valueOf()
      ).format("YYYY-MM-DD");
      obj.endTime = this.$moment(
        this.$moment()
          .week(this.$moment().week() - 1)
          .endOf("week")
          .add(1, "days")
          .valueOf()
      ).format("YYYY-MM-DD");
      this.queryTime = [obj.startTime, obj.endTime];
    },
    // 组织列表
    getTenantsList() {
      tenantsList({
        pageNum: -1,
        param: {},
      }).then((res) => {
        this.tenantsListData = res.data.list;
      });
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      // 判断时间数组是否有值
      this.queryParams.param.startTime = "";
      this.queryParams.param.endTime = "";
      if (this.queryTime?.length) {
        this.queryParams.param.startTime = this.queryTime[0] + " " + "00:00:00";
        this.queryParams.param.endTime = this.queryTime[1] + " " + "23:59:59";
      }
      queryDetail(this.queryParams).then((res) => {
        this.listDat = res.data.list;
        this.listDat.forEach((item) => {
          if (item.detailList) {
            item.detailList?.forEach((row) => {
              item[row.questionCode] = row.answer;
            });
          }
        });
        this.total = res.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.param = {
        keywords: "", //关键字
        startAge: "", //开始年龄
        endAge: "", //结束年龄
        tenantId: "", //建档组织
      };
      this.queryTime = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 导出按钮操作 */
    handleExport() {
      let data = JSON.parse(JSON.stringify(this.queryParams));
      data.param.idList = this.ids;
      this.download1(
        "/screening/exportScreen",
        {
          ...data,
        },
        `筛查列表.xlsx`
      );
    },
  },
};
</script>
<style scoped src="@/assets/styles/common.css"></style>
<style scoped>
.BIMTips1 {
  color: #cccccc;
}
.BIMTips2 {
  color: #66cc00;
}
.BIMTips3 {
  color: #c3c300;
}
.BIMTips4 {
  color: #ff9900;
}
.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;
} -->
