<template>
    <div class="examination materialConfiguration-alertComponents3">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" customClass="customWidth_mate">
        <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 :model="monitorForm.tableData" label-width="80px" :rules="monitorForm.monitorRules" ref="monitorForm">
          <el-row v-if="seen">
            <!-- <el-col :span='12'>
              <el-form-item prop="parentNo" label="父类编码">
                <el-input v-model="monitorForm.tableData.parentNo" readonly  disabled  size="small" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col> -->
            <el-col :span='24'>
              <el-form-item prop="parentName" label="父类名称">
                <el-input v-model="monitorForm.tableData.parentName" size="small" placeholder="请输入" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="seen">
            <!-- <el-col :span='12'>
              <el-form-item prop="matClassNo" label="子类编码">
                <el-input v-model="monitorForm.tableData.matClassNo" readonly  disabled size="small"  style="width: 100%;"></el-input>
              </el-form-item>
            </el-col> -->
            <el-col :span='24'>
              <el-form-item prop="matClassName" label="子类名称">
                <el-input v-model="monitorForm.tableData.matClassName" size="small" placeholder="请输入" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="beInvalid">
            <!-- <el-col :span='12'>
              <el-form-item prop="matClassNo2" label="分类编号">
                <el-input v-model="monitorForm.tableData.matClassNo2" readonly  disabled size="small"  style="width: 100%;"></el-input>
              </el-form-item>
            </el-col> -->
            <el-col :span='24'>
              <el-form-item prop="matClassName2" label="分类名称">
                <el-input v-model="monitorForm.tableData.matClassName2" size="small" placeholder="请输入" style="width: 100%;"></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="monitorForm.tableData.memo" size="small" placeholder="请输入" style="width: 100%;"></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: ['showFlag3', 'Params', 'Params3'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增物料分类',
            tableType: 0,
            seen: false,
            seenNo: true,
            beInvalid: false,
            isStdUnitType: [
              {isStdUnit: '是'},
              {isStdUnit: '否'}
            ],
            monitorForm: {
            tableData: {
                parentNo: '',
                parentName: '',
                matClassNo: '',
                matClassName: '',
                matClassNo2: '',
                matClassName2: '',
                memo: ''
            },
            monitorRules: {
            //   parentNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              parentName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
            //   matClassNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              matClassName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
            //   matClassNo2: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              matClassName2: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
            }
        }
    },
    methods: {
      cancel () {
        let _this = this;
        _this.monitorForm.tableData = {
             parentNo: '',
             parentName: '',
             matClassNo: '',
             matClassName: '',
             matClassNo2: '',
             matClassName2: '',
             memo: ''
        }
        _this.dialogTableVisible = false
        // _this.$refs['monitorForm'].clearValidate();
        _this.$emit('refresh')
      },
      addData () {
        let _this = this;
         _this.tableData = [];
         _this.$refs['monitorForm'].validate((valid) => {
          if (valid) {
             _this.dialogTableVisible = false;
             _this.monitorForm.tableData.createMan = userName;
             _this.monitorForm.tableData.createNo = userId;
             if (_this.beInvalid === true) {
                //  _this.monitorForm.tableData.matClassNo = _this.monitorForm.tableData.matClassNo2
                 _this.monitorForm.tableData.matClassName = _this.monitorForm.tableData.matClassName2
             }
             _this.axios.post('pass/baseManagement/v1/limsbasematerialsclasss/', _this.monitorForm.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['monitorForm'].validate((valid) => {
        if (valid) {
        _this.dialogTableVisible = false;
        _this.monitorForm.tableData.createMan = userName;
        _this.monitorForm.tableData.createNo = userId;
        _this.monitorForm.tableData.matClassName = _this.monitorForm.tableData.matClassName2;
        _this.axios.put('pass/baseManagement/v1/limsbasematerialsclasss/', _this.monitorForm.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)
          })
        }
        })
      },
      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 () {
       let _this = this;
        _this.monitorForm.tableData = {
             parentNo: '',
             parentName: '',
             matClassNo: '',
             matClassName: '',
             matClassNo2: '',
             matClassName2: ''
        }
    //    _this.$refs['monitorForm'].clearValidate();
       _this.seen = false;
       _this.seenNo = true;
       _this.beInvalid = false;
       _this.$emit('refresh')
     },
      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: {
      showFlag3 (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params3 !== null) {
          _this.monitorForm.tableData = _this.Params3;
          _this.monitorForm.tableData.matClassNo2 = _this.monitorForm.tableData.matClassNo;
          _this.monitorForm.tableData.matClassName2 = _this.monitorForm.tableData.matClassName;
          _this.tableType = 1;
          _this.tableTypeDsc = '修改物料分类';
          _this.beInvalid = true;
        } else {
          _this.tableType = 0;
          if (_this.Params.matClassNo !== undefined) {
               _this.seen = true;
               _this.monitorForm.tableData.parentNo = _this.Params.matClassNo;
               _this.monitorForm.tableData.parentName = _this.Params.matClassName;
          } else {
               _this.beInvalid = true;
               _this.seenNo = false;
          }
          _this.tableTypeDsc = '新增物料分类';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_mate{
  width: 550px 
}
.materialConfiguration-alertComponents3{
  .el-dialog{
    width: 35%;
  }
}
</style>
