<template>
    <div>
        <crumbs title="详情" :param="$route.params" linkTo="AutoTestTask">
            <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="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 label="状态：">
                    <el-select size="mini" v-model="filter.status" placeholder="状态" clearable>
                        <el-option v-for="item in statusTypes" :label="item.name" :value="item.id" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button size="mini" @click="filterDatas">确认</el-button>
                </el-form-item>
            </el-form>
        </crumbs>
        <div class="contents">
            <el-table :data="list"
                      border
                      v-loading="loading"
                      element-loading-text="拼命加载中"
                      element-loading-spinner="el-icon-loading"
                      element-loading-background="rgba(0, 0, 0, 0.6)"
                      style="width: 100%">
                <el-table-column
                        type="index"
                        :index="indexMethod"
                        label="编号"
                        width="60">
                </el-table-column>
                <el-table-column
                        prop="gmtModified"
                        label="所属医院"
                        width="110"
                        :formatter="hisFormatter"
                        :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column
                        prop="casesName"
                        width="110"
                        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
                        width="145"
                        prop="behospitalCodeStr"
                        label="病人住院序号">
                </el-table-column>
                <el-table-column
                        prop="status"
                        width="80"
                        :formatter="statusFormatter"
                        label="状态">
                </el-table-column>
                <el-table-column
                        prop="remark"
                        width="60"
                        label="备注">
                </el-table-column>
                <el-table-column
                        label="操作" width="100">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" @click="showEditDialog(scope.row)">修改</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>
        <el-dialog title="修改" :visible.sync="dialogVisible" class="autotest-dialog">
            <el-form>
                <el-form-item label="状态:" label-width="80">
                    <el-select size="mini" v-model="status" placeholder="状态">
                        <el-option v-for="item in editStatus" :label="item.name" :value="item.id" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注:" label-width="80">
                    <el-input type="textarea"
                              v-model="remark"
                              placeholder="请输入备注"
                              :rows="3"
                              autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="editTaskDetail">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

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

  export default {
    name: 'AutoTestList',
    data: function () {
      return {
        list: [],
        tagType:[1],
        currentPage: 1,
        pageSize: 10,
        total: 0,
        loading: false,
        fieldTypes:[],
        searched:false,
        hisTypes:[],
        statusTypes:[
          {id:null,name:'全部'},
          {id:0,name:'未处理'},
          {id:1,name:'已处理'},
          ],
        editStatus:[
          {id:0,name:'未处理'},
          {id:1,name:'已处理'},
        ],
        taskName:"",
        dialogVisible:false,
        editId:'',
        remark:'',
        status:'',
        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: {
      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("获取模块信息失败");
          }
        });
      },
      statusFormatter(it){
        return +it.status===0?"未处理":"已处理";
      },
      hisFormatter(item){
        const field = this.hisTypes.filter((it)=>it.id==item.hospitalId);
        return field[0]?field[0].name:'';
      },
      showEditDialog(row){
        this.dialogVisible = true;console.log(row)
        this.editId = row.id;
        this.remark=row.remark;
        this.status = row.status;
      },
      editTaskDetail(){
        const param = {
          id:this.editId,
          remark:this.remark,
          status:this.status,
        };
        api.editTaskDetail(param).then((res) => {
          if (res.data.code == '0') {
            this.dialogVisible = false;
              this.warning(res.data.msg||'修改成功','success');
            this.getDataList();
          }
        }).catch((error) => {
          console.log(error);
        });
      },
      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.getTaskDetailList(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,
          missionId:this.$route.params.id,
        },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.delTaskDetail({id: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>