<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" customClass="customWidth_comm">
         <div slot="title" class="titleBox" style="display: flex;">
        <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>
        <table class="common-style-table" v-if="seenSortCode">
          <tr>
            <td>父代码编码</td>
            <td>{{sortCode}}</td>
            <td>父代码名称</td>
            <td>{{sortName}}</td>
          </tr>
        </table>
        </div> -->
        </div>
       <el-form ref="baseData" label-width="80px" :rules="ruleValidate" :model="baseData">
          <el-row  v-if="seenSortCode">
          <el-col :span="10">
            <el-form-item prop="sortCode" label="父代码编码">
              <el-input v-model="sortCode" size="small" readonly disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item prop="sortName" label="父代码名称">
              <el-input v-model="sortName" size="small" readonly disabled></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="10">
            <el-form-item prop="baseCode" label="代码编号">
              <el-input v-model="baseData.baseCode" size="small" readonly disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item prop="baseName" label="代码名称">
              <el-input v-model="baseData.baseName" size="small" @blur="myBlur"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
          <el-row>
          <el-col :span="10">
            <el-form-item prop="mnemonicCode" label="拼音助记">
              <el-input v-model="baseData.mnemonicCode" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-checkbox v-model="isEditValue" style="margin-left:20px;margin-right:10px">是否可编辑</el-checkbox>
          </el-col>
           <el-col :span="5">
            <el-checkbox v-model="isSubValue" style="margin-right:10px">是否允许添加子类</el-checkbox>
          </el-col>
         </el-row>         
         <el-row>
          <el-col :span="20">
            <el-form-item prop="memo" label="备注">
              <el-input v-model="baseData.memo" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
          <el-row>
          <el-col :span="20">
            <el-form-item prop="memo1" label="备注1">
              <el-input v-model="baseData.memo1" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
          <el-row>
          <el-col :span="20">
            <el-form-item prop="memo2" label="备注2">
              <el-input v-model="baseData.memo2" 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);
import pinyinUtils from '../../../../utils/pinyin/pinyinUtils.js';
export default {
  props: ['showFlag', 'Params', 'Params2'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增项',
            tableType: 0,
            seen: true,
            seenNo: true,
            seenSortCode: false,
            beInvalid: false,
            isEditValue: true,
            isSubValue: true,
            tableData: [],
            baseData: {
              baseNo: '',
              baseName: '',
              mnemonicCode: '',
              memo: '',
              memo1: '',
              memo2: '',
              isEdit: '',
              isSub: ''
            },
            ruleValidate: {
              strName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // strDesc: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // mnemonicCode: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              baseName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    methods: {
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.baseData = {
              baseNo: '',
              baseName: '',
              mnemonicCode: '',
              memo: '',
              memo1: '',
              memo2: '',
              isEdit: '',
              isSub: ''
        }
        _this.isEditValue = true,
        _this.isSubValue = true,
        _this.dialogTableVisible = false
        _this.$emit('refresh')
      },
      addData () {
        let _this = this;
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             _this.baseData.sortCode = _this.sortCode;
             _this.baseData.sortName = _this.sortName;
             if (_this.isEditValue === false) {
               _this.baseData.isEdit = 0;
             } else {
               _this.baseData.isEdit = 1;
             }
            if (_this.isSubValue === false) {
               _this.baseData.isSub = 0;
             } else {
               _this.baseData.isSub = 1;
             }            
             _this.axios.post('pass/baseManagement/v1/limsbaseinfos/',_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)
               })
         }
       })
      },
        myBlur () {
        if (this.baseData.baseName) {
          this.baseData.mnemonicCode = pinyinUtils.initial(this.baseData.baseName);
        }
      },
      modifyData () {
        let _this = this;
        _this.$refs['baseData'].validate((valid) => {
        if (valid) {
        _this.baseData.updateMan = userName;
        _this.baseData.updateNo = userId;
         if (_this.isEditValue === false) {
               _this.baseData.isEdit = 0;
             } else {
               _this.baseData.isEdit = 1;
             }
            if (_this.isSubValue === false) {
               _this.baseData.isSub = 0;
             } else {
               _this.baseData.isSub = 1;
             }       
        _this.axios.put('pass/baseManagement/v1/limsbaseinfos/', _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:10px" onClick={() => this.addRow()}>
             <i class="el-icon-plus" style="font-size:10px; color:rgb(18, 51, 143)"></i>
             </div>
         )
      },
     closeDialog () {
       let _this = this;
       _this.tableData = [];
       _this.baseData = {
              baseNo: '',
              baseName: '',
              mnemonicCode: '',
              memo: '',
              memo1: '',
              memo2: '',
              isEdit: '',
              isSub: ''
        }
       _this.seen = true;
       _this.seenNo = true;
       _this.seenSortCode = false;
       _this.beInvalid = false;
       _this.isEditValue = true,
       _this.isSubValue = true,
       _this.$emit('refresh')
     },
      tableHeaderCellStyle () {
      return 'background-color: rgb(149,149,149) !important;color: rgb(73, 71, 71);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;
          if (_this.Params[0].isEdit === '0') {
            _this.isEditValue = false
          } else {
            _this.isEditValue = true
          }
          if (_this.Params[0].isSub === '0') {
            _this.isSubValue = false
          } else {
            _this.isSubValue = true
          }
        } else {
          _this.tableData = [];
          _this.baseData = {
                   baseNo: '',
                   baseName: '',
                   mnemonicCode: '',
                   memo: '',
                   memo1: '',
                   memo2: '',
                   isEdit: '',
                   isSub: ''
          }
          _this.sectionNameTypeIndex = 0;
          _this.levelNumTypeIndex = 0;
          _this.strTypeNameTypeIndex = 0;
          _this.companyNameTypeIndex = 0;
          _this.deptNameTypeIndex = 0;
          if ( _this.Params2.baseCode !== undefined) {
           _this.seenSortCode = true
           _this.sortCode = _this.Params2.baseCode
           _this.sortName = _this.Params2.baseName
          } 
          _this.tableType = 0;
          _this.seenNo = false;
          _this.tableTypeDsc = '新增项';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_comm{
  width: 700px !important
}
</style>
