<template>
    <div class="examination">
     <el-dialog @close="closeDialog" width="70%" :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="100px" :rules="ruleValidate" :model="baseData">
         <el-row>
         <el-col :span="12">
            <el-form-item prop="grandTypeName" label="类别区分">
             <el-select style="width: 322px" v-model="grandTypeNameTypeIndex" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in grandTypeNameType" :key="index" :label="item.baseName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="gradeName" label="牌号/等级名称">
              <el-input v-model="baseData.gradeName" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="doOrder" label="顺序号">
              <el-input v-model="baseData.doOrder" size="small"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item prop="gradeDesc" label="牌号/等级描述">
              <el-input v-model="baseData.gradeDesc" size="small"></el-input>
            </el-form-item>
          </el-col>

         </el-row>
          <el-row>
          <el-col :span="24">
            <el-form-item prop="memo" label="备注">
              <el-input v-model="baseData.memo" 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: ['showFlag', 'Params'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增牌号信息',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            grandTypeNameTypeIndex: 0,
            grandTypeNameType: [],
            tableData: [],
            baseData: {
              doOrder: '',
              gradeNo: '',
              gradeName: '',
              memo: '',
              gradeDesc: ''
            },
            ruleValidate: {
              //merchTypeName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              gradeName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // grandTypeName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      this.getgrandTypeNameType ()
    },
    methods: {
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.grandTypeNameTypeIndex = 0,
        _this.baseData = {
              doOrder: '',
              gradeNo: '',
              gradeName: '',
              memo: '',
              gradeDesc: ''
        }
        _this.dialogTableVisible = false
        _this.$emit('refresh')
      },
      addData () {
        let _this = this;
         _this.tableData = [];
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.dialogTableVisible = false;
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             _this.baseData.grandTypeCode = _this.grandTypeNameType[_this.grandTypeNameTypeIndex].baseCode;
             _this.baseData.grandTypeName = _this.grandTypeNameType[_this.grandTypeNameTypeIndex].baseName;
             _this.tableData.push(_this.baseData)
             _this.axios.post('pass/baseManagement/v1/limsbasegrades/', _this.tableData, {individualType: 'json'}).then(res => {
                if (res.succeed) {
                 this.$message.success('添加成功')
                 _this.$emit('refresh')
                } else {
                 this.$message.error(res.message)
                }
                }).catch(e => {
                 this.$message.error(e.message)
               })
         }
       })
      },
      modifyData () {
        let _this = this;
        _this.tableData = [];
        _this.$refs['baseData'].validate((valid) => {
        if (valid) {
        _this.dialogTableVisible = false;
        _this.baseData.updateMan = userName;
        _this.baseData.updateNo = userId;
        _this.baseData.grandTypeCode = _this.grandTypeNameType[_this.grandTypeNameTypeIndex].baseCode;
        _this.baseData.grandTypeName = _this.grandTypeNameType[_this.grandTypeNameTypeIndex].baseName;
        _this.tableData.push(_this.baseData)
        _this.axios.put('pass/baseManagement/v1/limsbasegrades/', _this.baseData, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            this.$message.success('修改成功')
            _this.$emit('refresh')
           } else {
            this.$message.error(res.message)
           }
           }).catch(e => {
            this.$message.error(e.message)
          })
        }
        })
      },
     closeDialog () {
       let _this = this;
       _this.tableData = [];
       _this.grandTypeNameTypeIndex = 0,
       _this.baseData = {
              doOrder: '',
              gradeNo: '',
              gradeName: '',
              memo: '',
              gradeDesc: ''
        }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
       _this.$emit('refresh')
     },
    getgrandTypeNameType () {
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4842}, {individualType: 'json'}).then(res => {
              this.grandTypeNameType = res.data
            })
     },
      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: {
      showFlag (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params !== null) {
          _this.baseData = _this.Params[0];
          _this.tableType = 1;
          _this.tableTypeDsc = '修改牌号信息';
          _this.seen = false;
          _this.beInvalid = true;
           for (let j = 0; j < _this.grandTypeNameType.length; j++) {
          if (_this.grandTypeNameType[j].baseName === _this.baseData.grandTypeName) {
             _this.grandTypeNameTypeIndex = j;
          }
          }
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.tableTypeDsc = '新增牌号信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>
