<template>
  <div class="examination">
    <div class="common-head-search">
      <el-form :inline="true" @submit.native.prevent>
        <el-form-item label='规则类型'>
          <el-select v-model="search.ruleTypeName" clearable filterable collapse-tags size="small" style="width:200px;margin-right: 10px;" placeholder="请选择查询内容">
            <el-option v-for="item in ruleType" :key="item.baseName" :label="item.baseName"
              :value="item.baseCode">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label='规则名称'>
          <el-input v-model="search.ruleName" @keyup.enter.native="searchData()" size="small" placeholder="请输入查询内容" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="goon" size="mini" @click="searchData">查询</el-button>
          <el-button icon="el-icon-refresh" type="info" plain size="mini" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-title-div">
      <div class="common-title-name">
        <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/logo.png' />
        业务规则信息
      </div>
      <div>
      <el-button icon="el-icon-circle-plus-outline" type="goon" size="mini" @click="addData">新增</el-button>
      <el-button icon="el-icon-edit" type="goon" size="mini" @click="modifyData">修改</el-button>
      <el-button icon="el-icon-remove-outline" type="danger" size="mini" @click="deleteData">停用</el-button>
      <el-button icon="el-icon-check" type="goon" size="mini" @click="recoverData">启用</el-button>
      </div>
    </div>
    <div class="common-table-div" style="margin-bottom:0;margin-top:0">
       <el-table v-loading="dataLoading" ref="dataTable" border :height="tableWidth" highlight-current-row @row-click="searchFollow" :data="table.list" :icore-filter-flag="icoreFilterFlag"
       :header-cell-style="tableHeaderCellStyle" :row-class-name="tableRowClassName" :summary-method="getSummaries" show-summary>
        <el-table-column type="index" label="NO" fixed="left" align="center">
        </el-table-column>
        <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="ruleNo" label="规则编号" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="ruleName" label="规则名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="ruleTypeName" label="规则类型" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="ruleVer" label="规则版本号" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="callApi" label="调用API"  align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="90px"  prop="validFlag1" label="数据状态" align="center">
          <!-- <template slot-scope="scope">
              <span v-if="scope.row.validFlag == 1">已启用</span>
              <span v-else-if="scope.row.validFlag == 0">已停用</span>
          </template> -->
        </el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="memo" label="备注" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="createMan" label="创建人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="createTime" label="创建时间" align="center"></el-table-column>
      </el-table>
    </div>
    <div class="common-foot-style">
        <el-pagination v-loading="dataLoading" @size-change="pageSizeChange" @current-change="pageCurrentChange"
          :current-page="page.pageNum" :page-sizes="[10, 20, 50, 100, 200]" :page-size="page.pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="page.totalPage" background>
        </el-pagination>
    </div>
    <div class="common-title-div">
      <div class="common-title-name">
        <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/logo.png' />
        规则参数明细
      </div>
      <div>
      <el-button icon="el-icon-circle-plus-outline" type="goon" size="mini" @click="addData2">新增</el-button>
      <el-button icon="el-icon-edit" type="goon" size="mini" @click="modifyData2">修改</el-button>
      <el-button icon="el-icon-remove-outline" type="danger" size="mini" @click="deleteData2">停用</el-button>
      <el-button icon="el-icon-check" type="goon" size="mini" @click="recoverData2">启用</el-button>
      <el-button icon="el-icon-remove-outline" type="danger" size="mini" @click="deletedetali">删除</el-button>
      </div>
    </div>
    <div class="common-table-div" style="margin-bottom:0;margin-top:0">
       <el-table v-loading="dataLoading2" ref="dataTable2" border :height="tableWidth" highlight-current-row @row-click="handleCurrentChange2" :data="table.list2" :icore-filter-flag="icoreFilterFlag"
       :header-cell-style="tableHeaderCellStyle" :cell-class-name="tableRowClassName2" :summary-method="getSummaries" show-summary>
        <el-table-column type="index" label="NO" fixed="left" align="center">
        </el-table-column>
        <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="ruleNoD" label="项编号" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="140px" prop="ruleItemName" label="项名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="ruleItemValue" label="默认值" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px"  prop="dataInputName" label="数据来源" align="center">
           <!-- <template slot-scope="scope">
              <span v-if="scope.row.ruleItemSource == 0">输入</span>
              <span v-else-if="scope.row.ruleItemSource == 1">输出</span>
              <span v-else-if="scope.row.ruleItemSource == 2">固定</span>
          </template> -->
        </el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="dataTypeName" label="数据类型" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="isPara1" label="是否参数" align="center">
          <!-- <template slot-scope="scope">
              <span v-if="scope.row.isPara == 0">非参数</span>
              <span v-else-if="scope.row.isPara == 1">参数</span>
          </template> -->
        </el-table-column>
        <!-- <el-table-column width="100px" prop="ruleItemValue" label="项目数据" align="center"></el-table-column> -->
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="ruleItemOrder" label="参数顺序" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="condition1" label="条件1" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="condition2" label="条件2" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="condition3" label="条件3" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="condition4" label="条件4" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="condition5" label="条件5" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="90px" prop="validFlag1" label="数据状态" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="memo" label="备注" align="center"></el-table-column>
        <!-- <el-table-column width="95px" prop="createMan" label="创建人" align="center"></el-table-column>
        <el-table-column width="160px" prop="createTime" label="创建时间" align="center"></el-table-column>
        <el-table-column width="95px" prop="updateMan" label="修改人" align="center"></el-table-column>
        <el-table-column width="160px" prop="updateTime" label="修改时间" align="center"></el-table-column> -->
      </el-table>
    </div>
    <div is="alertComponets" :showFlag="showFlag" :Params="Params" @refresh='refresh'></div>
    <div is="alertComponets2" :showFlag2="showFlag2" :Params="Params" :Params2="Params2" @refresh='refresh2'></div>
  </div>
