<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 label-width="80px" ref="baseData" :rules="ruleValidate" :model="baseData">
          <el-row>
            <el-col :span='12'>
              <el-form-item prop='tmpName' label='模板名称'>
                <el-input v-model="baseData.tmpName" size="small" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span='12'>
              <el-form-item label='证书'>
                <el-select v-model="baseData.cretNo" filterable clearable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="item in cretNameType" :key="item.cretNo" :label="item.cretName"
                    :value="item.cretNo">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='12'>
              <el-form-item prop='sampleTypeCode' label='样品类型'>
                <el-select v-model="baseData.sampleTypeCode" clearable filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="item in sampleTypeNameType" :key="item.baseCode" :label="item.baseName"
                    :value="item.baseCode">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span='12'>
              <el-form-item  prop="companyNo" label="检验单位">
                <el-select v-model="baseData.companyNo" filterable clearable 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-row>
          <el-row>
            <el-col :span='12'>
              <el-form-item prop="tmpTypeCode" label='模板类型'>
                <el-select v-model="baseData.tmpTypeCode" clearable filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="item in tmpTypeNameType" :key="item.baseCode" :label="item.baseName"
                    :value="item.baseCode">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span='12'>
              <el-form-item label="检验车间">
                <el-select v-model="baseData.deptNo" clearable 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-row>
          <el-row>
            <el-col :span='12'>
              <el-form-item label='备注'>
                <el-input v-model="baseData.memo" size="small" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span='12'>
              <el-form-item label="检验班组">
                <el-select v-model="baseData.sectionNo" style="width:100%" clearable 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-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'],
    data () {
      return {
        dialogTableVisible: false,
        tableTypeDsc: '新增样品模板信息',
        tableType: 0,
        ruleValidate: {
          tmpName: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
          sampleTypeCode: [{ required: true, message: '该项不能为空', trigger: 'change' }],
          companyNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
          tmpTypeCode: [{ required: true, message: '该项不能为空', trigger: 'change' }]
        },
        cretNameType: [],
        baseData:{deptNo:'',sectionNo:''},
        sampleTypeNameType: [],
        companyNameType: [],
        tmpTypeNameType: [],
        sectionNameType:[],
        sectionClass:[]
      }
    },
    mounted () {
      //获取公司
      this.axios.post("pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll",{}, {individualType: 'json'}).then(response => {
        this.companyNameType = response.data
      });
      //获取证书
      this.axios.post('pass/baseManagement/v1/limscrettmps/queryLimsCretTmpList',{validFlag:1}, {individualType: 'json'}).then(res => {
        this.cretNameType = res.data;
      })
      this.getsampleTypeNameType ()
      this.gettmpTypeNameType ()
    },
    methods: {
      cancel () {
        this.$refs['baseData'].resetFields();
        this.tableType = 0;
        this.baseData ={deptNo:'',sectionNo:''},
        this.tableTypeDsc = '新增样品模板信息';
        this.dialogTableVisible = false
      },
      //根据选中的公司，查询部门名称数据
      selectDepartment(val){
        this.sectionNameType = []
        this.companyNameType.find(item =>{
          if(val === item.orgCode){
            this.sectionNameType = item.sysOrgList
            this.baseData.clearFlag ? '' : this.baseData.deptNo = ''
          }
        })
      },
      //根据选中的部门名称，查询班组名称
      selectClass(val){
        this.sectionClass = []
        this.sectionNameType.find(item =>{
          if(val === item.orgCode){
            this.sectionClass = item.sysOrgList
            this.baseData.clearFlag ? '' : this.baseData.sectionNo = ''
          }
        })
      },
      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] : '';
        },
      addData () {
        let _this = this;
        this.$refs['baseData'].validate((valid) => {
          if (valid) {
        this.baseData.createMan = userName;
        this.baseData.createNo = userId
        this.baseData.cretName = this.getCode(this.baseData.cretNo,this.cretNameType,'cretNo','cretName')
        this.baseData.sampleTypeName = this.getCode(this.baseData.sampleTypeCode,this.sampleTypeNameType,'baseCode','baseName')
        this.baseData.companyName = this.getCode(this.baseData.companyNo,this.companyNameType,'orgCode','orgName')
        this.baseData.tmpTypeName = this.getCode(this.baseData.tmpTypeCode,this.tmpTypeNameType,'baseCode','baseName')
        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.axios.post('pass/baseManagement/v1/limsstdsmptmps/addLimsStdSmpTmp', 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.$refs['baseData'].validate((valid) => {
          if (valid) {
            this.baseData.cretName = this.getCode(this.baseData.cretNo,this.cretNameType,'cretNo','cretName')
            this.baseData.sampleTypeName = this.getCode(this.baseData.sampleTypeCode,this.sampleTypeNameType,'baseCode','baseName')
            this.baseData.companyName = this.getCode(this.baseData.companyNo,this.companyNameType,'orgCode','orgName')
            this.baseData.tmpTypeName = this.getCode(this.baseData.tmpTypeCode,this.tmpTypeNameType,'baseCode','baseName')
            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.axios.post('pass/baseManagement/v1/limsstdsmptmps/updateLimsStdSmpTmp/', 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)
            })
            }
        })  
      },
    getsampleTypeNameType () {
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4801}, {individualType: 'json'}).then(res => {
        this.sampleTypeNameType = res.data
      })
    },
    gettmpTypeNameType () {
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4811}, {individualType: 'json'}).then(res => {
        this.tmpTypeNameType = res.data
      })
    },
    closeDialog () {
      this.$refs['baseData'].resetFields();
      this.tableType = 0;
      this.baseData = {deptNo:'',sectionNo:''}
      this.tableTypeDsc = '新增样品模板信息';
      this.dialogTableVisible = false
    },
    },
    watch: {
      showFlag (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params !== null) {
          this.baseData = Object.assign({clearFlag:true},_this.Params[0])
          this.selectDepartment(this.baseData.companyNo)
          this.selectClass(this.baseData.deptNo)
          this.baseData.clearFlag = false
          _this.tableType = 1;
          _this.tableTypeDsc = '修改样品模板信息';
       
        } else {
          _this.tableType = 0;
          _this.tableTypeDsc = '新增样品模板信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_certifi1{
  width: 600px !important
}
</style>
