<template>
 <div class="main-contain">
    <div class="position">
      <bread-crumb :crumbs='crumbs'></bread-crumb>
    </div>
    <div class="app-container" id="outcome-indicators-query">
      <analysis-nav activeName="weight"></analysis-nav>

      <div class="cell clearfix">
        <label class="title">
          <span class="name">日期查询</span> :
        </label>
        <el-date-picker
          v-model="listQuery.start_time"
          prefix-icon="el-icon-date"
          @change="changeTime"
          :editable="false"
          style="width: 196px;"
          type="date"
          placeholder="选择日期时间"
          align="right"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
        <span class>-</span>
        <el-date-picker
          v-model="listQuery.end_time"
          prefix-icon="el-icon-date"
          @change="changeTime"
          :editable="false"
          style="width: 196px;"
          type="date"
          placeholder="选择日期时间"
          align="right"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </div>
      <div class="cell clearfix">
        <label class="title">
          <span class="name">转归状态</span> :
        </label>
        <div class="time">
          <ul class>
            <li
              :class="item.lapseto==lapsetoType?'active':''"
              @click="selectLapseTo( item.lapseto)"
              v-for="item in lapsetoState"
              :key="item.value"
            >{{item.label}}</li>
          </ul>
        </div>
      </div>
      <div class="cell clearfix">
        <label class="title">
          <span class="name">年龄</span> :
        </label>
        <div class="time">
          <ul class>
            <li
              :class="item.age==ageType?'active':''"
              @click="selectAge( item.age)"
              v-for="item in ageOptions"
              :key="item.value"
            >{{item.label}}</li>
          </ul>
        </div>
      </div>


      <div class="cqd-dataTitle">统计图</div>
      <el-row :gutter="32">
        <el-col :xs="24" :sm="24" :lg="8">
          <div class="chart-wrapper">
              <pie-chart title="干体重（人次）" :options="CompletionOptions"/>
          </div>
        </el-col>
        <el-col :xs="24" :sm="24" :lg="8">
          <div class="chart-wrapper">
              <pie-chart title="体重增加（人次）" :options="WeightIncrOptions"/>
          </div>
        </el-col>
        <el-col :xs="24" :sm="24" :lg="8">
          <div class="chart-wrapper">
              <pie-chart title="透后称重（人次）" :options="WeightAfterOptions"/>
          </div>
        </el-col>
      </el-row>

      <div style="height:20px">&nbsp;</div>
      <div class="cqd-dataTitle">统计表</div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        border
        fit
        highlight-current-row
        style="width: 100%;margin-top: 10px;"
        :header-cell-style="{ backgroundColor: 'rgb(245, 247, 250)'}"
      >

        <el-table-column  label="透析日期" min-width="80" property="schedule_date" align="center">
          <template slot-scope="scope"><span>{{getTime(scope.row.schedule_date, '{y}{m}{d}')}}</span></template>
        </el-table-column>
        <el-table-column  label="透析号" min-width="100" property="patient" align="center">
          <template slot-scope="scope"><span>{{scope.row.patient.dialysis_no}}</span></template>
        </el-table-column>
        <el-table-column  label="患者姓名" min-width="100" property="patient" align="center">
          <template slot-scope="scope"><span>{{scope.row.patient.name}}</span></template>
        </el-table-column>
        <el-table-column  label="性别" min-width="80" property="patient" align="center">
          <template slot-scope="scope"><span>{{getPatientGender(scope.row.patient.gender)}}</span></template>
        </el-table-column>
        <el-table-column  label="年龄" min-width="80" property="patient" align="center">
          <template slot-scope="scope"><span>{{tranAge(scope.row.patient.birthday)}}</span></template>
        </el-table-column>
        <el-table-column  label="转归状态" min-width="80" property="patient" align="center">
          <template slot-scope="scope"><span>{{tranLapseto(scope.row.patient.lapseto)}}</span></template>
        </el-table-column>
        <el-table-column  label="干体重" min-width="80" property="assessment_before_dislysis" align="center">
          <template slot-scope="scope"><span>{{scope.row.assessment_before_dislysis?scope.row.assessment_before_dislysis.dry_weight + 'Kg':'--'}}</span></template>
        </el-table-column>
        <el-table-column  label="透前体重" min-width="80" property="assessment_before_dislysis" align="center">
          <template slot-scope="scope"><span>{{scope.row.assessment_before_dislysis?scope.row.assessment_before_dislysis.weight_before + 'Kg':'--'}}</span></template>
        </el-table-column>
        <el-table-column  label="体重增加" min-width="80" property="assessment_before_dislysis" align="center">
          <template slot-scope="scope"><span>{{scope.row.assessment_before_dislysis?scope.row.assessment_before_dislysis.weight_before - scope.row.assessment_before_dislysis.dry_weight + 'Kg':'--'}}</span></template>
        </el-table-column>
        <el-table-column  label="透后称重" min-width="80" property="assessment_after_dislysis" align="center">
          <template slot-scope="scope"><span>{{scope.row.assessment_after_dislysis?scope.row.assessment_after_dislysis.weight_after+ 'Kg':'--'}}</span></template>
        </el-table-column>

        <el-table-column  label="统计" min-width="80" property="patient" align="center">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="统计" placement="top">
              <el-button type="primary" icon="el-icon-share" size="small" @click="OpenPage(scope.row.patient.id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- <pagi-nation title="分页"></pagi-nation> -->

      <el-pagination
      align="right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.pate"
        :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>
