<template>
  <div>
    <crumbs title="术语集合内容维护" :minWidth="titleWidth" class="knowledgeTitle">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="术语集合类型：">
          <el-select v-model="filter.collectionLibType" clearable placeholder="请选择" size="mini">
            <el-option
              v-for="item in ruleTypeList"
              :key="item.val"
              :label="item.name"
              :value="item.val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="术语集合名称：">
          <el-input size="mini" v-model="filter.collectionLibName" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="基础术语名称：">
          <el-input size="mini" v-model="filter.conceptLibName" placeholder="请输入" clearable></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" class="demo-form-inline secLine">
        <el-form-item label="说明：">
          <el-input size="mini" v-model="filter.collectionRemark" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" @click="filterDatas">确认</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" @click="addRule" type="warning" style="margin:0 10px">+ 关联术语</el-button>
        </el-form-item>
      </el-form>
    </crumbs>
    <div class="contents knowledgeContents">
      <el-table :data="list" border style="width: 100%">
        <el-table-column type="index" :index="indexMethod" label="编号" width="60"></el-table-column>
        <el-table-column prop="collectionLibName" label="术语集合名称"></el-table-column>
        <el-table-column prop="conceptLibName" label="术语集合类型">
          <template slot-scope="scope">
            <span
              v-for="item in ruleTypeList"
            >{{item.val==scope.row.collectionLibType?item.name:''}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="collectionRemark" label="说明">
          <template slot-scope="scope">
            <el-tooltip
              v-if="scope.row.collectionRemark && scope.row.collectionRemark.length>80"
              class="item"
              effect="dark"
              :content="scope.row.collectionRemark"
              placement="top"
            >
              <span>{{scope.row.collectionRemark.slice(0,80)+'...'}}</span>
            </el-tooltip>
            <span v-if="scope.row.collectionRemark && scope.row.collectionRemark.length<81">{{scope.row.collectionRemark}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="concatConceptLibName" label="基础术语名称" width="500">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              :content="scope.row.concatConceptLibName"
              placement="top"
            >
              <span
                style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"
              >{{scope.row.concatConceptLibName}}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="modifier" label="操作人"></el-table-column>
        <el-table-column prop="gmtModified" label="操作时间"></el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="editData(scope.row.collectionId)">修改</el-button>
            <span style="margin:0 3px;">|</span>
            <el-button
              type="text"
              size="small"
              class="delete"
              @click="showDelDialog(scope.row,1)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <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>
</template>

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

export default {
  name: 'TermSet',
  data: function() {
    return {
      list: [],
      ruleTypeList: [],
      searched: false,
      filter: {
        collectionRemark: '',
        collectionLibName: '',
        conceptLibName: '',
        collectionLibType: '0'
      },
      cacheData: {},
      currentPage: 1,
      pageSize: config.pageSize,
      pageSizeArr: config.pageSizeArr,
      pageLayout: config.pageLayout,
      total: 0,
      titleWidth: '970px' //头部最小宽度
    };
  },
  created() {
    const param = this.$route.params;
    if (param.currentPage && param.filter) {
      this.inCurrentPage = param.currentPage;
      this.filter = param.filter;
    }
    const that = this;
    //返回时避免参数未赋值就获取列表
    setTimeout(function() {
      that.getDataList();
    });
    this.getDict();
  },
  watch: {
    filter: {
      handler: function() {
        this.searched = false;
      },
      deep: true
    }
  },
  methods: {
    getDict() {
      api
        .zskgetDict()
        .then(res => {
          if (res.data.code == '0') {
            const data = res.data.data;
            this.ruleTypeList = data[62];
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = utils.getCurrentPage(
        this.currentPage,
        this.total,
        this.pageSize
      );
      this.getDataList();
    },
    addRule() {
      const pam = this.searched
        ? {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            filter: this.filter
          }
        : { currentPage: this.currentPage, pageSize: this.pageSize };
      this.$router.push({ name: 'AddTermSet', params: pam });
    },
    filterDatas() {
      this.currentPage = 1;
      this.getDataList(1);
    },
    getDataList(flag, isTurnPage) {
      const params = 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
        .getConceptPage(params)
        .then(res => {
          loading.close();
          if (res.data.code == '0') {
            const data = res.data.data;
            for (let j = 0; j < data.records.length; j++) {
              data.records[j].condition =
                data.records[j].parStatus == '1' ? '启用' : '禁用';
            }
            this.list = data.records;
            if (!flag) {
              //搜索时不缓存
              this.cacheData[params.current] = data.records;
            } else {
              this.cacheData = {};
            }
            this.total = data.total;
            if (this.inCurrentPage !== undefined) {
              this.currentPage = this.inCurrentPage;
              this.inCurrentPage = undefined;
            }
          } else {
            this.warning(res.data.msg || '获取列表数据失败');
          }
        })
        .catch(error => {
          loading.close();
          console.log(error);
        });
    },
    getFilterItems(isTurnPage) {
      //翻页时筛选条件没点确定则清空
      if (isTurnPage && !this.searched) {
        this.clearFilter();
      }
      const param = {
        current: this.inCurrentPage || this.currentPage,
        size: this.pageSize,
        ...this.filter
      };
      return param;
    },
    indexMethod(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
    currentChange(next) {
      this.currentPage = next;
      /*if (this.cacheData[next]) {       //如果已请求过该页数据，则使用缓存不重复请求
                  this.list = this.cacheData[next];
                } else {*/
      this.getDataList(1, true);
      //}
    },
    warning(msg, type) {
      this.$message({
        showClose: true,
        message: msg,
        type: type || 'warning'
      });
    },
    showConfirmDialog(msg, resolve) {
      this.$alert(msg, '提示', {
        confirmButtonText: '确定',
        type: 'warning'
      })
        .then(() => {
          resolve();
        })
        .catch(() => {});
    },
    editData(id) {
      const pam = this.searched
        ? {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            filter: this.filter
          }
        : { currentPage: this.currentPage, pageSize: this.pageSize };
      api
        .getRecordById({ id })
        .then(res => {
          if (res.data.code == '0') {
            const data = res.data.data;
            this.$router.push({
              name: 'AddTermSet',
              params: { ...pam, data }
            });
          }
        })
        .catch(error => {
          this.warning('获取详情失败，请重试');
        });
    },
    showDelDialog(row) {
      const param = {
        id: row.collectionId
      };
      let waringTxt = '是否删除该集合内容，可能对现有系统造成影响';
      this.showConfirmDialog(waringTxt, () => {
        api
          .deleteById(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.warning(res.data.msg || '操作成功', 'success');
              this.getDataList();
            } else {
              this.warning(res.data.msg);
            }
          })
          .catch(error => {
            this.warning(error);
          });
      });
    },
    clearFilter() {
      this.filter = {
        collectionRemark: '',
        conceptLibName: '',
        collectionLibName: '',
        collectionLibType: ''
      };
    }
  }
};
</script>

<style lang="less" scoped>
@import '../../less/admin.less';
/deep/ .container.knowledgeTitle {
  height: 80px;
}
/deep/ .contents.knowledgeContents {
  padding: 104px 20px 0;
}
// /deep/ .el-form--inline .el-form-item{
//   height: 40px;
// }
/deep/ .secLine {
  float: right;
  display: flex;
  position: relative;
  top: -10px;
  .el-input__inner {
    width: 380px;
  }
}
.delete {
  color: red;
}
.review {
  color: #22ccc8;
}
.el-table .cell {
  overflow: hidden;
  white-space: nowrap;
}
#upFile {
  display: none !important;
}
.unvailable {
  color: #fe7d3d;
  &:hover {
    color: #f19061;
  }
}
</style>