<template>
    <div>
        <crumbs title="医学术语关联维护" style="min-width: 980px">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item label="标准术语名称：">
                    <el-input size="mini" v-model="filter.conceptName" placeholder="术语名称" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="mini" @click="filterDatas">确认</el-button>
                    <el-button size="mini" type="warning" @click="addRelation">添加关联</el-button>
                </el-form-item>
            </el-form>
        </crumbs>
        <div class="tab">
          <span v-for="it in typeList" 
                :key="it.key" 
                :class="{'curr':it.key==filter.type}"
                @click="toggTab(it)">
            {{it.name}}
          </span>
        </div>
        <div class="contents">
            <el-table
                :data="list"
                border
                style="width: 100%">
                <el-table-column
                    :resizable = "false"
                    type="index"
                    :index = 'indexMethod'
                    label="编号"
                    width="60">
                </el-table-column>
                <el-table-column
                    :resizable = "false"
                    prop="operTime"
                    label="操作时间"
                    width="180">
                </el-table-column>
                <el-table-column
                    :resizable = "false"
                    prop="libNameType"
                    label="医学标准术语"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    :resizable = "false"
                    prop="otherNames"
                    label="关联术语"
                    show-overflow-tooltip>
                </el-table-column>
                <!-- <el-table-column
                  label="状态">
                  <template slot-scope="scope">
                    <span :class="scope.row.isDeleted == 'N'?'':'delete'">
                      {{scope.row.isDeleted == 'N'?'启用中':'已删除'}}
                    </span>
                  </template>
                </el-table-column> -->
                <el-table-column
                    :resizable = "false"
                    prop="operName"
                    label="操作人">
                </el-table-column>
                <el-table-column
                    label="操作" 
                    width="160"
                    :resizable = "false">
                    <template slot-scope="scope">
                      <el-button type="text" size="small" :disabled="scope.row.isDeleted != 'N'" @click="modifyRelation(scope.row)">修改</el-button>
                      <span style="margin:0 3px;">|</span>
                      <el-button type="text" size="small" :class="scope.row.isDeleted == 'N'?'delete':'review'" @click="showDelDialog(scope.row)">{{scope.row.isDeleted == 'N'?'删除':'恢复'}}</el-button>
                    </template>
                </el-table-column>
            </el-table>
        <div class="pagination">
            <el-pagination :current-page.sync="currentPage"
               @current-change="currentChange"
               background
               :page-size="pageSize"
               :page-sizes="pageSizeArr"
               @size-change="handleSizeChange"
               :layout="pageLayout"
               :total="total">
            </el-pagination>
        </div>
        </div>
    </div>
</template>

<script>
import api from '@api/icss.js';
import config from '@api/config.js';
import utils from '@api/utils.js';

