<template>
  <div>
    <crumbs title="知识图谱标准术语维护" style="min-width: 1020px">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="术语分类：" class="selectMedicine">
          <el-select size="mini" v-model="filter.labelType" placeholder="请选择" clearable>
            <el-option
              v-for="item in terminologyData"
              :label="item.name"
              :value="item.val"
              :key="item.orderNo"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="术语名称：">
          <el-input size="mini" maxlength="50" v-model="filter.name" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="静态知识标识：">
          <el-select
            size="mini"
            v-model="filter.is_kl"
            placeholder="请选择"
            clearable
            style="width: 100%"
          >
            <el-option label="有" :value="1"></el-option>
            <el-option label="无" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="状态：" class="selectMedicine">
          <el-select size="mini" v-model="filter.status" placeholder="请选择" clearable>
            <el-option v-for="item in stateList" :label="item.name" :value="item.id" :key="item.id"></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item class="dododo">
          <el-button size="mini" @click="filterDatas">检索</el-button>
          <el-button size="mini" type="warning" style="margin:0 10px" @click="addKnowledge">添加标准术语</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="80"></el-table-column>
        <el-table-column prop="labelType" label="术语分类">
          <template slot-scope="scope">{{ labelTypeRender( scope.row.labelType)}}</template>
        </el-table-column>
        <el-table-column prop="name" label="术语名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="pycode" label="术语拼音" width="240" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="is_kl" label="静态知识标识" width="120" :show-overflow-tooltip="true">
          <template slot-scope="scope">{{ is_klRender( scope.row.is_kl)}}</template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="toEditKnowledge(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        @current-change="currentChange"
        background
        :page-size="pageSize"
        class="pagepage pagepage"
        :page-sizes="pageSizeArr"
        @size-change="handleSizeChange"
        :layout="pageLayout"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

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

export default {
  name: 'KnowledgeManage',
  data: function() {
    return {
      list: [],
      currentPage: 1,
      pageSize: config.pageSize,
      pageSizeArr: config.pageSizeArr,
      pageLayout: config.pageLayout,
      total: 0,
      terminologyData: [],
      searched: false,
      filter: {
        name: '',
        status: '',
        labelType: '',
        is_kl: ''
      }
    };
  },
  created() {
    const that = this;
    //返回时避免参数未赋值就获取列表
    setTimeout(function() {
      that.getDataList();
      that._getTerminologyData();
    });
    // 非首页 编辑页返回 设置 this.currentPage
    if (Object.keys(this.$route.params).length !== 0) {
      this.currentPage = this.$route.params.currentPage;
    }
  },
  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: {
    _getTerminologyData() {
      api.getListDicCDSS().then(res => {
        if (res.data.code === '0') {
          this.terminologyData = res.data.data && res.data.data['8'];
        }
      });
    },

    is_klRender(is_kl) {
      if (is_kl === 0) {
        return '无';
      } else if (is_kl === 1) {
        return '有';
      } else {
        return null;
      }
    },
    labelTypeRender(labelType) {
      let term = this.terminologyData.find(item => item.val === labelType);
      return term.name;
    },

    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = utils.getCurrentPage(
        this.currentPage,
        this.total,
        this.pageSize
      );
      this.getDataList();
    },
    addKnowledge() {
      const pam = this.searched
        ? {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            filter: this.filter
          }
        : { currentPage: this.currentPage, pageSize: this.pageSize };
      this.$router.push({ name: 'AddKnowledge', params: pam });
    },
    toEditKnowledge(row) {
      const pam = this.searched
        ? {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            filter: this.filter
          }
        : { currentPage: this.currentPage, pageSize: this.pageSize };
      this.$router.push({
        name: 'AddKnowledge',
        params: Object.assign(pam, { data: row, isEdit: true })
      });
    },
    filterDatas() {
      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
        .getEntityPageCDSS(param)
        .then(res => {
          loading.close();
          if (res.data.code == '0') {
            this.list = res.data.data && res.data.data.content;
            this.total = res.data.data && res.data.data.totalElements;
            if (this.inCurrentPage !== undefined) {
              this.currentPage = this.inCurrentPage;
              this.inCurrentPage = undefined;
            }
          }
        })
        .catch(error => {
          if (error.code === '900010001') {
            return false;
          }
          console.log(error);
        });
    },
    clearFilter() {
      this.filter = {
        name: '',
        status: '',
        labelType: '',
        is_kl: ''
      };
    },
    getFilterItems(isTurnPage) {
      //翻页时筛选条件没点确定则清空
      if (isTurnPage && !this.searched) {
        this.clearFilter();
      }
      const param = {
        number: this.inCurrentPage - 1 || this.currentPage - 1,
        size: this.pageSize,
        name: this.filter.name.trim(),
        labelType: this.filter.labelType,
        is_kl: this.filter.is_kl,
        status: this.filter.status
      };
      return param;
    },
    indexMethod(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
    currentChange(next) {
      this.currentPage = next;
      this.getDataList(true);
    },
    warning(msg, type) {
      this.$message({
        showClose: true,
        message: msg,
        type: type || 'warning'
      });
    }
  }
};
</script>

<style lang="less" scoped>
@import '../../../less/admin.less';
.status-span {
  font-size: 12px;
  margin-right: 10px;
  color: unset;
}
.dododo {
  margin-top: 1px !important;
}
.pagepage {
  .el-input.el-input--mini.el-input--suffix input {
    height: 28px;
    line-height: 28px;
  }
  .el-pagination__sizes {
    margin-right: 20px !important;
  }
}
.el-message-box {
  /deep/ .cancelBtn {
    background-color: #d7d7d7;
    border-color: transparent;
  }
  /deep/ .delBtn {
    background-color: #ff545b !important;
    border-color: transparent !important;
  }
  /deep/ .confirmBtn1 {
    position: relative;
    right: 0px !important;
  }
}

.selectMedicine {
  // /deep/ .el-input__suffix-inner {
  //   position: relative;
  //   top: -1px;
  // }
  /deep/ .el-icon-circle-close {
    position: relative;
    // top: -2px;
    left: 12px;
  }
}

body {
  .el-tooltip__popper {
    max-width: 400px;
  }
}
</style>
