<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="healthDataList" @selection-change="handleSelectionChange" >
 <!--     <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="用户电话" align="center" prop="phone" /> -->
      <el-table-column label="体重" align="center" prop="weight" />
      <el-table-column label="心率" align="center" prop="heartRate" />
      <el-table-column label="体温" align="center" prop="temperature" />
      <el-table-column label="血压" align="center" prop="bloodPressure" />
      <el-table-column label="血糖" align="center" prop="bloodSugar" />
      <el-table-column label="尿酸值" align="center" prop="uric" />
      <el-table-column label="睡眠" align="center" prop="sleep" />
      <el-table-column label="步数或运动" align="center" prop="step" />
      <el-table-column label="排便" align="center" prop="shit" />
      <el-table-column label="心情" align="center" prop="mood" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改健康数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="体重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入体重" />
        </el-form-item>
        <el-form-item label="心率" prop="heartRate">
          <el-input v-model="form.heartRate" placeholder="请输入心率" />
        </el-form-item>
        <el-form-item label="体温" prop="temperature">
          <el-input v-model="form.temperature" placeholder="请输入体温" />
        </el-form-item>
        <el-form-item label="血压" prop="bloodPressure">
          <el-input v-model="form.bloodPressure" placeholder="请输入血压" />
        </el-form-item>
        <el-form-item label="血糖" prop="bloodSugar">
          <el-input v-model="form.bloodSugar" placeholder="请输入血糖" />
        </el-form-item>
        <el-form-item label="尿酸值" prop="uric">
          <el-input v-model="form.uric" placeholder="请输入尿酸值" />
        </el-form-item>
        <el-form-item label="睡眠" prop="sleep">
          <el-input v-model="form.sleep" placeholder="请输入睡眠" />
        </el-form-item>
        <el-form-item label="步数或运动" prop="step">
          <el-input v-model="form.step" placeholder="请输入步数或运动" />
        </el-form-item>
        <el-form-item label="排便" prop="shit">
          <el-input v-model="form.shit" placeholder="请输入排便" />
        </el-form-item>
        <el-form-item label="心情" prop="mood">
          <el-input v-model="form.mood" placeholder="请输入心情" />
        </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 { listHealthData, getHealthData, delHealthData, addHealthData, updateHealthData, exportHealthData } from "@/api/his/healthData";
export default {
  name: "HealthData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 健康数据表格数据
      healthDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        weight: null,
        heartRate: null,
        temperature: null,
        bloodPressure: null,
        bloodSugar: null,
        uric: null,
        sleep: null,
        step: null,
        shit: null,
        mood: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {

  },
  methods: {
    getHealthDataDetails(userId){
      this.queryParams.userId=userId
      this.getList();
    },
    /** 查询健康数据列表 */
    getList() {
      this.loading = true;
      listHealthData(this.queryParams).then(response => {
        this.healthDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        weight: null,
        heartRate: null,
        temperature: null,
        bloodPressure: null,
        bloodSugar: null,
        uric: null,
        sleep: null,
        step: null,
        shit: null,
        mood: null,
        createTime: null
      };
      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
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加健康数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHealthData(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改健康数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHealthData(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHealthData(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除健康数据编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delHealthData(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有健康数据数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportHealthData(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
