<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb :crumbs="crumbs"></bread-crumb>
    </div>
    <div class="app-container">
      <div class="page_process">
        <new-nav activeName="monitor"></new-nav>
        <div class="cell clearfix">
          <el-form :inline="true">
            <el-form-item label>
              <el-autocomplete
                class="checkSearch"
                popper-class="my-autocomplete"
                v-model="search_value"
                :fetch-suggestions="querySearchAsync"
                :trigger-on-focus="false"
                placeholder="请输入病人名字"
                @select="handleSelect"
              >
                <i class="el-icon-search el-input__icon" slot="suffix"></i>
                <template slot-scope="{ item }">
                  <div class="name">{{ item.name }}</div>
                </template>
              </el-autocomplete>

            </el-form-item>
          </el-form>
          <el-select v-model="query.statistics_type" placeholder="请选择" style="width:110px;margin-right:10px" @change="changeProject">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.name"
              :value="item.value"

            ></el-option>
          </el-select>
          <label class="title">
            <span class="name">日期查询</span> :
          </label>
          <el-date-picker
            v-model="query.start_time"
            prefix-icon="el-icon-date"
            @change="changeTime"
            :editable="false"
            style="width: 150px;"
            type="date"
            :picker-options="pickerOptions"
            placeholder="选择日期时间"
            align="right"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
          <span class>-</span>
          <el-date-picker
            v-model="query.end_time"
            prefix-icon="el-icon-date"
            @change="changeEndTime"
            :editable="false"
            :picker-options="pickerOptions"
            style="width: 150px;"
            type="date"
            placeholder="选择日期时间"
            align="right"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </div>
        <el-container>
          <div style="width:150px">
            <div class="tableTitle">患者列表</div>
            <el-table :data="patientsData" border style="width: 100%;" height="500" :row-style="{ color: '#303133' }"
                      :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}"
                      highlight-current-row
                      @current-change="handleChange">
              <el-table-column prop="dialysis_no" label="透析号" width="80">
                <template slot-scope="scope">{{scope.row.dialysis_no}}</template>
              </el-table-column>
              <el-table-column prop="name" label="姓名" width="80">
                <template slot-scope="scope">{{ scope.row.name }}</template>
              </el-table-column>
            </el-table>
          </div>
          <div style="padding-left:10px;flex:1">
            <div class="tableTitle">指标趋势</div>
            <div>
              <line-chart :options="chart"></line-chart>
            </div>
            <div class="tableTitle">统计表</div>
            <div>
              <el-table :data="tableData" style="width: 100%" :height="tableHeight" v-loading="loading">
                <el-table-column fixed label="监测时间" align="center">
                  <template slot-scope="scope">
                    <!--{{getTime(scope.row.operate_time)}}-->
                    {{scope.row.operate_time | parseTime("{y}-{m}-{d} {h}:{i}") }}

                  </template>
                </el-table-column>

                <el-table-column label="收缩压" align="center">
                  <template slot-scope="scope">
                    {{scope.row.systolic_blood_pressure?scope.row.systolic_blood_pressure:''}}
                  </template>
                </el-table-column>

                <el-table-column label="舒张压" align="center">
                  <template slot-scope="scope">
                    {{scope.row.diastolic_blood_pressure?scope.row.diastolic_blood_pressure:''}}
                  </template>
                </el-table-column>

                <el-table-column label="脉搏" align="center">
                  <template slot-scope="scope">
                    {{scope.row.pulse_frequency?scope.row.pulse_frequency:''}}
                  </template>
                </el-table-column>

                <el-table-column label="呼吸" align="center">
                  <template slot-scope="scope">
                    {{scope.row.breathing_rate?scope.row.breathing_rate:''}}
                  </template>
                </el-table-column>

                <el-table-column label="体温" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.temperature?scope.row.temperature:''}}
                  </template>
                </el-table-column>

                <el-table-column label="血流量" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.blood_flow_volume?scope.row.blood_flow_volume:''}}
                  </template>
                </el-table-column>

                <el-table-column label="静脉压" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.venous_pressure?scope.row.venous_pressure:''}}
                  </template>
                </el-table-column>


                <el-table-column label="动脉压" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.arterial_pressure?scope.row.arterial_pressure:''}}

                  </template>
                </el-table-column>

                <el-table-column label="跨膜压" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.transmembrane_pressure?scope.row.transmembrane_pressure:''}}
                  </template>
                </el-table-column>


                <el-table-column label="透析液温度" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.dialysate_temperature?scope.row.dialysate_temperature:''}}
                  </template>
                </el-table-column>

                <el-table-column label="超滤率" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.ultrafiltration_rate?scope.row.ultrafiltration_rate:''}}
                  </template>
                </el-table-column>

                <el-table-column label="超滤量" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.ultrafiltration_volume?scope.row.ultrafiltration_volume:''}}
                  </template>
                </el-table-column>
              </el-table>

              <el-pagination
                align="right"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="query.page"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="10"
                background
                style="margin-top:20px;"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              ></el-pagination>
            </div>
          </div>
        </el-container>
      </div>
    </div>
  </div>
