<template>
  <div class="routineBox">
    <div class="header">
      <div>
        <el-checkbox v-model="checkedSeJue" class="mr30">色觉检查</el-checkbox>
        <span style="margin-right: 16px">眼别：{{ osType }}</span>
        <span>结果：</span>
        <el-select v-model="osStatus" placeholder="请选择" style="display: inline-block;width: 120px" size="small">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <span style="margin-left: 32px;margin-right: 16px">眼别：{{ odType }}</span>
        <span>结果：</span>
        <el-select v-model="odStatus" placeholder="请选择" style="display: inline-block;width: 120px;" size="small">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div v-if="!onlyRead">
        <div size="small" style="display:inline-block;margin-left:10px;width: 32px;height: 32px;line-height:32px;text-align:center;background-color: #1e79ff;border-radius: 4px;vertical-align: middle" @click="addDataList">
          <img :src="require('@/assets/img/data.png')" alt="" style="width: 17px;height: 16px;margin-bottom: 4px">
        </div>
      </div>
    </div>
    <div class="content">
      <div class="lineBox">
        <el-checkbox v-model="checkedShiLi">视力</el-checkbox>
        <div style="display: flex">
          <echarts-yanya
            :id-name="'indicators-shili'"
            :chart-data="routineData.shili"
            class="scroll-echearts"
          />
          <div class="eyeBox">
            <div class="eye el">
              <div>最近一次</div>
              <div>眼别</div>
              <div>裸眼视力</div>
              <div>矫正视力</div>
            </div>
            <div class="eye">
              <div>{{ shiliLast.length ? shiliLast[0].examTime : '' }}</div>
              <div class="info">
                <span class="eye_w">OD</span>
                <span class="eye_w">OS</span>
              </div>
              <div class="info">
                <span class="eye_w">{{ shiliLast.length ? shiliLast[0].odVan : '' }}</span>
                <span class="eye_w">{{ shiliLast.length ? shiliLast[0].osVan : '' }}</span>
              </div>
              <div class="info">
                <span class="eye_w">{{ shiliLast.length ? shiliLast[0].odVag : '' }}</span>
                <span class="eye_w">{{ shiliLast.length ? shiliLast[0].osVag : '' }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="lineBox">
        <el-checkbox v-model="checkedYanYa">眼压</el-checkbox>
        <div style="display: flex">
          <echarts-yanya
            :id-name="'indicators-yanya'"
            :chart-data="routineData.yanya"
            class="scroll-echearts"
          />
          <div class="eyeBox" style="height: 135px">
            <div class="eye el">
              <div>最近一次</div>
              <div>眼别</div>
              <div>眼压</div>
            </div>
            <div class="eye">
              <div>{{ yanyaLast.length ? yanyaLast[0].examTime : '' }}</div>
              <div class="info">
                <span class="eye_w">OD</span>
                <span class="eye_w">OS</span>
              </div>
              <div class="info">
                <span class="eye_w">{{ yanyaLast.length ? yanyaLast[0].odIop : '' }}</span>
                <span class="eye_w">{{ yanyaLast.length ? yanyaLast[0].osIop : '' }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="lineBox">
        <el-checkbox v-model="checkedDL" @change="changeDL">电脑验光</el-checkbox>
        <el-table
          ref="tableDL"
          :data="routineData.dNyanguang"
          style="width: 100%;margin-top: 32px"
          @selection-change="handleSelectionDL"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column
            prop="examTime"
            label="时间"
            width="180"
          />
          <el-table-column
            prop="eyeType"
            label="眼别"
            width="180"
          />
          <el-table-column
            prop="odSph"
            label="球镜 DS"
          />
          <el-table-column
            prop="odCyl"
            label="柱镜 DC"
          />
          <el-table-column
            prop="odAx"
            label="轴位 Axis"
          />
          <el-table-column
            prop="odSe"
            label="等效球镜"
          />
        </el-table>
      </div>
      <div class="lineBox">
        <el-checkbox v-model="checkedZJ" @change="changeZJ">主觉验光</el-checkbox>
        <el-table
          ref="tableZJ"
          :data="routineData.zJyanguang"
          style="width: 100%;margin-top: 32px"
          @selection-change="handleSelectionZJ"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column
            prop="examTime"
            label="时间"
            width="180"
          />
          <el-table-column
            prop="eyeType"
            label="眼别"
            width="180"
          />
          <el-table-column
            prop="odSph"
            label="球镜 DS"
          />
          <el-table-column
            prop="odCyl"
            label="柱镜 DC"
          />
          <el-table-column
            prop="odAx"
            label="轴位 Axis"
          />
          <el-table-column
            prop="odVa"
            label="矫正视力VA"
          />
        </el-table>
      </div>
    </div>
  </div>
</template>
<script>
import eventBus from '@/page-subspecialty/utils/eventBus'
import echartsYanya from '@/components/echarts/yanya'
const Base64 = require('js-base64').Base64
export default {
  components: {
    echartsYanya
  },
  mixins: [],
  props: {
    patientIdNumber: {
      type: String,
      default: ''
    },
    patientId: {
      type: String,
      default: ''
    },
    onlyRead: {
      type: Boolean,
      default: false
    },
    date: {
      type: String,
      default: ''
    }
  },
  inject: ['refresh'],
  data() {
    return {
      checkedSeJue: false,
      checkedShiLi: false,
      checkedYanYa: false,
      checkedDL: false,
      checkedZJ: false,
      options: [
        {
          value: '',
          label: '全部'
        },
        {
          value: '10',
          label: '正常'
        },
        {
          value: '20',
          label: '色盲'
        },
        {
          value: '30',
          label: '色弱'
        }
      ],
      routineData: {},
      osType: '',
      osStatus: '',
      odType: '',
      odStatus: '',
      selectionDL: [],
      selectionZJ: [],
      shiliLast: [],
      yanyaLast: []
    }
  },
  created() {

  },
  mounted() {
    this.getroutineDataData()
  },
  methods: {
    async getroutineDataData() {
      const { data: res } = await this.$http.get('/patient/view/getVaAndIopAndRefAndLast', {
        params: {
          patientId: this.patientId,
          date: ''
          // patientId: this.patientId 797064
        }
      })
      if (res.code === 0) {
        this.routineData = res.data
        this.osType = res.data.sejue.length ? res.data.sejue[0].eyeType : ''
        this.osStatus = res.data.sejue.length ? res.data.sejue[0].eyeStatus : ''
        this.odType = res.data.sejue.length ? res.data.sejue[1].eyeType : ''
        this.odStatus = res.data.sejue.length ? res.data.sejue[1].eyeStatus : ''
        this.shiliLast = res.data.shiliLast || []
        this.yanyaLast = res.data.yanyaLast || []
      } else {
        this.routineData = {}
        this.$message.error(res.msg)
      }
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        if (rowIndex % 2 === 0) {
          return {
            rowspan: 2,
            colspan: 1
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    },
    handleSelectionDL(val) {
      this.selectionDL = val
    },
    handleSelectionZJ(val) {
      this.selectionZJ = val
    },
    changeDL() {
      if (this.checkedDL) {
        const data = this.routineData.dNyanguang.slice(0, 2)
        if (data.length) {
          data.forEach(row => {
            this.$refs.tableDL.toggleRowSelection(row)
          })
        }
      } else {
        this.$refs.tableDL.clearSelection()
      }
    },
    changeZJ() {
      if (this.checkedZJ) {
        const data = this.routineData.zJyanguang.slice(0, 2)
        if (data.length) {
          data.forEach(row => {
            this.$refs.tableZJ.toggleRowSelection(row)
          })
        }
      } else {
        this.$refs.tableZJ.clearSelection()
      }
    },
    setResult(data) {
      switch (data) {
        case '10':
          return '正常'
        case '20':
          return '色盲'
        case '30':
          return '色弱'
      }
    },
    // 添加数据集
    addDataList() {
      let dataArray = []
      if (this.checkedSeJue) {
        const data = [{
          name: '',
          odValue: '',
          osValue: ''
        }]
        data[0].name = '色觉检查'
        data[0].odValue = this.osType === 'OD' ? this.setResult(this.osStatus) : this.setResult(this.odStatus)
        data[0].osValue = this.osType === 'OS' ? this.setResult(this.osStatus) : this.setResult(this.odStatus)
        data[0].id = 'SJ001'
        dataArray = dataArray.concat(data)
      }
      if (this.checkedShiLi) {
        const data = [
          {
            name: '',
            odValue: '',
            osValue: ''
          },
          {
            name: '',
            odValue: '',
            osValue: ''
          }]
        const shili = this.shiliLast[0]
        data[0].name = '裸眼视力'
        data[0].odValue = shili.odVan
        data[0].osValue = shili.osVan
        data[0].id = 'LY001'
        data[1].name = '矫正视力'
        data[1].odValue = shili.odVag
        data[1].osValue = shili.osVag
        data[1].id = 'JZ001'
        dataArray = dataArray.concat(data)
      }
      if (this.checkedYanYa) {
        const data = [{
          name: '',
          odValue: '',
          osValue: ''
        }]
        const yanya = this.yanyaLast[0]
        data[0].name = '眼压'
        data[0].odValue = yanya.odIop
        data[0].osValue = yanya.osIop
        data[0].id = 'YY001'
        dataArray = dataArray.concat(data)
      }
      if (this.selectionDL) {
        console.log(this.selectionDL)
        this.selectionDL.forEach((item, index) => {
          item.name = '电脑验光'
          item.id = 'dl' + index
          item.sort = 5
        })
        dataArray = dataArray.concat(this.selectionDL)
      }
      if (this.selectionZJ) {
        this.selectionZJ.forEach((item, index) => {
          item.name = '主觉验光'
          item.id = 'zj' + index
          item.sort = 6
        })
        dataArray = dataArray.concat(this.selectionZJ)
      }
      eventBus.$emit('sendForData', dataArray)
    }
  }
}
</script>
<style lang="scss" scoped>
.routineBox{
  width: 100%;
  height: 100%;
  padding: 16px;
  box-sizing: border-box;
  overflow-y: auto;

  .header{
    width: 100%;
    display: flex;
    justify-content: space-between;
    height: 49px;
    line-height: 49px;
    padding: 0px 16px;
    box-sizing: border-box;
    margin-bottom: 32px;
    border: 1px solid rgba(0, 0, 0, 0.06);
  }
  .content{
    width: 100%;
    //display: flex;
    //justify-content: space-between;
    box-sizing: border-box;

  }
  .lineBox{
    //width: 48%;
    width: 100%;
    height: 260px;
    padding: 16px;
    box-sizing: border-box;
    border: 1px solid rgba(0, 0, 0, 0.06);
    margin-bottom: 32px;
  }
  .scroll-echearts {
    padding: 5px 0;
    flex: 1;
    margin-bottom: 16px;
    //background: #171515;
    border-radius: 4px;
  }
  .eyeBox{
    width: 480px;
    height: 180px;
    display: flex;
    margin-left: 32px;
    border: 1px solid rgba(0, 0, 0, 0.06);
    .eye{
      width: 50%;
      display: flex;
      flex-direction: column;

      div{
        height: 45px;
        line-height: 45px;
        text-align: center;
      }
      .info{
        display: flex;
        justify-content: space-between;
        padding: 0 14px;
        border-top: 1px solid rgba(0, 0, 0, 0.06);
      }
      .eye_w{
        display: inline-block;
        width: 80px;
        text-align: center;
      }
    }
    .el{
      background: rgba(0, 0, 0, 0.04);
    }
  }
  .mr30{
    margin-right: 30px;
  }
}
</style>
<style lang="scss">
.routineBox{
  .el-radio {
    margin-right: 15px;
  }
}
</style>
