<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb :crumbs='crumbs'></bread-crumb>
    </div>
    <div class="app-container">
      <div class="cell clearfix">
        <el-input size="small" style="width:150px;" v-model="keywords"
                  class="filter-item"/>
        <el-button size="small" style="margin:0 10px;" class="filter-item" type="primary" @click="searchAction">搜索
        </el-button>
        <el-date-picker v-model="record_date" prefix-icon="el-icon-date" @change="handleScheduleDateChange"
                        :editable="false" :clearable="false" style="width: 196px;margin-right:10px;" type="date"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期时间" align="right"></el-date-picker>
      </div>
      <el-table :data="tableData" border ref="table" style="width: 100%;" :row-style="{ color: '#303133' }"
                :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}" highlight-current-row>
        <el-table-column align="center" label="序号" width="60" type="index"></el-table-column>
        <el-table-column align="center" prop="name" label="姓名" width="100">
          <template slot-scope="scope">{{scope.row.patients?scope.row.patients.name:''}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="处方日期" width="200">
          <template slot-scope="scope"> {{scope.row.ctime?getTimes(scope.row.ctime):''}}</template>

        </el-table-column>
        <!--<el-table-column align="center" prop="name" label="患者类型">-->
          <!--<template slot-scope="scope">{{}}</template>-->
        <!--</el-table-column>-->
        <el-table-column align="center" prop="name" label="处方号">
          <template slot-scope="scope">{{scope.row.prescription_number?scope.row.prescription_number:''}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="开立医生">
          <template slot-scope="scope">{{scope.row.doctor?scope.row.doctor:''}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="诊断">
          <template slot-scope="scope">{{getDiagnosis(scope.row.diagnosis)}}</template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="状态" width="100">
          <template slot-scope="scope">
            <div v-if="  scope.row.id > 0">已就诊</div>
            <div v-else>未就诊</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="操作" width="100">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handerShowDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 50, 100]"
        :page-size="10"
        background
        style="margin-top:20px;float: right"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"

      >
      </el-pagination>
      <inquiries-detail ref="inquiriesDetail"></inquiries-detail>
    </div>
  </div>
</template>

<script>
  import BreadCrumb from '@/xt_pages/components/bread-crumb'
  import inquiriesDetail from './components/inquiriesDetail'
  import { getHisHospitalPrescriptionList } from '@/api/his/his'
  import { uParseTime } from '@/utils/tools'
  import { getInitData} from '@/api/his/his'
  export default {
    components: {
      BreadCrumb,
      inquiriesDetail
    },
    data() {
      return {
        keywords:"",
        total:0,
        record_date:"",
        page:1,
        limit:10,
        crumbs: [
          { path: false, name: '住院工作站' },
          { path: false, name: '既往查询' }
        ],
        tableData: [],
        diagnoses:[]
      }
    },
    methods: {
      searchAction(){
        this.page = 1;
        this.limit = 10;
        this.getHisPrescriptionList()

      },
      handleScheduleDateChange(){
        this.page = 1;
        this.limit = 10;
        this.getHisPrescriptionList()

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

      },

      handleSizeChange(limit) {
        this.limit = limit;
        this.getHisPrescriptionList()


      },
      getTimes(time) {
        return uParseTime(time, "{y}-{m}-{d} {h}:{i}:{s}");
      },
      handerShowDetail(row) {
        if(row.id == 0){
          this.$message.error("未就诊");
          return
        }
        this.$refs.inquiriesDetail.show(row.id)
      }, getHisPrescriptionList() {
        let params = {
          record_date:this.record_date,
          page: this.page,
          limit: this.limit,
          keywords: this.keywords,
        };
        getHisHospitalPrescriptionList(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false
          } else {
            this.tableData = response.data.data.order;
            this.total = response.data.data.total;
            this.$nextTick(() => {
              this.$refs.table.doLayout(); //解决表格错位
            });
          }
        })

      },
       getInitData() {
        getInitData().then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false
          } else {
            this.diagnoses = response.data.data.diagnose;
            console.log("诊断列表",this.diagnoses)
          }
        })
      },
      getDiagnosis(id){
        let ids = id.split(",")
        var name = ""
        for(let i = 0; i < ids.length; i++){

          for(let b=0;b<this.diagnoses.length;b++){
            if(parseInt(ids[i]) == this.diagnoses[b].id){
              if(name.length == 0){
                name = this.diagnoses[b].class_name
              }else{
                name = name +"," + this.diagnoses[b].class_name

              }
            }
          }
        }
        return name
      }
    }, created() {
      var nowDate = new Date();
      var nowYear = nowDate.getFullYear();
      var nowMonth = nowDate.getMonth() + 1;
      var nowDay = nowDate.getDate();
      this.record_date =
        nowYear +
        '-' +
        (nowMonth < 10 ? '0' + nowMonth : nowMonth) +
        '-' +
        (nowDay < 10 ? '0' + nowDay : nowDay);


      this.getHisPrescriptionList();
      this.getInitData()

    }
  }
</script>
