<template>
    <div>
        <crumbs title="查体模板维护">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item label="科室名称：">
                  <el-select v-model="filter.deptId" clearable placeholder="请选择" size="mini">
                    <el-option
                      v-for="item in deptList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button size="mini" @click="filterDatas">确认</el-button>
                    <el-button size="mini" type="warning" @click="addModule" style="margin:0 10px">添加模板</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="gmtOperate"
                        label="操作时间"
                        :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column
                        :resizable = "false"
                        prop="name"
                        label="科室名称">
                </el-table-column>
                <el-table-column
                        :resizable = "false"
                        prop="operatorName"
                        label="操作人"
                        width="180">
                </el-table-column>
                
                <el-table-column
                        :resizable = "false"
                        label="操作" width="200">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" @click="toEditProduct(scope.row)">修改</el-button>
                        <span style="margin:0 3px;">|</span>
                        <el-button type="text" size="small" class="delete" @click="showDelDialog(scope.row.deptId)">删除</el-button>
                    </template>
                </el-table-column>
                <el-table-column
                        :resizable = "false"
                        label="详情">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" @click="getDetailList(scope.row)">详情</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/icss.js';
  import config from '@api/config.js';
  import utils from '@api/utils.js';

  export default {
    name: 'PhysicalExamTemplate',
    data: function () {
      return {
        list: [], //常见症状列表
        cacheData: {},
        deptList: [], //科室下拉列表
        currentPage: 1,
        pageSize: config.pageSize,
        pageSizeArr:config.pageSizeArr,
        pageLayout:config.pageLayout,
        total: 0,
        linkIn:[],
        pays:[],
        searched: false,
        filter: {
          deptId: ''
        }
      }
    },
    created() {
      this.getDeptShortListSearh();
      const that = this;
      setTimeout(function () {
        that.getDataList();
      }, 200);
    },
    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();
      },
      addModule() {
        const pam = this.searched ? {
          currentPage: this.currentPage,
          pageSize:this.pageSize,
          filter: this.filter
        } : {currentPage: this.currentPage,
          pageSize:this.pageSize};
        this.$router.push({name: 'AddPhysicalExamTemp', params: pam});
      },
      toEditProduct(row){
        this.getPhysicalExamTempByDepId('isEdit', row)
      },
      getPhysicalExamTempByDepId(type, row) {
        const param = {'deptId': row.deptId,};
        api.getPhysicalExamTempByDepId(param).then((res)=>{
            const {code,data,msg} = res.data;
            if(code=='0'){
              const pam = this.searched ? {
                currentPage: this.currentPage,
                filter: this.filter
              } : {currentPage: this.currentPage};
                const item = Object.assign({},row,data);
              this.$router.push({name: 'AddPhysicalExamTemp', params: Object.assign(pam, {[type]: true, data: item})});
            }else{
                this.$message({
                message: msg,
                type: 'warning'
                });
            }
        });

      },
      filterDatas(){
        this.currentPage = 1;
        this.getDataList();
      },
      getDeptShortListSearh() {
        api.getDeptShortListSearh().then((res) => {
          const { data } = res
          if(data.code == 0) {
            this.deptList = data.data
          }
        })
      },
      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.getPhysicalExamTempList(param).then((res) => {
           loading.close()
          if (res.data.code == '0') {
            const data = res.data.data;
            this.list = data.records;
            this.total = data.total;
            if(this.inCurrentPage!==undefined){
              this.currentPage=this.inCurrentPage;
              this.inCurrentPage = undefined;
            }
          }
        }).catch((error) => {
          console.log(error);
        });
      },
      getDetailList(row) {
         this.getPhysicalExamTempByDepId('isDetail', row)
      },
      getFilterItems(isTurnPage) {
        //翻页时筛选条件没点确定则清空
        if(isTurnPage&&!this.searched){
          this.filter.deptId='';
        };
        const param = {
          deptId: this.filter.deptId,
          current: this.inCurrentPage||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(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(() => {});
      },
      showDelDialog(deptId){
        this.showConfirmDialog('是否删除该科室查体模板？',()=>{
          api.delPhysicalExamTemp({'deptId':deptId}).then((res)=>{
            if(res.data.code=='0'){
              if(!this.searched){
                //未点确认时清空搜索条件
                this.filter={
                  deptId: ''
                };
              }
              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);
          })
        });
      }
    }
  }
</script>

<style lang="less">
    @import "../../less/admin.less";
    .status-span{
        font-size: 12px;
        margin-right:10px;
        color: unset;
    }
    .delete{
        color: red
    }
</style>