<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" width="70%">
        <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="strTypeCode"  label="仓库类型">
              <el-select v-model="baseData.strTypeCode" style="width:100%" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in strType" :key="index" :label="item.baseName" :value="item.baseCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="levelNum" label="仓库属性">
              <el-select v-model="baseData.levelNum" filterable style="width:100%" collapse-tags size="small">
                <el-option v-for="(item,index) in levelNumType" :key="index" :label="item.levelNum" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="strDesc" label="仓库描述">
              <el-input v-model="baseData.strDesc" style="width:100%" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item prop="strName" label="仓库名称">
              <el-input v-model="baseData.strName" style="width:100%" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item  label="层高">
              <el-input v-model="baseData.layer" style="width:100%" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item  label="列宽">
              <el-input v-model="baseData.colwidth" style="width:100%" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
           <el-col :span="8">
            <el-form-item prop="companyNo" label="公司名称">
              <el-select v-model="baseData.companyNo" filterable style="width:100%" collapse-tags @change="selectDepartment" size="small">
                <el-option v-for="item in companyNameType" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="sectionName" label="部门名称">
             <el-select v-model="baseData.deptNo" filterable style="width:100%" @change="selectClass" collapse-tags size="small" >
                <el-option v-for="item in sectionNameType" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="deptName" label="班组名称">
             <el-select v-model="baseData.sectionNo" style="width:100%" filterable collapse-tags size="small">
                <el-option v-for="item in sectionClass" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                </el-option>
              </el-select>
            </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" 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: ['showFlag', 'Params', 'Params2'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增仓库信息',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            sectionNameTypeIndex: 0,
            levelNumTypeIndex: 0,
            strTypeNameTypeIndex: 0,
            companyNameTypeIndex: 0,
            deptNameTypeIndex: 0,
            levelNumType: [
              {id: '0', levelNum: '仓库'},
              {id: '1', levelNum: '货架'},
              {id: '2', levelNum: '货位'}
            ],
            strType: [
              // {strTypeCode: '483301', strTypeName: '留样库'},
              // {strTypeCode: '483302', strTypeName: '材料库'}
            ],
            companyNameType: [],
            deptNameType: [],
            sectionClass:[],
            sectionNameType: [],
            tableData: [],
            baseData: {},

            ruleValidate: {
              strName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              strTypeCode:[{ required: true, message: '该项不能为空', trigger: 'change' }],
              levelNum:[{ required: true, message: '该项不能为空', trigger: 'change' }],
              companyNo:[{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      //获取公司
  this.axios.post("pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll",{}, {individualType: 'json'}).then(response => {
    this.companyNameType = response.data
  });
      this.getStrTypeList()
    },
    methods: {
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.baseData = {}
        _this.dialogTableVisible = false
      },
      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] : '';
      },
    //根据选中的公司，查询部门名称数据
    selectDepartment(val){
      this.sectionNameType = []
      this.companyNameType.find(item =>{
        if(val === item.orgCode){
          this.sectionNameType = item.sysOrgList
        }
      })
    },
    //根据选中的部门名称，查询班组名称
    selectClass(val){
      this.sectionClass = []
      this.sectionNameType.find(item =>{
        if(val === item.orgCode){
          this.sectionClass = item.sysOrgList
        }
      })
    },
      addData () {
        let _this = this;
        _this.tableData = [];
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             _this.baseData.companyName = this.getCode(this.baseData.companyNo,this.companyNameType,'orgCode','orgName')
             _this.baseData.deptName = this.getCode(this.baseData.deptNo,this.sectionNameType,'orgCode','orgName')
             _this.baseData.sectionName = this.getCode(this.baseData.sectionNo,this.sectionClass,'orgCode','orgName')
             _this.baseData.strTypeName = this.getCode(this.baseData.strTypeCode,this.strType,'baseCode','baseName')
             _this.axios.post('pass/baseManagement/v1/limsbasestorages/', [_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 () {
        let _this = this;
        _this.tableData = [];
        _this.$refs['baseData'].validate((valid) => {
        if (valid) {
          _this.baseData.companyName = this.getCode(this.baseData.companyNo,this.companyNameType,'orgCode','orgName')
          _this.baseData.deptName = this.getCode(this.baseData.deptNo,this.sectionNameType,'orgCode','orgName')
          _this.baseData.sectionName = this.getCode(this.baseData.sectionNo,this.sectionClass,'orgCode','orgName')
          _this.baseData.strTypeName = this.getCode(this.baseData.strTypeCode,this.strType,'baseCode','baseName')
        _this.axios.put('pass/baseManagement/v1/limsbasestorages/', _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>
         )
      },
      getStrTypeList () {
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4833}, {individualType: 'json'}).then(res => {
              this.strType = res.data
            }).catch(e => {
              // this.dataLoading = false;
            });
      },
     closeDialog () {
       let _this = this;
       _this.tableData = [];
       _this.baseData = {
              colwidth: '',
              layer: '',
              strDesc: '',
              strName: '',
              sortNo: '',
              memo: ''
        }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
     },
      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;
        if (_this.Params !== null) {
          this.baseData = Object.assign({},this.Params[0])
          console.log(this.baseData,'this.baseData');
          _this.tableType = 1;
          _this.tableTypeDsc = '修改仓库信息';
          _this.seen = false;
          _this.beInvalid = true;
          this.selectDepartment(this.baseData.companyNo)
          this.selectClass(this.baseData.deptNo)
        } else {
          _this.tableData = [];
          _this.baseData = {}
          if (_this.Params2.strNo !== null || _this.Params2.strNo !== undefined) {
            _this.baseData.sortNo = _this.Params2.strNo
          }
          _this.tableType = 0;
          _this.seenNo = false;
          _this.tableTypeDsc = '新增仓库信息';
        }
        _this.dialogTableVisible = true;
      }
    }
}
</script>
<style lang="less">
.customWidth_ware{
  width: 500px !important
}
</style>
