<template>
    <div class="standardRecordWrapper">
        <crumbs title="标准病历维护">
            <el-form :inline="true" class="demo-form-inline" style="max-width: 870px;text-align: right;">
                <el-form-item label="所属医院：">
                    <el-select size="mini" v-model="filter.hospitalId" placeholder="所属医院" clearable>
                        <el-option v-for="item in hisTypes" :label="item.name" :value="item.id" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
              <el-form-item label="病人住院序号：">
                    <el-input size="mini" v-model.trim="filter.behospitalCode" placeholder="病人住院序号" clearable></el-input>
                </el-form-item>
                 <el-form-item label="病人姓名：">
                    <el-input size="mini" v-model.trim="filter.name" placeholder="病人姓名" clearable></el-input>
                </el-form-item>
                <el-form-item label="科室：">
                     <el-select size="mini" v-model.trim="filter.deptId" @change="getValue"  placeholder="请选择科室" clearable>
                        <el-option v-for="item in moduleList" :label="item.deptName" :value="Number(item.deptId)" :key="Number(item.deptId)" ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出院日期：">
                    <el-date-picker
                      v-model="filter.date1"
                      type="date"
                      placeholder="选择日期">
                    </el-date-picker>
                    <span>-</span>
                    <el-date-picker
                      v-model="filter.date2"
                      type="date"
                      placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
               
                <el-form-item>
                    <el-button size="mini" @click="filterDatas">确认</el-button>
                    <el-button size="mini" type="warning" @click="addTemp">新增病历</el-button>
                </el-form-item>
            </el-form>
        </crumbs>
        <div class="contents">
            <el-table
                    :data="list"
                    border
                    style="width: 100%">
                <el-table-column
                    type="index"
                    :index="indexMethod"
                    label="编号"
                    width="60">
                </el-table-column>
                <el-table-column
                        :resizable = "false"
                        label="所属医院"
                        prop="hospitalName"
                        >
                </el-table-column>
                <el-table-column
                        :resizable = "false"
                        prop="behospitalCode"
                        label="病人住院序号"
                        width="180">
                </el-table-column>
                <el-table-column
                        :resizable = "false"
                        prop="deptName"
                        label="科室">
                </el-table-column>
                
                <el-table-column
                        :resizable = "false"
                        prop="name"
                        label="病人姓名">
                </el-table-column>
                <el-table-column
                        :resizable = "false"
                        prop="sex"
                        label="性别">
                </el-table-column>
                <el-table-column
                        :resizable = "false"
                        prop="behospitalDate"
                        :formatter="dateFormatter"
                        label="入院日期">
                </el-table-column>
                <el-table-column
                        :resizable = "false"
                        prop="leaveHospitalDate"
                        :formatter="dateFormatter"
                        label="出院日期">
                </el-table-column>
                <el-table-column
                        :resizable = "false"
                        prop="gmtModified"
                        :formatter="dateFormatter"
                        label="加入时间">
                </el-table-column>
                <el-table-column
                        :resizable = "false"
                        prop="operate"
                        label="操作">
                    <template slot-scope="scope">
                        <el-button  @click="modifyStd(scope.row)" type="text" size="small">更新</el-button>
                        <span style="margin:0 3px;">|</span>
                        <el-button @click="showDelDialog(scope.row)" class="delete" type="text" size="small">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
          <el-pagination  :current-page.sync="currentPage"
                          @current-change="currentChange"
                          background
                          :page-size.sync="pageSize"
                          :page-sizes="pageSizeArr"
                           @size-change="handleSizeChange"
                          :layout="pageLayout"
                          :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    import api from '@api/qualityControl.js';
  import config from '@api/config.js';
  import utils from '@api/utils.js'
  export default {
    name: 'StandardRecord',
    data: function() {
      return {
        list: [],
        hisTypes:[],
        moduleList:[],
        searched: false,
        filter: {
          hospitalId:'',
          behospitalCode: '',
          deptId:'',
          name: '',
          date1:"",
          date2:"",
        },
        currentPage: 1,
        pageSize: config.pageSize,
        pageSizeArr:config.pageSizeArr,
        pageLayout:config.pageLayout,
        total: 0,
      }
    },
    created() {
      const _this=this;
      setTimeout(function(){
        _this.getAllTypes()
      },100);

    },
    watch: {
      'filter': {
        handler: function () {
          this.searched = false;
        },
        deep: true
      }
    },
    beforeRouteEnter(to, from, next) {
      next(vm => {
        //const pm = to.param;
        Object.assign(vm, to.params);
        vm.inCurrentPage=to.params.currentPage;
      })
    },
    methods: {
      dateFormatter(item,prop,it){
        return it.substr(0,10);
      },
      getAllTypes(){
        api.getRecordHopitalList().then((res)=>{
          if(res.data.code==="0"){
            const data = res.data.data;
            this.hisTypes =data;
          }else{
            this.warning("获取医院列表失败");
          }
        });
        api.getStdDeptList({}).then((res)=>{
            if(res.data.code==="0"){
                const data = res.data.data;
                this.moduleList=data;
            }else{
                this.warning("获取科室列表失败");
            }
        });
        this.getDataList();
      },
      handleSizeChange(val){
        this.pageSize = val;
        this.currentPage = utils.getCurrentPage(this.currentPage, this.total, this.pageSize);
        this.getDataList();
      },
      getValue(val) {
        console.log('changeVal', val, this.filter.tagAdscription)
      },
      getDataList(isTurnPage) {
        const param = this.getFilterItems(isTurnPage);
        const {leaveHosDateStart,leaveHosDateEnd}=param;
        if(leaveHosDateStart&&leaveHosDateEnd&&leaveHosDateStart>leaveHosDateEnd){
          this.warning('开始时间不能大于结束时间');
          return;
        }
        this.searched = true;
        const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        });
        api.getStdList(param).then((res) => {
          loading.close();
          if(res.data.code==="0"){
            let list = res.data.data.records;
            this.list = list;
            this.total = res.data.data.total;
            if(this.inCurrentPage!==undefined){
              this.currentPage=this.inCurrentPage;
              this.inCurrentPage = undefined;
            }
          }else{
            this.warning(res.data.msg||'操作失败');
          }
        })
      },
      filterDatas() {
        this.currentPage = 1;
        this.getDataList();
      },
      addTemp() {
        const pam = this.searched ? {
          currentPage: this.currentPage,
          pageSize:this.pageSize,
          filter: this.filter,
          hisTypes:this.hisTypes
        } : {currentPage: this.currentPage,pageSize:this.pageSize,hisTypes:this.hisTypes};
        this.$router.push({
          name: 'AddStandardRecord',
          params: pam
        })
      },
      modifyStd(row) {
        api.updateStdRecord({behospitalCode:row.behospitalCode,hospitalId:row.hospitalId}).then((res)=>{
          const {code,data,msg} = res.data;
          if(code=='0'){
            this.warning(res.data.msg || '更新成功','success');
            this.getDataList();
          }else{
            this.$message({
              message: msg,
              type: 'warning'
            });
          }
        });
        //this.$router.push({name:'AddIndeptLabel',params:{isEdit:true,data:row}});
      },
      currentChange(next) {
        this.currentPage = next;
        this.getDataList(true);
        // if (this.cacheData[next]) {       //如果已请求过该页数据，则使用缓存不重复请求
        //     this.list = this.cacheData[next];
        // } else {
        //     this.getDataList();
        // }
      },
      clearFilter(){
        this.filter={
          hospitalId:'',
          behospitalCode: '',
          deptId:'',
          name: '',
          date1:"",
          date2:"",
        };
      },
      getFilterItems(isTurnPage) {
        //翻页时筛选条件没点确定则清空
        if(isTurnPage&&!this.searched){
          this.clearFilter();
        };
        const {hospitalId,behospitalCode, deptId, name,date1,date2} = this.filter;
        let beCode=behospitalCode;
        var patrn = /[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、]/g;
        beCode=behospitalCode.replace(patrn,function(x){
          return "\\"+x;
        });
        const {tranformDate} = utils;
        const param = {
          current: this.inCurrentPage||this.currentPage,
          size: this.pageSize,
          hospitalId,
          deptId,
          name,
          behospitalCode:beCode,
          leaveHosDateEnd:tranformDate(date2,' 23:59:59'),
          leaveHosDateStart: tranformDate(date1," 00:00:00")
        };
        return param;
      },
      indexMethod(index) {
        return ((this.currentPage - 1) * this.pageSize) + index + 1;
      },
      getTagType(val) {
        return val
      },
      warning(msg,type){
        this.$message({
          showClose: true,
          dangerouslyUseHTMLString: true,
          message:'<p>'+msg+'</p>',
          type:type||'warning'
        })
      },
      showConfirmDialog(msg,resolve){
        this.$alert(msg, '提示', {
          confirmButtonText: '确定',
          type: 'warning'
        }).then(() => {
          resolve();
        }).catch(() => {});
      },
      showDelDialog(row){
        const param = {
          "behospitalCode": row.behospitalCode,
          "hospitalId":row.hospitalId
        }
        this.showConfirmDialog('是否删除该病历？',()=>{
          api.delStdRecord(param).then((res)=>{
            if(res.data.code=='0'){
              if(!this.searched){
                //未点确认时清空搜索条件
                this.clearFilter();
              }
              if(this.list.length==1){
                //当前在最后一页且只有一条数据时，删除后跳到前一页
                this.currentPage = this.currentPage===1?1:this.currentPage-1;
              }
              this.getDataList();
              this.warning(res.data.msg || '操作成功','success');
            }else{
              this.warning(res.data.msg);
            }
          }).catch((error)=>{
            this.warning(error);
          })
        });
      }
    }
  }
</script>


<style lang="less">
  @import "../../less/admin.less";
  .standardRecordWrapper{
    .el-date-editor.el-input{
      width: 130px;
      input{
        padding: 0 5px;
        width: 130px;
        height: 25px;
      }
    }
  }
  .delete{
      color: red
  }
  .delete:hover {
      color: red;
  }
  .el-select .el-input .el-icon-arrow-up{
      display: inline-block!important;
  }
  .el-select .el-input .el-icon-circle-close{
      float:left;
  }
  .el-pagination__sizes{
      margin-right: 20px;
    .el-input .el-input__inner{
        height: 28px;
        line-height: 28px;
    }
  }
  .standardRecordWrapper .container{
      height: 90px!important;
  }
  .standardRecordWrapper>.contents{
      padding:120px 20px 0;
  }
  .standardRecordWrapper .el-form-item{
      margin-bottom: 0;
  }
  .standardRecordWrapper .el-input__suffix{
      right: 22px;
  }
</style>