</template>


<script>
import { GetPAWeightData  } from "@/api/qcd";
import PieChart from '../components/PieChart'
import {jsGetAge, uParseTime} from "@/utils/tools";
import analysisNav from './components/analysisNav'
import BreadCrumb from "@/xt_pages/components/bread-crumb";
export default {
  name: "totalAnalysis",
  data() {
    return {
      crumbs:[
        {path:false, name:'科室质控'},
        {path:false, name:'患者分析'},
        {path:false, name:'体重分析'},
      ],
      total: 0,
      patientMap:{},
      listQuery: {
        start_time: "",
        end_time: "",
        page: 1,
        limit: 10,
      },
      CompletionOptions:{
        legend:[],
        series:[],
      },
      WeightIncrOptions:{
        legend:[],
        series:[],
      },
      WeightAfterOptions:{
        legend:[],
        series:[],
      },
      tableData: [],
      lapsetoType: 0,
      lapsetoState: [
        { value: 0, label: "全部", source: 0, lapseto: 0 },
        { value: 1, label: "转出", source: 0, lapseto: 2 },
        { value: 2, label: "留治", source: 0, lapseto: 1 }
      ],
      ageType: 0,
      ageOptions: [
        { value: 0, label: "全部", age: 0 },
        { value: 1, label: "20～30", age: 1 },
        { value: 2, label: "30～40", age: 2 },
        { value: 3, label: "40～50", age: 3 },
        { value: 4, label: "其他", age: 4 },
      ],
    };
  },
  created() {

    var nowDate = new Date();
    var nowYear = nowDate.getFullYear();
    var nowMonth = nowDate.getMonth() + 1;
    var nowDay = nowDate.getDate();
    this.listQuery.end_time =
      nowYear +
      "-" +
      (nowMonth < 10 ? "0" + nowMonth : nowMonth) +
      "-" +
      (nowDay < 10 ? "0" + nowDay : nowDay);
    nowDate.setMonth(nowDate.getMonth() - 3);
    nowYear = nowDate.getFullYear();
    nowMonth = nowDate.getMonth() + 1;
    nowDay = nowDate.getDate();
    this.listQuery.start_time =
      nowYear +
      "-" +
      (nowMonth < 10 ? "0" + nowMonth : nowMonth) +
      "-" +
      (nowDay < 10 ? "0" + nowDay : nowDay);
    this.getList();
  },
  methods: {
    OpenPage(id) {
      this.$router.push({ path: "/qcd/pa/person/weight?id="+id });
    },
    getList() {
      GetPAWeightData(this.listQuery).then(response => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
            this.tableData = [];
            this.total = response.data.data.total;
            var weights = response.data.data.weights;
            for (const index in weights) {
                this.tableData.push(weights[index]);
            }

            this.CompletionOptions = {
                legend:[],
                series:[],
            };
            var dryweightOptions = [
              { weight:1, name:'<40Kg' },
              { weight:2, name:'40～50Kg' },
              { weight:3, name:'50～60Kg' },
              { weight:4, name:'60～70Kg' },
              { weight:5, name:'>=70Kg' },
            ];
            var other = response.data.data.total;
            var dryweight = response.data.data.dryweight;
            var dryweightMap = {};
            for (const index in dryweight) {
              dryweightMap[dryweight[index].weight] = dryweight[index].count;
              other -= dryweight[index].count;
            }
            for (const index in dryweightOptions) {
              this.CompletionOptions.legend.push(dryweightOptions[index].name);
              if(dryweightOptions[index].weight in dryweightMap) {
                this.CompletionOptions.series.push({
                    value:dryweightMap[dryweightOptions[index].weight],name:dryweightOptions[index].name
                });
              }else {
                this.CompletionOptions.series.push({
                    value:0,name:dryweightOptions[index].name
                });
              }
            }
            if (other > 0) {
              this.CompletionOptions.legend.push('其他');
              this.CompletionOptions.series.push({
                  value:other,name:'其他'
              });
            }

            this.WeightIncrOptions = {
                legend:[],
                series:[],
            };
            var weightOptions = [
              { weight:1, name:'<1Kg' },
              { weight:2, name:'1～2Kg' },
              { weight:3, name:'2～3Kg' },
              { weight:4, name:'3～4Kg' },
              { weight:5, name:'>=4Kg' },
            ];
            var other = response.data.data.total;
            var weightincrease = response.data.data.weightincrease;
            var weightincreaseMap = {};
            for (const index in weightincrease) {
              weightincreaseMap[weightincrease[index].weight] = weightincrease[index].count;
              other -= weightincrease[index].count;
            }
            for (const index in weightOptions) {
              this.WeightIncrOptions.legend.push(weightOptions[index].name);
              if(weightOptions[index].weight in weightincreaseMap) {
                this.WeightIncrOptions.series.push({
                    value:weightincreaseMap[weightOptions[index].weight],name:weightOptions[index].name
                });
              }else {
                this.WeightIncrOptions.series.push({
                    value:0,name:weightOptions[index].name
                });
              }
            }
            if (other > 0) {
              this.WeightIncrOptions.legend.push('其他');
              this.WeightIncrOptions.series.push({
                  value:other,name:'其他'
              });
            }

            this.WeightAfterOptions = {
                legend:[],
                series:[],
            };
            var afterWeightOptions = [
              { weight:1, name:'<40Kg' },
              { weight:2, name:'40～50Kg' },
              { weight:3, name:'50～60Kg' },
              { weight:4, name:'60～70Kg' },
              { weight:5, name:'>=70Kg' },
            ];
            var other = response.data.data.total;
            var afterweight = response.data.data.afterweight;
            var afterweightMap = {};
            for (const index in afterweight) {
              afterweightMap[afterweight[index].weight] = afterweight[index].count;
              other -= afterweight[index].count;
            }
            for (const index in weightOptions) {
              this.WeightAfterOptions.legend.push(weightOptions[index].name);
              if(weightOptions[index].weight in afterweightMap) {
                this.WeightAfterOptions.series.push({
                    value:afterweightMap[weightOptions[index].weight],name:weightOptions[index].name
                });
              }else {
                this.WeightAfterOptions.series.push({
                    value:0,name:weightOptions[index].name
                });
              }
            }
            if (other > 0) {
              this.WeightAfterOptions.legend.push('其他');
              this.WeightAfterOptions.series.push({
                  value:other,name:'其他'
              });
            }


        }
      });
    },
    changeTime() {
      this.getList();
    },
    changeModel() {
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    selectLapseTo(lapseto) {
      this.lapsetoType = lapseto;
      this.listQuery.lapseto = lapseto;
      this.getList();
    },
    selectAge(age) {
      this.ageType = age;
      this.listQuery.age = age;
      this.getList();
    },

    getTime(value, temp) {
        if (value != undefined) {
          return uParseTime(value, temp)
        }
        return ""
    },
    getProjectName(id) {
      return (id in this.projectOptions) ? this.projectOptions[id].project_name:'';
    },
    getPatientName(id) {
      return (id in this.patientMap) ? this.patientMap[id].name:'';
    },
    tranLapseto(lapseto) {
      if (lapseto == 1) {
        return '留治';
      } else if(lapseto == 2) {
        return '转出';
      }
      return '';
    },
    getPatientGender(gender) {
      if (gender == 1) {
        return '男';
      } else if(gender == 2) {
        return '女';
      }
      return '';
    },
    tranAge(birthday) {
        var birth = uParseTime(birthday, '{y}-{m}-{d}');
        return jsGetAge(birth, '-');
    },
    getPatientDialysisNo(id) {
      return (id in this.patientMap) ? this.patientMap[id].dialysis_no:'';
    },
    getItemVulue(project_id, item_id, row) {
        var key = project_id + '_' + item_id;
        return (typeof(row.items) !== 'undefined' && (key in row.items)) ? row.items[key].inspect_value:'';
    },
    renderheader(h, { column, $index }) {
        return h('span', {}, [
            h('span', {}, column.label.split('//')[0]),
            h('br'),
            h('span', {}, column.label.split('//')[1])
        ])
    },
  },
  components: {
      analysisNav,
      PieChart,
      BreadCrumb
  }
};
</script>
<style>
#oictable ::-webkit-scrollbar {
  height: 15px;
}
#outcome-indicators-query .el-form-item {
    margin-bottom: 0px !important;
}
</style>


