<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.name" 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="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="name" label="医院名称" show-overflow-tooltip></el-table-column>
        <el-table-column :resizable="false" prop="connect" label="术语关联">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.connect"
              @change="handleStatusChange(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#4BC4D7"
              inactive-color="#BBBBBB"
            ></el-switch>
            <span class="titlwSwitchStatus">{{scope.row.connect === 1 ? '开' : '关'}}</span>
          </template>
        </el-table-column>
        <el-table-column :resizable="false" prop="diseaseNum" label="诊断关联">
          <template slot-scope="scope">
            <span
              class="relation"
              @click="goRelationPage('Disease',scope.row)"
            >{{scope.row.diseaseNum}}</span>
          </template>
        </el-table-column>
        <el-table-column :resizable="false" prop="lisNum" label="检验关联">
          <template slot-scope="scope">
            <span class="relation" @click="goRelationPage('Lis',scope.row)">{{scope.row.lisNum}}</span>
          </template>
        </el-table-column>
        <el-table-column :resizable="false" prop="pacsNum" label="检查关联">
          <template slot-scope="scope">
            <span class="relation" @click="goRelationPage('Pacs',scope.row)">{{scope.row.pacsNum}}</span>
          </template>
        </el-table-column>
        <el-table-column :resizable="false" prop="drugNum" label="药品关联">
          <template slot-scope="scope">
            <span
              class="relation"
              @click="goRelationPage('DrugManage',scope.row)"
            >{{scope.row.drugNum}}</span>
          </template>
        </el-table-column>
        <el-table-column :resizable="false" prop="operationNum" label="手术/操作关联">
          <template slot-scope="scope">
            <span
              class="relation"
              @click="goRelationPage('Operation',scope.row)"
            >{{scope.row.operationNum}}</span>
          </template>
        </el-table-column>
        <el-table-column :resizable="false" prop="transfusionNum" label="输血关联">
          <template slot-scope="scope">
            <span
              class="relation"
              @click="goRelationPage('Fusion',scope.row)"
            >{{scope.row.transfusionNum}}</span>
          </template>
        </el-table-column>
        <el-table-column :resizable="false" prop="deptNum" label="科室关联">
          <template slot-scope="scope">
            <span
              class="relation"
              @click="goRelationPage('DeptManage',scope.row)"
            >{{scope.row.deptNum}}</span>
          </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/cdss.js';
import config from '@api/config.js';
import utils from '@api/utils.js';

export default {
  name: 'Plan',
  data: function() {
    return {
      list: [],
      searched: false,
      filter: {
        name: '' //标准诊断名称
      },
      currentPage: 1,
      pageSize: config.pageSize,
      pageSizeArr: config.pageSizeArr,
      pageLayout: config.pageLayout,
      total: 0
    };
  },
  created() {
    const that = this;
    //返回时避免参数未赋值就获取列表
    setTimeout(function() {
      that.getDataList();
    });
    // 非首页 编辑页返回 设置 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: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = utils.getCurrentPage(
        this.currentPage,
        this.total,
        this.pageSize
      );
      this.getDataList();
    },

    // 获取列表数据
    async getDataList(isTurnPage) {
      let params = await 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.getMappingListPageCDSS(params).then(res => {
        loading.close();
        if (res.data.code === '0') {
          this.list = res.data.data && res.data.data.records;
        }
        this.total = res.data.data && res.data.data.total;
        if (this.inCurrentPage !== undefined) {
          this.currentPage = this.inCurrentPage;
          this.inCurrentPage = undefined;
        }
      });
    },

    // 处理列表请求数据参数
    async getFilterItems(isTurnPage) {
      //翻页时筛选条件没点确定则清空
      if (isTurnPage && !this.searched) {
        this.clearFilter();
      }
      const param = {
        current: this.inCurrentPage || this.currentPage,
        size: this.pageSize,
        name: this.filter.name.trim()
      };
      return param;
    },

    filterDatas() {
      this.currentPage = 1;
      this.getDataList();
    },

    // 页面跳转
    goRelationPage(from, row) {
      const item = Object.assign({}, row);
      // const pam = this.searched
      //   ? {
      //       currentPage: this.currentPage,
      //       pageSize: this.pageSize,
      //       filter: this.filter
      //     }
      //   : { currentPage: this.currentPage, pageSize: this.pageSize };
      this.$router.push({
        name: from,
        // params: Object.assign(pam, { from, data: item })
        params: { data: { ...item } }
      });
    },

    currentChange(next) {
      this.currentPage = next;
      this.getDataList(true);
    },
    // 清空搜索参数
    clearFilter() {
      this.filter = {
        name: ''
      };
    },

    indexMethod(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },

    // 医院关联处理
    handleStatusChange(row) {
      console.log(row, '=状态');
      let connectInfo = row.connect == 1 ? '开启' : '关闭';
      this.$alert(`确定要${connectInfo}${row.name}的术语关联吗？`, '提示', {
        confirmButtonText: '确定',
        // cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          api
            .modifyConnectStatusCDSS({
              connect: row.connect,
              id: row.hospitalId
            })
            .then(res => {
              if (res.data.code === '0') {
                this.getDataList();
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                });
              }
            });
        })
        .catch(() => {
          this.getDataList();
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    }
  }
};
</script>


<style lang="less" scoped>
@import '../../less/admin.less';
.delete {
  color: red;
}
.delete:hover {
  color: red;
}
.pagination {
  min-width: 1010px;
}
.disable {
  border-color: transparent;
}
.el-message-box {
  /deep/.cancelBtn {
    background-color: #d7d7d7;
    border-color: transparent;
  }
  /deep/ .delBtn {
    background-color: #ff545b !important;
    border-color: transparent !important;
  }
  /deep/ .confirmBtn2 {
    position: relative;
    right: 0px !important;
  }
}
.exportBoxL {
  /deep/ .el-message-box__btns {
    margin-top: 20px;
  }
  /deep/ .el-message-box__message {
    text-align: left;
  }
  /deep/ .el-message-box__btns {
    // text-align: center;
    margin-bottom: 24px;
  }
  /deep/ .leftbtn {
    margin-right: 46px;
    background-color: #d7d7d7;
    border-color: transparent;
  }
  /deep/ .cancelSureL {
    // text-align: center;
    display: none;
  }
  /deep/ .sureL {
    float: right;
  }
}
.contents {
  .is-plain {
    color: #dad7d7;
  }
  .is-plain:hover {
    color: #dad7d7;
  }
}

.el-table__row {
  /deep/ .is-disabled {
    border-color: transparent !important;
  }
}
.relation {
  color: #009dd9;
  cursor: pointer;
}
.titlwSwitchStatus {
  margin-left: 16px;
}
</style>