<template>
    <div>
        <crumbs title="自动化测试统计">
            <el-form :inline="true" class="demo-form-inline" style="max-width: 870px;text-align: right;">
                <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.id" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="所属模块：">
                    <el-select size="mini" v-model="filter.casesId" placeholder="所属模块" clearable>
                        <el-option v-for="item in fieldTypes" :label="item.name" :value="String(item.id)" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="缺陷名称：">
                    <el-input size="mini" v-model="filter.entryName" placeholder="缺陷名称"></el-input>
                </el-form-item>
                <el-form-item label="条目编码：">
                    <el-input size="mini" v-model="filter.entryCode" placeholder="条目编码"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="mini" @click="filterDatas">确认</el-button>
                    <el-button size="mini" type="warning" @click="setTaskName">转化为任务</el-button>
                    <el-button size="mini" type="warning" @click="showTestDialog">自动化测试</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="120"
                        :formatter="hisFormatter"
                        :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column
                        prop="casesName"
                        width="120"
                        label="所属模块">
                </el-table-column>
                <el-table-column
                        prop="entryName"
                        label="缺陷名称">
                </el-table-column>
                <el-table-column
                        prop="entryCode"
                        width="120"
                        label="条目编码">
                </el-table-column>
                <el-table-column
                        prop="amount"
                        width="80"
                        label="差异数量">
                </el-table-column>
                <el-table-column
                        prop="behospitalCodeStr"
                        width="145"
                        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="pageLayout"
                           :total="total">
            </el-pagination>
        </div>
        <el-dialog title="新建" :visible.sync="dialogTaskName" class="autotest-dialog">
            <el-form>
                <el-form-item label="任务名称:" label-width="80">
                    <el-input v-model.tirm="taskName" placeholder="请输入任务名称" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="addTask" :disabled="addBtnDisable">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog title="提示" :visible.sync="dialogTippop" class="tip-dialog">
            <div class="dialog-body">
                <p>经程序自动化测试，下列病历存在问题，请及时处理~</p>
                <br/>
                <div class="warn-tip" v-html="tips"></div>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="addTask">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
  import api from '@api/qualityControl.js';
  import { Loading } from 'element-ui';
  import config from '@api/config.js';

  export default {
    name: 'AutoTestList',
    data: function () {
      return {
        list: [],
        tagType:[1],
        currentPage: 1,
        pageSize: 10,
        total: 0,
        loading: false,
        fieldTypes:[],
        searched:false,
        hisTypes:[],
        taskName:"",
        dialogTaskName:false,
        dialogTippop:false,
        addBtnDisable:false,
        pageLayout:config.pageLayout,
        tips:'',
        filter: {
          hospitalId:'',
          entryName:'',
          entryCode: '',
          casesId:'',
        }
      }
    },
    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: {
      hisFormatter(item){
        const field = this.hisTypes.filter((it)=>it.id==item.hospitalId);
        return field[0]?field[0].name:'';
      },
      getAllTypes(){
        api.getRecordHopitalList().then((res)=>{
          if(res.data.code==="0"){
            const data = res.data.data;
            this.hisTypes =data;
            this.getDataList();
          }else{
            this.warning("获取医院信息失败");
          }
        });
        api.getRecordCases().then((res)=>{
          if(res.data.code==="0"){
            const data = res.data.data;
            this.fieldTypes =data;
          }else{
            this.warning("获取模块信息失败");
          }
        });
      },
      autoTest(){
        //this.loading=true;
        let loadingInstance =Loading.service({background:"rgba(0, 0, 0, 0.6)"});
        api.autoTest().then((res)=>{
          if(res.data.code==="0"){
            //this.loading=false;
            loadingInstance.close();
            const {messages}=res.data.data;
            let str='';
            if(messages.length>0){
              for(let i=0;i<messages.length;i++){
                str+=`<p>`+messages[i]+`</p>`;
              }
              this.tips = str;
              this.dialogTippop=true;
            }else{
              this.warning(res.data.msg||'操作成功','success');
            }
            this.getDataList();
          }else{
            this.warning(res.data.msg||"自动化测试失败");
          }
        });
      },
      setTaskName(){
        this.dialogTaskName = true;
      },
      addTask(){
        if(!this.taskName.trim()){
          this.warning("请输入任务名称");
          return;
        }
        this.addBtnDisable=true;
        api.addTask({missionName:this.taskName}).then((res)=>{
          this.addBtnDisable=false;
          if(res.data.code==="0"){
            this.dialogTaskName = false;
            this.toTaskList();
          }else{
            this.warning(res.data.msg||"转化为任务失败");
          }
        });
      },
      toTaskList(){
        const pam = this.searched ? {
          currentPage: this.currentPage,
          filter: this.filter
        } : {currentPage: this.currentPage};
        this.$router.push({name: 'AutoTestTask', params: pam});
      },
      filterDatas(){
        this.currentPage = 1;
        this.getDataList();
      },
      getDataList() {
        const param = this.getFilterItems();
        this.searched = true;
        api.getTestItems(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,
        },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'
        })
      },
      showTestDialog(){
        this.showConfirmDialog('所有标准病历自动化测试，可能将等待很长时间~',()=>{
          this.autoTest();
        });
      },
      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>
   .el-loading-spinner{
        top:200px;
    }
    .autotest-dialog .el-dialog__body{
        padding: 30px 20px;
    }
   .autotest-dialog .el-form-item__content{
       display: inline-block;
       width: 272px;
    }
    .autotest-dialog .el-dialog{
        width: 400px!important;
    }
</style>
<style lang="less" scoped>
    @import "../../less/admin.less";
    .delete{
        color: red !important;
    }
    div.container{
        height: 90px!important;
    }
    .contents{
        padding:120px 20px 0;
    }
</style>