<template>
    <div>
        <crumbs title="医学术语类型维护">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item label="医学类型：">
                    <el-input size="mini" v-model="name" placeholder="输入医学类型"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="mini" @click="filterDatas">确认</el-button>
                    <el-button size="mini" type="warning" @click="addType">添加医学类型</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
                        prop="gmtModified"
                        label="操作时间"
                        :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="医学类型"
                        show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                        prop="modifier"
                        label="操作人">
                </el-table-column>
            </el-table>
            <el-pagination v-if="total>pageSize"
                           :current-page.sync="currentPage"
                           @current-change="currentChange"
                           background
                           :page-size="pageSize"
                           layout="total,prev, pager, next, jumper"
                           :total="total">
            </el-pagination>
        </div>

    </div>
</template>

<script>
  import api from '@api/icss.js';
  export default {
    name: 'MedicalType',
    data: function () {
      return {
        list: [],
        cacheData: {},
        currentPage: 1,
        pageSize: 10,
        total: 0,
        name:''
      }
    },
    created() {
      this.getDataList();
    },
    methods: {
      filterDatas(){
        this.currentPage = 1;
        this.getDataList();
      },
      getDataList() {
        const param = this.getFilterItems();
        api.getTypeList(param).then((res) => {
          if (res.data.code == '0') {
            const data = res.data.data;
            this.list = data.records;
            this.cacheData[param.current] = data.records;
            this.total = data.total;
          }
        }).catch((error) => {
          console.log(error);
        });
      },
      getFilterItems() {
        const param = {
          name: this.name,
          current: this.currentPage,
          size: this.pageSize
        };
        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();
        }
      },
      addType(){
        this.$prompt('添加：','医学类型添加',{
          confirmButtonText: '确定',
          showCancelButton: false,
          inputPattern:/^[\u4e00-\u9fa5]+$/,
          inputErrorMessage: '医学类型只能输入汉字',
          inputValidator:(value)=>{
            if(value.trim().length>30){
              return '最多可输入30个字'
            }
          }
        }).then(({value})=>{
          if(value.trim()){
            api.addType({name:value.trim()}).then((res)=>{
              if(res.data.code==0){
                this.$message({
                  message:res.data.msg||'添加成功',
                  type:'success'
                })
                this.getDataList();
              }else{
                this.$message({
                  message:res.data.msg || '添加失败',
                  type:'warning'
                })
              }
            }).catch((error)=>{
              console.log(error);
            })
          }
        }).catch(()=>{

        })
      }
    }
  }
</script>

<style lang="less" scoped>
    @import "../../less/admin.less";
    .el-table .cell{
      overflow: hidden;
      white-space: nowrap;
    }
</style>