</template>
<script>
import {
  getCookie
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
import alertComponets from './alertComponents';
import alertComponets2 from './alertComponents2';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  components: {alertComponets, alertComponets2},
  data () {
    return {
      table: {
        list: [],
        list2: []
      },
      dataLoading: false,
      dataLoading2: false,
      deTails:{},
      icoreFilterFlag: true,
      page: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0
      },
      page2: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0
      },
      ruleNo: '',
      showFlag: false,
      Params: {},
      showFlag2: false,
      Params2: {},
      ruleType: [
          // {ruleTypeCode: '483801', ruleTypeName: '组批'},
          // {ruleTypeCode: '483802', ruleTypeName: '抽签'},
          // {ruleTypeCode: '483803', ruleTypeName: '批号'}
      ],
      multipleSelection: [],
      // multipleSelection3: {},
      multipleSelection4: {},
      multipleSelection2: [],
      search: {
        ruleName: '',
        ruleTypeName: ''
      },
      tableWidth:0
    }
  },
  created(){
    this.tableWidth = window.innerHeight/2 - 130 < 249 ? 249 : window.innerHeight/2 - 100
  },
 mounted () {
    this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4838}, {individualType: 'json'}).then(res => {
      this.ruleType = res.data
    })
    this.searchData();
    this.addScreen();
    this.addScreen2();
  },
  methods: {
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
        }
        if (index === 1) {
          sums[index] = data.length;
        } 
      });
      return sums;
    },
    refresh () {
      this.getDataList();
    },
    refresh2 () {
      this.searchFollow(this.deTails);
    },
    //新增业务规则信息
    addData () {
      this.showFlag = !this.showFlag;
      this.Params = null;
    },
    //修改业务规则信息
    modifyData () {
      if (JSON.stringify(this.deTails)==="{}") {
        this.$message.warning('请点击选中您要修改的一条数据')
        return
      }
      this.showFlag = !this.showFlag;
      this.Params = Object.assign({},this.deTails) 
    },
    deleteData () {
      if (JSON.stringify(this.deTails)==="{}") {
        this.$message.warning('请点击选中您要停用的一条数据')
        return
      }
      this.deTails.updateMan = userName
      this.deTails.updateNo = userId
      this.axios.post('pass/baseManagement/v1/limsbaserules/abolish', [this.deTails], {individualType: 'json'}).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message);
          this.refresh()
        } else {
          this.$message.error(res.message)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    recoverData () {
      if (JSON.stringify(this.deTails)==="{}") {
        this.$message.warning('请点击选中您要启用的一条数据')
        return
      }
      this.deTails.updateMan = userName
      this.deTails.updateNo = userId 
      this.axios.post('pass/baseManagement/v1/limsbaserules/recover', [this.deTails], {individualType: 'json'}).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message);
          this.refresh()
        } else {
          this.$message.error(res.message)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    addData2 () {
      if (JSON.stringify(this.deTails)==="{}") {
        this.$message.warning('请点击选中一条业务规则信息数据')
        return
      }
      this.showFlag2 = !this.showFlag2;
      this.Params2 = null;
      this.Params = this.deTails
    },
    //修改规则参数明细
    modifyData2 () {
      if (JSON.stringify(this.multipleSelection4)==="{}") {
        this.$message.warning('请点击选中您要修改的一条数据')
        return
      }
      this.showFlag2 = !this.showFlag2;
      this.Params2 = Object.assign({},this.multipleSelection4);
      this.Params = null
    },
    //修改规则参数停用
    deleteData2 () {
      if (JSON.stringify(this.multipleSelection4)==="{}") {
        this.$message.warning('请点击选中您要停用的一条数据')
        return
      }
      this.multipleSelection4.updateMan = userName
      this.multipleSelection4.updateNo = userId
      this.axios.post('pass/baseManagement/v1/limsbaseruleds/abolish', [this.multipleSelection4], {individualType: 'json'}).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message);
          this.refresh2()
        } else {
         this.$message.error(res.message)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    //修改规则参数启用
    recoverData2 () {
      if (JSON.stringify(this.multipleSelection4)==="{}") {
        this.$message.warning('请点击选中您要启用的一条数据')
        return
      }
      this.multipleSelection4.updateMan = userName
      this.multipleSelection4.updateNo = userId 
      this.axios.post('pass/baseManagement/v1/limsbaseruleds/recover', [this.multipleSelection4], {individualType: 'json'}).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message);
          this.refresh2()
        } else {
         this.$message.error(res.message)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    deletedetali () {
      if (JSON.stringify(this.multipleSelection4)==="{}") {
        this.$message.warning('请点击选中您要删除的一条数据')
        return
      }
      this.multipleSelection4.updateMan = userName
      this.multipleSelection4.updateNo = userId 
      this.axios.delete('pass/baseManagement/v1/limsbaseruleds/'+this.multipleSelection4.id, {individualType: 'json'}).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message);
          this.refresh2()
        } else {
         this.$message.error(res.message)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    searchData () {
      this.page.pageNum = 1;
      this.getDataList()
    },
    getDataList () {
      this.deTails = {}
      let obj = {
        object:{
          ruleTypeCode:this.search.ruleTypeName,
          ruleName:this.search.ruleName
        },
        pageIndex:this.page.pageNum,
        pageSize:this.page.pageSize,
      }
      this.dataLoading = true;
      this.axios.post('pass/baseManagement/v1/limsbaserules/queryLimsBaseRuleInfoPage',obj,{individualType: 'json'}).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.table.list = res.data.list;
          this.page.totalPage = res.data.total;
          this.table.list.forEach(item =>{
            item.validFlag1 = item.validFlag == 0 ? '无效' : item.validFlag == 1 ? '有效' : ''
          })
          if (this.table.list.length > 0) {
            this.$refs.dataTable.setCurrentRow(this.table.list[0])
            this.searchFollow(this.table.list[0])
          }
        }else{
          this.$message.error(res.message)
        }
        this.dataLoading = false;
      }).catch(e => {
        this.dataLoading = false;
        this.$message.error(e.message)
      });
    },
    //查询规则参数明细数据
    searchFollow(row){
      this.deTails = row
      this.multipleSelection4 = {}
      this.dataLoading2 = true;
      this.axios.post('pass/baseManagement/v1/limsbaseruleds/queryLimsBaseRuleDList',{ruleNo:row.ruleNo},{individualType: 'json'}).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.table.list2 = res.data;
          this.table.list2.forEach(item =>{
            item.validFlag1 = item.validFlag === '0' ? '已停用' : item.validFlag === '1' ? '已启用' : ''
            item.ruleItemSource1 = item.ruleItemSource === '0' ? '输入' : item.ruleItemSource === '1' ? '输入' : item.ruleItemSource === '2' ? '固定' : ''
            item.isPara1 = item.isPara === '0' ? '非参数' : item.isPara === '1' ? '参数' : ''
          })
          this.page2.totalPage = res.data.total;  
        }else{
          this.$message.error(res.message)
        }
        this.dataLoading2 = false;
      }).catch(e => {
        this.dataLoading2 = false;
        this.$message.error(e.message)
      });
    },
    reset () {
      this.search.ruleName = '';
      this.search.ruleTypeName = '';
      this.searchData();
    },
    pageSizeChange (size) {
      this.page.pageNum = 1
      this.page.pageSize = size;
      this.getDataList();
    },
    pageCurrentChange (index) {
      this.page.pageNum = index;
      this.getDataList();
    },
    handleCurrentChange2 (val) {
      this.multipleSelection4 = val;
    },
    addScreen () {
      this.$nextTick(() => {
          let ss = document.createElement('div');
          ss.innerHTML = '筛选';
          ss.style = 'text-align: center;'
          this.$refs.dataTable.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
       });
    },
    addScreen2 () {
     this.$nextTick(() => {
          let ss = document.createElement('div');
          ss.innerHTML = '筛选';
          ss.style = 'text-align: center;'
          this.$refs.dataTable2.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
       });
    },
    tableRowClassName ({row, column, rowIndex, columnIndex}) {
      if (row.validFlag !== '1') {
        return 'error-row';
      }
      return '';
    },
    tableRowClassName2 ({row, column, rowIndex, columnIndex}) {
      if (row.validFlag == '0') {
        return 'error-row';
      }
      return '';
    },
    tableHeaderCellStyle () {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    }
  }
}
</script>
<style  scoped>
</style>
