<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible"  width='60%'>
        <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="8">
            <el-form-item prop="unitTypeName" label="度量名称">
              <el-input v-model="baseData.unitTypeName" style="width:100%" readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
           <el-col :span="8">
            <el-form-item prop="unitName" label="单位名称">
              <el-input v-model="baseData.unitName" style="width:100%" size="small"></el-input>
            </el-form-item>
           </el-col>
           <el-col :span="8">
            <el-form-item prop="unitDesc" label="中文描述">
              <el-input v-model="baseData.unitDesc" style="width:100%" size="small"></el-input>
            </el-form-item>
           </el-col>
          </el-row>
          <el-row>
          <!-- <el-col :span="10">
            <el-form-item prop="matClassName" label="物料类型">
              <el-select v-model="matClassNameTypeIndex" filterable collapse-tags size="small" @change="selectTrigger(matClassNameTypeIndex)">
                <el-option v-for="(item,index) in matClassNameType" :key="index" :label="item.matClassName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item prop="matName" label="物料名称">
             <el-select v-model="matNameTypeIndex" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in matNameType" :key="index" :label="item.matName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
         </el-row>
         <el-row>
           <el-col :span="16">
            <el-form-item prop="unitEnglish" label="英文描述">
              <el-input v-model="baseData.unitEnglish" style="width:100%" size="small"></el-input>
            </el-form-item>
           </el-col>
           <el-col :span="8">
            <el-form-item prop="isStdUnit" style="width:100%" label="是否标准单位">
              <el-radio v-model="isStdUnitTypeIndex" label='0'>是</el-radio>
              <el-radio v-model="isStdUnitTypeIndex" label='1'>否</el-radio>
            </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', 'unitType'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增单位信息',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            isStdUnitTypeIndex: '0',
            isStdUnitType: [
              {id: '0', isStdUnit: '是'},
              {id: '1', isStdUnit: '否'}
            ],
            tableData: [],
            baseData: {
              unitTypeCode: '',
              unitTypeName: '',
              unitName: '',
              unitDesc: '',
              unitEnglish: '',
            },
            ruleValidate: {
              unitName: [{ required: true, message: '该项不能为空', trigger: 'blur' }]
             }
        }
    },
    methods: {
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.isStdUnitTypeIndex = '0',
        _this.baseData = {
              unitTypeCode: '',
              unitTypeName: '',
              unitName: '',
              unitDesc: '',
              unitEnglish: '',
        }
        _this.dialogTableVisible = false
        _this.$emit('refresh')
      },
      addData () {
        let _this = this;
         _this.tableData = [];
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             _this.baseData.isStdUnit = _this.isStdUnitType[_this.isStdUnitTypeIndex].isStdUnit;
             _this.tableData.push(_this.baseData)
             _this.axios.post('pass/baseManagement/v1/limsbaseunits/', _this.tableData, {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 () {
        let _this = this;
        _this.tableData = [];
        _this.$refs['baseData'].validate((valid) => {
        if (valid) {
        _this.baseData.updateMan = userName;
        _this.baseData.updateNo = userId;
        _this.baseData.isStdUnit = _this.isStdUnitType[_this.isStdUnitTypeIndex].isStdUnit;
        _this.tableData.push(_this.baseData)
        _this.axios.put('pass/baseManagement/v1/limsbaseunits/', _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)
          })
        }
        })
      },
     closeDialog () {
       let _this = this;
       _this.tableData = [];
       _this.baseData = {
              unitTypeCode: '',
              unitTypeName: '',
              unitName: '',
              unitDesc: '',
              unitEnglish: '',
        }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
       _this.$emit('refresh')
     }
    },
    watch: {
      showFlag (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params !== null) {
          _this.baseData = Object.assign({},this.Params)
          _this.tableType = 1;
          _this.tableTypeDsc = '修改单位信息';
          _this.seen = false;
          _this.beInvalid = true;
          // for (let j = 0; j < _this.matNameType.length; j++) {
          // if (_this.matNameType[j].matName === _this.baseData.matName) {
          //    _this.matNameTypeIndex = j;
          // }
          // }
          // for (let j = 0; j < _this.matClassNameType.length; j++) {
          // if (_this.matClassNameType[j].matClassName === _this.baseData.matClassName) {
          //    _this.matClassNameTypeIndex = j;
          // }
          // }
          for (let j = 0; j < _this.isStdUnitType.length; j++) {
          if (_this.isStdUnitType[j].isStdUnit === _this.baseData.isStdUnit) {
             _this.isStdUnitTypeIndex = j;
          }
          }
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.baseData.unitTypeCode = _this.unitType.baseCode;
          _this.baseData.unitTypeName = _this.unitType.baseName;
          _this.tableTypeDsc = '新增单位信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>
