<template>
    <div class="examination">
      <el-dialog @close="closeDialog" :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="80px" :rules="ruleValidate" :model="baseData">
          <el-row>
            <el-col :span="12">
              <el-form-item prop="isoName" label="体系名称">
                <el-input v-model="baseData.isoName" style="width: 100%" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="isoDesc" label="体系评审说明">
                <el-input v-model="baseData.isoDesc" style="width: 100%" size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="isoTypeName" label="体系类型">
                <el-select v-model="baseData.isoTypeNo" filterable style="width: 100%" collapse-tags size="small">
                  <el-option v-for="(item,index) in isoTypeNameType" :key="index" :label="item.baseName" :value="item.baseCode">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="companyName" label="管理单位">
                <el-select v-model="baseData.companyNo" filterable style="width: 100%" collapse-tags size="small" @change="selectDepartment">
                  <el-option v-for="(item,index) in companyNameType" :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="12">
              <el-form-item prop="deptName" label="管理车间">
                <el-select v-model="baseData.deptNo" filterable style="width: 100%" collapse-tags size="small" @change="selectClass">
                  <el-option v-for="(item,index) in deptNameType" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="sectionName" label="管理班组">
                <el-select v-model="baseData.sectionNo" filterable style="width: 100%" collapse-tags size="small">
                  <el-option v-for="(item,index) in sectionNameType" :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="12">
              <el-form-item prop="filePath" label="体系路径">
                <el-input v-model="baseData.filePath" style="width: 100%" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <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,
            isoTypeNameIndex: 0,
            companyNameIndex: 0,
            deptNameIndex:0,
            sectionNameIndex:0,
            merchTypeNameType: [],
            isoTypeNameType: [],
            companyNameType:[],
            deptNameType:[],
            sectionNameType:[],
            tableData: [],
            baseData: {
              isoName: '',
              isoDesc:'',
              filePath: '',
              companyNo:'',
              deptNo:'',
              sectionNo:'',
              isoTypeNo:''
            },
            ruleValidate: {
              //merchTypeName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              merchName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // merchAtrrName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      this.getIsoNameType();
      this.axios
        .post("pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll", {}, { individualType: "json" }).then((response) => {
          this.companyNameType = response.data;
        });
    },
    methods: {
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.merchTypeNameTypeIndex = 0,
        _this.merchAtrrNameTypeIndex = 0,
        _this.baseData = {
          isoName: '',
          isoDesc:'',
          filePath: '',
          companyNo:'',
          deptNo:'',
          sectionNo:'',
          isoTypeNo:''
        }
        _this.dialogTableVisible = false
        _this.$emit('refresh')
      },
      open(mag, typ, time) {
        this.$message({
          showClose: true,
          message: mag,
          type: typ,
          duration: time
        });
      },
      addData () {
        let _this = this;
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.dialogTableVisible = false;
            let obj = {
              createMan:userName,
              createNo:userId,
              createTime:new Date(),
              isoName: this.baseData.isoName,
              isoDesc:this.baseData.isoDesc,
              filePath: this.baseData.filePath,
              companyNo:this.baseData.companyNo,
              companyName: this.getCode(this.baseData.companyNo,this.companyNameType,"orgCode","orgName"),
              deptNo:this.baseData.deptNo,
              deptName:this.getCode(this.baseData.deptNo,this.deptNameType,"orgCode","orgName"),
              sectionNo:this.baseData.sectionNo,
              sectionName:this.getCode(this.baseData.sectionNo,this.sectionNameType,"orgCode","orgName"),
              isoTypeNo:this.baseData.isoTypeNo,
              isoTypeName:this.getCode(this.baseData.isoTypeNo,this.isoTypeNameType,"baseCode","baseName"),
              validFlag:'1',
              stateFlag:'0'
            }
            _this.axios.post('pass/baseManagement/v1/limsisodatas/addLimsIsoData', obj, {individualType: 'json'}).then(res => {
              if (res.succeed) {
                this.open('添加成功', 'success', 1200)
                _this.$emit('refresh')
              } else {
                this.$message.error(res.message)
              }
            }).catch(e => {
              this.$message.error(e.message)
            })
         }
       })
      },
      modifyData () {
        let _this = this;
        _this.$refs['baseData'].validate((valid) => {
        if (valid) {
          let obj = {
            createMan:userName,
            createNo:userId,
            createTime:new Date(),
            merchName:this.baseData.merchName,
            merchTypeCode:_this.merchTypeNameType[_this.merchTypeNameTypeIndex].baseCode,
            merchTypeName:_this.merchTypeNameType[_this.merchTypeNameTypeIndex].baseName,
            merchChnAbbr:this.baseData.merchChnAbbr,
            merchAtrrName:this.merchAtrrNameType[this.merchAtrrNameTypeIndex].baseName,
            merchAtrrCode:this.merchAtrrNameType[this.merchAtrrNameTypeIndex].baseCode,
            merchLinkman:this.baseData.merchLinkman,
            merchTel:this.baseData.merchTel
          }
        _this.axios.put('pass/baseManagement/v1/limsisomerchs/'+this.baseData.id,obj, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            // this.$message.success('修改成功')
            this.open('修改成功', 'success', 1200)
             this.dialogTableVisible =false
            _this.$emit('refresh')
           } else {
            this.$message.error(res.message)
           }
           }).catch(e => {
            this.$message.error(e.message)
          })
        }
        })
      },
      getCode(code, arr, codeKey, nameKey) {
        if (!code) {
          return "";
        }
        let targetRow = arr.find((item) => {
          return item[codeKey] === code;
        });
        return targetRow ? targetRow[nameKey] : "";
      },
      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.tableData = [];
       _this.baseData = {
         isoName: '',
         isoDesc:'',
         filePath: '',
         companyNo:'',
         deptNo:'',
         sectionNo:'',
         isoTypeNo:''
        }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
       _this.$emit('refresh')
     },
      selectDepartment (val) {
        this.sectionNameType = [];
        this.deptNameType = [];
        this.companyNameType.find((item) => {
          if (val === item.orgCode) {
            this.deptNameType = item.sysOrgList;
            this.baseData.deptNo = ''
            this.baseData.sectionNo = ''
          }
        });
      },
      // 根据选中的车间名称，查询班组名称
      selectClass(val) {
        this.sectionNameType = [];
        this.deptNameType.find((item) => {
          if (val === item.orgCode) {
            this.sectionNameType = item.sysOrgList;
          }
        });
      },
      getIsoNameType () {
        this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4832}, {individualType: 'json'}).then(res => {
              this.isoTypeNameType = res.data;
            }).catch(e => {
            });
      },
      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.companyNameType.length; j++) {
             if (this.companyNameType[j].orgCode === this.baseData.companyNo){
               this.companyNameTypeIndex = j;
             }
          }
          //车间
          this.deptNameType = this.companyNameType.orgList;
          for (let j = 0; j < _this.deptNameType.length; j++) {
            if (_this.deptNameType[j].orgCode === _this.baseData.deptNo) {
              this.deptNameIndex = j;
            }
          }
          //班组
          this.sectionNameType = this.deptNameType.orgList
          for (let j = 0; j < _this.sectionNameType.length; j++) {
            if (_this.sectionNameType[j].orgCode === _this.baseData.sectionNo) {
              this.sectionNameIndex = j;
            }
          }
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.tableTypeDsc = '新增客商信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>