</template>


<script>
  import echarts from 'echarts'
  import NewNav from '../indicatorControlAnalysis/components/NewNav'
  import BreadCrumb from '@/xt_pages/components/bread-crumb'
  import LineChart from '../../qcd/components/LineChart'
  import { getCurrentOrgPatients } from "@/api/common/common";
  import {
    GetDefalutPatient,
    GetDialysisWatchChartData,
    GetDialysisWatchTableData
  } from '@/api/common/statistics'


  import {
    PostSearch
  } from '@/api/patient'

  import { uParseTime } from "@/utils/tools";
  import { getDataConfig } from "@/utils/data";

  export default {
    components: {
      LineChart,
      BreadCrumb,
      NewNav
    },
    data() {
      return {
        pickerOptions: {
          disabledDate(time) {
            let threeMonths = new Date(new Date().setFullYear(new Date().getFullYear()-1)).getTime() - 24 * 3600 * 1000;
            return time.getTime() > Date.now() || time.getTime() < threeMonths;;
          }
        },
        patientsData:[],
        tableData: [
        ],
        loading: false,
        search_value: '',
        total: '',
        query: {
          patient_id: '',
          statistics_type: 1,
          start_time: '',
          end_time: '',
          limit: 10,
          page: 1
        },
        options: [
          { value: 1, name: '收缩压' },
          { value: 2, name: '舒张压' },
          { value: 3, name: '脉搏' },
          { value: 4, name: '呼吸' },
          { value: 5, name: '体温' },
          { value: 6, name: '血流量' },
          { value: 7, name: '静脉压' },
          { value: 8, name: '动脉压' },
          { value: 9, name: '跨膜压' },
          { value: 10, name: '透析液温度' },
          { value: 11, name: '超滤率' },
          { value: 12, name: '超滤量' },
        ],
        crumbs: [
          { path: false, name: '科室质控' },
          { path: false, name: '指标评估统计' },
          { path: false, name: '透析过程指标统计' }
        ],
        tableData: [],
        tableData1: [],
        chart: {
          title: {
            text: 'ECharts 入门示例'
          },
          tooltip: {},
          legend: {
            data: [],
            left: 0
          },
          xAxis: {
            data: []
          },
          yAxis: {
            axisLabel: {
              formatter: '{value} %'
            },
            show: false
          },
          series: [
            {
              name: '',
              type: 'line',
              data: [],
              barWidth: 30,
              label: {
                normal: {
                  show: true,
                  position: 'top',
                  formatter: '{c}'
                }
              },
              //配置样式
              itemStyle: {
                //通常情况下：

                //每个柱子的颜色即为colorList数组里的每一项，如果柱子数目多于colorList的长度，则柱子颜色循环使用该数组
                normal: {
                  color: function(params) {
                    //我这边就两个柱子，大体就两个柱子颜色渐变，所以数组只有两个值，多个颜色就多个值
                    var colorList = [
                      ['#A9E0F3', '#9FBDFC'],

                      ['#FFD7C0', '#FF9994']
                    ]

                    var index = params.dataIndex
                    if (params.dataIndex >= colorList.length) {
                      index = params.dataIndex % colorList.length
                    }

                    return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                      { offset: 0, color: colorList[index][0] },
                      // { offset: 0.5, color: colorList[index][1] },
                      { offset: 1, color: colorList[index][1] }
                    ])
                  },
                  barBorderRadius: [5, 5, 0, 0] //柱状角成椭圆形
                },

                //鼠标悬停时：
                emphasis: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        },
        bar: {
          title: {
            text: 'ECharts 入门示例'
          },
          tooltip: {},
          legend: {
            data: [],
            left: 0
          },
          xAxis: {
            data: []
          },
          yAxis: {
            axisLabel: {
              formatter: '{value} %'
            },
            show: false
          },
          series: [
            {
              name: '',
              type: 'bar',
              data: [],
              barWidth: 30,
              label: {
                normal: {
                  show: true,
                  position: 'top',
                  formatter: '{c}次'
                }
              },
              //配置样式
              itemStyle: {
                //通常情况下：

                //每个柱子的颜色即为colorList数组里的每一项，如果柱子数目多于colorList的长度，则柱子颜色循环使用该数组
                normal: {
                  color: function(params) {
                    //我这边就两个柱子，大体就两个柱子颜色渐变，所以数组只有两个值，多个颜色就多个值
                    var colorList = [
                      ['#A9E0F3', '#9FBDFC'],
                      ['#FFD7C0', '#FF9994']
                    ]

                    var index = params.dataIndex
                    if (params.dataIndex >= colorList.length) {
                      index = params.dataIndex % colorList.length
                    }

                    return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                      { offset: 0, color: colorList[index][0] },
                      // { offset: 0.5, color: colorList[index][1] },
                      { offset: 1, color: colorList[index][1] }
                    ])
                  },
                  barBorderRadius: [5, 5, 0, 0] //柱状角成椭圆形
                },

                //鼠标悬停时：
                emphasis: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        }

      }
    },
    methods: {
      changeProject(val){
        this.query.statistics_type = val
        this.query.page = 1
        this.GetDialysisWatchChartData(this.query)

      },
      handleChange(val){
        this.query.patient_id = val.id
        this.query.page = 1
        this.GetDialysisWatchTableData(this.query)
        this.GetDialysisWatchChartData(this.query)
      },
      handleSizeChange(limit) {
        this.query.limit = limit;

      },
      handleCurrentChange(page) {
        this.query.page = page;

      },
      changeTime(val) {
        var time = this.getTimestamp(val) - this.getTimestamp(this.query.end_time);
        if (time > 0) {
          this.$message.error("结束时间不能小于开始时间");
          this.query.start_time = "";
        } else {
          // this.getDialysisList()
          this.query.page = 1;
          this.GetDialysisWatchChartData(this.query)
          this.GetDialysisWatchTableData(this.query)

        }

      },
      changeEndTime(val) {
        var time =
          this.getTimestamp(val) - this.getTimestamp(this.query.start_time);
        if (time < 0) {
          this.$message.error("结束时间不能小于开始时间");
          this.query.end_time = "";
        } else {
          this.query.page = 1;
          this.GetDialysisWatchChartData(this.query)
          this.GetDialysisWatchTableData(this.query)

        }
      },
      getTimestamp(time) {
        // 把时间日期转成时间戳
        return new Date(time).getTime() / 1000;
      },
      QueryOperaById: function(val) {
        let vascular_access_desc_name = "";
        let vascular_access_desc = getDataConfig(
          "hemodialysis",
          "vascular_access_desc"
        );
        for (let i = 0; i < vascular_access_desc.length; i++) {
          if (vascular_access_desc[i].id == val) {
            vascular_access_desc_name = vascular_access_desc[i].name;
          }
        }

        return vascular_access_desc_name;
      },
      GetAnticoagulantById: function (val) {
        let anticoagulan_name = ''
        var anticoagulantsConfitTwo = this.$store.getters.anticoagulants_confit
        let anticoagulant = anticoagulantsConfitTwo
        for (let keys in anticoagulant) {
          if (anticoagulant[keys].id == val) {
            anticoagulan_name = anticoagulant[keys].name
          }
        }
        return anticoagulan_name
      },
      getModeName(mode_id){
        return this.$store.getters.treatment_mode[mode_id] != undefined ? this.$store.getters.treatment_mode[mode_id].name: ""
      },
      changeTime() {

      }, changeEndTime() {


      }, GetDialysisWatchTableData(params) {
        this.loading = true
        this.tableData = []
        GetDialysisWatchTableData(params)
          .then(rs => {
            var resp = rs.data
            if (resp.state == 1) {
              this.loading = false
              for (let i = 0; i < resp.data.data.length; i++) {
                this.tableData.push(resp.data.data[i])
              }
            } else {
              this.loading = false
            }
          })
          .catch(error => {
          })
      },
      GetDialysisWatchChartData(params) {
        this.chart.xAxis.data = []
        this.chart.series[0].data = []
        this.bar.xAxis.data = []
        this.bar.series[0].data = []
        GetDialysisWatchChartData(params)
          .then(rs => {
            var resp = rs.data
            if (resp.state == 1) {
              if (this.query.statistics_type != 9 && this.query.statistics_type != 10) {
                for (let i = 0; i < resp.data.data.length; i++) {
                  this.chart.xAxis.data.push(resp.data.data[i].date)
                  this.chart.series[0].data.push(resp.data.data[i].value)
                }
              } else {
                for (let i = 0; i < resp.data.data.length; i++) {
                  this.bar.xAxis.data.push(resp.data.data[i].name)
                  this.bar.series[0].data.push(resp.data.data[i].total)
                }
              }
            } else {
            }
          })
          .catch(error => {
          })
      },
      handleSelect(val) {
        this.query.patient_id = val.id
        this.query.page = 1
        this.GetDialysisWatchTableData(this.query)
        this.GetDialysisWatchChartData(this.query)

      },
      querySearchAsync(keyword, cb) {
        let key = ''
        if (keyword != undefined) {
          key = keyword
        }
        let searchArray = []
        PostSearch(key).then(response => {
          if (response.data.state == 1) {
            searchArray = response.data.data.patient
            cb(searchArray)
          } else {
            cb([])
          }
        })
      },
      chooseWay(way) {
        this.wayType = way
      }, getCurrentOrgPatients(){
        getCurrentOrgPatients().then(response=>{
          if(response.data.state == 1){
            var patients = response.data.data.patients
            this.patientsData = patients
          }
        })
      },
    }, created() {
      var date = new Date()
      var year = date.getFullYear() //获取完整的年份(4位)
      var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1).toString() : date.getMonth() + 1 //获取当前月份(0-11,0代表1月)
      var day = date.getDate() < 0 ? '0' + date.getDate().toString() : date.getDate() //获取当前日(1-31)
      var last_month = date.getMonth() < 10 ? '0' + date.getMonth().toString() : date.getMonth() //获取当前月份(0-11,0代表1月)
      this.query.patient_id = 0
      this.query.end_time = year + '-' + month + '-' + day
      this.query.start_time = year + '-' + last_month + '-' + day
      this.query.statistics_type = 1
      this.GetDialysisWatchChartData(this.query)
      this.GetDialysisWatchTableData(this.query)
      this.getCurrentOrgPatients()


    }
  }
</script>

<style lang="scss" scoped>
  .tableTitle {
    font-size: 16px;
    color: #000;
    font-weight: bold;
    margin-bottom: 10px;
  }
</style>
<style lang="scss">
  .page_process {

  .el-button--medium {
    padding: 10px 8px;
  }

  .el-form-item {
    margin-bottom: 0;
  }

  }
</style>
