<template>
    <div>
        <crumbs title="字段映射维护">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item label="所属医院：">
                    <el-select size="mini" v-model="filter.hospitalId" placeholder="所属医院" clearable>
                        <el-option v-for="item in hisTypes" :label="item.name" :value="item.val" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="所属模块：">
                    <el-select size="mini" v-model="filter.modeId" placeholder="所属模块" clearable>
                        <el-option v-for="item in fieldTypes" :label="item.name" :value="item.val" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注：">
                    <el-input size="mini" v-model="filter.tagName" placeholder="备注"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="mini" @click="filterDatas">确认</el-button>
                    <el-button size="mini" type="warning" @click="addBaseField">添加字段映射</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="所属医院"
                        width="150"
                        :formatter="hisFormatter"
                        :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column
                        prop="name"
                        :formatter="moduleFormatter"
                        label="所属模块">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="显示名称">
                </el-table-column>
                <el-table-column
                        prop="val"
                        label="取值字段">
                </el-table-column>
                <el-table-column
                        prop="tagName"
                        label="备注">
                </el-table-column>
                <!--<el-table-column
                        prop="name"
                        width="200"
                        label="关联缺陷条目">
                </el-table-column>
                <el-table-column
                        prop="name"
                        width="60"
                        label="换行">
                </el-table-column>
                <el-table-column
                        prop="name"
                        width="60"
                        label="加粗">
                </el-table-column>
                <el-table-column
                        prop="name"
                        width="60"
                        label="缩进">
                </el-table-column>-->
                <el-table-column
                        label="操作" width="100">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" @click="toEditField(scope.row, 'modify')">修改</el-button>
                        <span style="margin:0 3px;">|</span>
                        <el-button type="text" size="small" @click="toEditField(scope.row, 'copy')">复制</el-button>
                        <span style="margin:0 3px;">|</span>
                        <el-button type="text" size="small" class="delete" @click="showDelDialog(scope.row.id)">删除</el-button>
                    </template>
                </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/qualityControl.js';

  export default {
    name: 'BaseField',
    data: function () {
      return {
        list: [],
        tagType:[1],
        currentPage: 1,
        pageSize: 10,
        total: 0,
        fieldTypes:[],
        searched:false,
        hisTypes:[],
        filter: {
          hospitalId:'',
          modeId:'',
          tagName: ''
        }
      }
    },
    created() {
      const _this=this;
      setTimeout(function() {
        _this.getAllTypes()
      },100);
    },
    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: {
      moduleFormatter(item){
        const field = this.fieldTypes.filter((it)=>it.val==item.modeId);
        return field[0]?field[0].name:'';
      },
      hisFormatter(item){
        const field = this.hisTypes.filter((it)=>it.val==item.hospitalId);
        return field[0]?field[0].name:'';
      },
      getAllTypes(){
        /*if(localStorage.getItem("qcModuleTypes")){
          this.hisTypes = JSON.parse(localStorage.getItem("qcHospitalTypes"));
          this.fieldTypes = JSON.parse(localStorage.getItem("qcModuleTypes"));
          this.getDataList();
          return ;
        }*/
        //获取枚举信息
        api.getQcTypes().then((res)=>{
          if(res.data.code==="0"){
            const data = res.data.data;
            //localStorage.setItem("qcModuleTypes",JSON.stringify(data[12]));
            //localStorage.setItem("qcHospitalTypes",JSON.stringify(data[13]));
            this.hisTypes =data[13];
            this.fieldTypes =data[12];
            this.getDataList();
          }else{
            this.warning("获取枚举信息失败");
          }
        });
      },
      addBaseField(){
        const pam = this.searched ? {
          currentPage: this.currentPage,
          filter: this.filter
        } : {currentPage: this.currentPage};
        this.$router.push({name: 'FieldMatch', params: pam});
      },
      toEditField(row,type){
        api.getFieldDetail({id:row.id}).then((res)=>{
          const {code,data,msg} = res.data;
          if(code=='0'){
            const item = Object.assign({},row,data);
            const pam = this.searched ? {
              currentPage: this.currentPage,
              filter: this.filter
            } : {currentPage: this.currentPage};
            if(type == 'modify') {
              this.$router.push({name: 'FieldMatch', params: Object.assign(pam, {isEdit: true, info: item})});
            } else if( type == 'copy') {
              this.$router.push({name: 'FieldMatch', params: Object.assign(pam, {isCopy: true, info: item})});
            } else {
              return
            }
          }else{
            this.$message({
              message: msg,
              type: 'warning'
            });
          }
        });
        // this.$router.push({
        //   name:'FieldMatch',
        //   params: {info:row}
        // })
      },
      filterDatas(){
        this.currentPage = 1;
        this.getDataList();
      },
      getDataList() {
        const param = this.getFilterItems();
        this.searched = true;
        api.getFieldList(param).then((res) => {
          if (res.data.code == '0') {
            const data = res.data.data;
            this.list = data.records;
            this.total = data.total;
          }
        }).catch((error) => {
          console.log(error);
        });
      },
      getFilterItems() {
        const param = Object.assign({
          current: this.currentPage,
          size: this.pageSize,
          tagTypeList:[1]
        },this.filter);
        return param;
      },
      indexMethod(index) {
        return ((this.currentPage - 1) * this.pageSize) + index + 1;
      },
      currentChange(next) {
        this.currentPage = next;
        this.getDataList();
      },
      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(id){
        this.showConfirmDialog('是否删除该字段映射？',()=>{
          api.delFieldMatch({ids:id}).then((res)=>{
            if(res.data.code=='0'){
              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";
    .delete{
        color: red !important;
    }
</style>