<style rel="stylesheet/scss" lang="scss" scoped>
.app-container {
//   margin: 20px;
  font-size: 15px;
  .filter-container {
    padding-bottom: 5px;
  }
  .cqd-dataTitle{
    color: #303133;
    font-size: 14px;
    border-bottom: 2px #E4E7ED solid;
    height: 36px;
    line-height: 36px;
    margin: 0 0 25px 0;
    position: relative;
  }
  .cqd-dataTitle::before {
      position: absolute;
      left: 0;
      bottom: -2px;
      content: "";
      width: 42px;
      height: 2px;
      background: #409eff;
  }
  .search-component {
    width: 500px;
    .searchBox {
      width: 300px;
      height: 36px;
      line-height: 36px;
      padding-left: 15px;
      border: 1px #dcdfe6 solid;
      border-right: none;
      outline: none;
      float: left;
      border-radius: 6px 0 0 6px;
      font-size: 14px;
      color: #333;
      background: #fff;
      box-shadow: 3px 3px 4px rgba(135, 135, 135, 0.05);
    }
    .searchBtn {
      background-color: #409eff;
      color: #fff;
      font-size: 15px;
      text-align: center;
      height: 36px;
      line-height: 36px;
      float: left;
      outline: none;
      width: 70px;
      border: none;
      border-radius: 0 6px 6px 0;
      font-family: "Microsoft Yahei";
      cursor: pointer;
    }
  }

  .amount {
    font-weight: normal;
    padding: 10px 0 0 0;
    color: #606266;
    font-size: 14px;
    span {
      color: #ef2525;
      font-family: "Arial";
      padding: 0 2px;
    }
  }
}
</style>