export default {
    name: 'ConceptRelation',   //诊断量表关联维护
    data: function() {
        return {
          list: [],
          searched: false,
          filter:{
            conceptName:'',
            type:'' 
          },
          currentPage: 1,
          pageSize: config.pageSize,
          pageSizeArr:config.pageSizeArr,
          pageLayout:config.pageLayout,
          total: 0,
          typeList:[]
        }
    },
    created() {
      let typeList = JSON.parse(localStorage.getItem("knowledgeEnumsData"));
      this.typeList = typeList.relationModelTypeEnum;
      this.filter.type = this.typeList[0].key;//默认展示科室，请置于第一位
      const that = this;
      //返回时避免参数未赋值就获取列表
      setTimeout(function(){
        that.getDataList();
      });
    },
  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: {
      handleSizeChange(val){
        this.pageSize = val;
        this.currentPage = utils.getCurrentPage(this.currentPage, this.total, this.pageSize);
        this.getDataList();
      },
        toggTab(item){
          // bug2348 搜索后切换tab时清空搜索条件
          this.filter.conceptName = '';
          this.filter.type = item.key;
          this.currentPage = 1;
          this.getDataList();
        },
        getDataList(isTurnPage) {
            const param = this.getFilterItems(isTurnPage);
            this.searched = true;
            const loading = this.$loading({
                lock: true,
                text: 'Loading',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            api.getConceptRelation(param).then((res) => {
              loading.close()
              if(res.data.code == '0') {
                this.list = res.data.data.records
                this.total = res.data.data.total;
                if(this.inCurrentPage!==undefined){
                  this.currentPage=this.inCurrentPage;
                  this.inCurrentPage = undefined;
                }
              }
            })
        },
        filterDatas() {
            this.currentPage = 1;
            this.getDataList();
        },
        addRelation() {
          const pam = this.searched ? {
            currentPage: this.currentPage,
            pageSize:this.pageSize,
            filter: this.filter
          } : {currentPage: this.currentPage,
            pageSize:this.pageSize,filter:Object.assign(this.filter,{conceptName:''})};
          this.$router.push({name:'AddConceptRelation',params:pam})
        },
        modifyRelation(row) {
          const typeL = this.filter.type.toString().split('101');
          const typeId = [];
          typeId[0] = +typeL[1];
            const param = {
              "conceptId": row.conceptId,
              // "relationId":17
              "relationIds":[17],//9-20新增
              "typeIds":typeId
            }
          const pam = this.searched ? {
            currentPage: this.currentPage,
            pageSize:this.pageSize,
            filter: this.filter
          } : {currentPage: this.currentPage,
            pageSize:this.pageSize,filter:Object.assign(this.filter,{conceptName:''})};
            api.getConceptRelationDet(param).then((res) => {
              if(res.data.code=='0'){
                const { data } = res.data;
                this.$router.push({name:'AddConceptRelation',params:Object.assign(pam, {isEdit: true, data: data})});
              } else {
                this.warning(res.data.msg);
              }
            }).catch((error)=>{
                this.warning(error)
            })
            
        },
        currentChange(next) {
            this.currentPage = next;
            this.getDataList(true);
        },
        getFilterItems(isTurnPage) {
          //翻页时筛选条件没点确定则清空
          if(isTurnPage&&!this.searched){
            this.filter.conceptName='';
          };
            const param = {
                current: this.inCurrentPage||this.currentPage,
                size: this.pageSize,
                name:this.filter.conceptName.trim(),
                relationId:17,
                relationModelTypeCode:this.filter.type
            };
            return param;
        },
        indexMethod(index) {
            return ((this.currentPage - 1) * this.pageSize) + index + 1;
        },
        getTagType(val) {
            return val
        },
        warning(msg,type){
          this.$message({
            showClose: true,
            message:msg,
            type:type||'warning'
          })
        },
        showConfirmDialog(msg,resolve){
          this.$alert(msg, '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          }).then(() => {
            resolve();
          }).catch(() => {});
        },
        showDelDialog(row){
          this.showConfirmDialog('是否删除该关联？',()=>{
            const typeL = this.filter.type.toString().split('101');
            const typeId = [];
            typeId[0] = +typeL[1];
            const param = {
              "conceptId": row.conceptId,
              // "isDeleted": row.isDeleted=== 'N'?'Y':'N',
              "relationIds":[17],//9-20新增
              "typeIds":typeId
            }
            api.delConceptRelation(param).then((res)=>{
              if(res.data.code=='0'){
                if(!this.searched){
                  //未点确认时清空搜索条件
                  this.filter.conceptName='';
                }
                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";
    .tab{
      background:#fff;
      padding: 5px 0;
      position: relative;
      top:40px;
      span{
        display: inline-block;
        // width:145px;
        padding: 0 20px;
        height: 45px;
        line-height: 45px;
        text-align: center;
        // border: 1px solid #ccc;
        margin-right: 5px;
        cursor: pointer;
      }
      .curr{
        color: #48C5D7;
        &:after{
          content: '';
          width:50px;
          height: 2px;
          background:#48C5D7;
          display: block;
          margin: 0 auto;
        }
      }
    }
    .delete{
       color: red;
    }
    .delete:hover {
        color: red;
    } 
    .pagination {
        min-width: 1010px;
    }
</style>
