<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible">
        <div slot="title" class="titleBox">
        <i :class="tableType === 0? 'el-icon-document':'el-icon-edit-outline'" style="font-size: 20px"></i>
        <!-- <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/more.png' /> -->
        <span style="margin-left:10px;font-size:18px">{{tableTypeDsc}}</span>
        </div>
       <el-form ref="baseData" label-width="80px" :rules="ruleValidate" :model="baseData">
          <el-row>
            <el-col :span="12">
              <el-form-item prop="ruleItemName" label="项名称">
                <el-input v-model="baseData.ruleItemName" style="width:100%"  size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="条件1">
                <el-input v-model="baseData.condition1" style="width:100%"  size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="ruleItemValue" label="默认值">
                <el-input v-model="baseData.ruleItemValue" style="width:100%"  size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="条件2">
                <el-input v-model="baseData.condition2" style="width:100%"  size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
          <el-col :span="12">
            <el-form-item prop="dataInputCode" label="数据来源">
              <el-select v-model="baseData.dataInputCode" style="width:100%" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in ruleItemSourceType" :key="index" :label="item.baseName" :value="item.baseCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="条件3">
              <el-input v-model="baseData.condition3" style="width:100%"  size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="dataTypeCode" label="数据类型">
               <el-select v-model="baseData.dataTypeCode" style="width:100%" filterable collapse-tags size="small">
                  <el-option v-for="(item,index) in dataTypeNameType" :key="index" :label="item.baseName" :value="item.baseCode">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="条件4">
              <el-input v-model="baseData.condition4" style="width:100%"  size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
          <el-row>
            <el-col :span="12">
            <el-form-item prop="isPara" label="是否参数">
             <el-select v-model="baseData.isPara" filterable style="width:100%" collapse-tags size="small">
                <el-option v-for="(item,index) in isParaType" :key="index" :label="item.isPara" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
            <el-col :span="12">
              <el-form-item  label="条件5">
                <el-input v-model="baseData.condition5" style="width:100%" size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="ruleItemOrder" label="参数顺序">
                <el-input v-model="baseData.ruleItemOrder" style="width:100%"  size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="memo" label="备注">
                <el-input v-model="baseData.memo" style="width:100%" size="small"></el-input>
              </el-form-item>
            </el-col>
         </el-row>
       </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button type="goon" size="mini" icon="el-icon-check" @click="tableType === 0 ? addData():modifyData()">确定</el-button>
        <el-button size="mini" type="goon" icon="el-icon-remove-outline" @click="cancel">取消</el-button>
      </span>
    </el-dialog>
    </div>
</template>
<script>
import {
  getCookie
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  props: ['showFlag2', 'Params', 'Params2'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增规则参数明细信息',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            ruleItemSourceTypeIndex: 0,
            dataTypeNameTypeIndex: 0,
            isParaTypeIndex: 0,
            ruleItemSourceType: [],
            dataTypeNameType: [],
            isParaType: [
              {id: '0', isPara: '非参数'},
              {id: '1', isPara: '参数'}
            ],
            tableData: [],
            baseData: {
              isPara:'0'
            },
            ruleValidate: {
              ruleItemName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              dataInputCode: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              dataTypeCode: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              isPara: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              ruleItemOrder: [{ required: true, message: '该项不能为空', trigger: 'blur' }]
             }
        }
    },
    mounted () {
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4825}, {individualType: 'json'}).then(res => {
        this.dataTypeNameType = res.data
      })
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4844}, {individualType: 'json'}).then(res => {
        this.ruleItemSourceType = res.data
      })
    },
    methods: {
      getCode (code, arr, codeKey, nameKey) {
        if (!code) {
          return '';
        }
        let targetRow = arr.find(item => {
          return item[codeKey] === code;
        });
        // console.log(targetRow, 'targetRow');
        return targetRow ? targetRow[nameKey] : '';
      },
      cancel () {
        this.baseData= {isPara:'0'},
        this.dialogTableVisible = false
      },
      addData () {
        this.$refs['baseData'].validate((valid) => {
          if (valid) {
            this.baseData.createMan = userName;
            this.baseData.createNo = userId;
            this.baseData.dataInputName = this.getCode(this.baseData.dataInputCode,this.ruleItemSourceType,'baseCode','baseName')    
            this.baseData.dataTypeName = this.getCode(this.baseData.dataTypeCode,this.dataTypeNameType,'baseCode','baseName')
            this.axios.post('pass/baseManagement/v1/limsbaseruleds/', [this.baseData], {individualType: 'json'}).then(res => {
              if (res.succeed) {
                this.dialogTableVisible = false;
                this.$message.success('添加成功')
                this.$emit('refresh')
              } else {
                this.$message.error(res.message)
              }
            }).catch(e => {
              this.$message.error(e.message)
            })
          }
        })
      },
      modifyData () {
        this.$refs['baseData'].validate((valid) => {
          if (valid) {
            this.baseData.updateMan = userName;
            this.baseData.updateNo = userId;
            this.baseData.dataInputName = this.getCode(this.baseData.dataInputCode,this.ruleItemSourceType,'baseCode','baseName')
            this.baseData.dataTypeName = this.getCode(this.baseData.dataTypeCode,this.dataTypeNameType,'baseCode','baseName')
            this.axios.put('pass/baseManagement/v1/limsbaseruleds/', this.baseData, {individualType: 'json'}).then(res => {
              if (res.succeed) {
                this.dialogTableVisible = false;
                this.$message.success('修改成功')
                this.$emit('refresh')
              } else {
                this.$message.error(res.message)
              }
            }).catch(e => {
              this.$message.error(e.message)
            })
          }
        })
      },
      renderHeader (h) {
         return (
             <div style="cursor: pointer ;line-height:20px" onClick={() => this.addRow()}>
             <i class="el-icon-plus" style="font-size:20px; color:rgb(18, 52, 243)"></i>
             </div>
         )
      },
      closeDialog () {
        this.baseData= {isPara:'0'}
      },
      tableHeaderCellStyle () {
        return 'background-color: rgb(249,249,249) !important;color: rgb(73, 72, 72);border-bottom: 1px solid #DCDFE7;border-top:3px solid #3ac1ea;'
      }
    },
    watch: {
      showFlag2 (arg) {
        if (this.Params2 !== null) {
          this.baseData = Object.assign({},this.Params2);
          this.tableType = 1;
          this.tableTypeDsc = '修改规则参数明细信息';
        } else {
          this.tableType = 0;
          this.baseData.ruleNo = this.Params.ruleNo;
          // _this.baseData.ruleNoD = _this.Params.ruleNoD;
          // _this.baseData.merchNo = _this.Params.merchNo;
          this.tableTypeDsc = '新增规则参数明细信息';
        }
        this.dialogTableVisible = true;
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>

