<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" customClass="customWidth_pro">
        <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:8px;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 label="项目属性">
                  <el-select :readonly="beInvalid" :disabled="beInvalid" v-model="itemAttrIndex" filterable collapse-tags size="small">
                    <el-option v-for="(item,index) in itemAttrType" :key="index" :label="item.itemAttr" :value="index">
                    </el-option>
                  </el-select>
              </el-form-item>
           </el-col>
           <el-col :span="9">
              <el-form-item label="项目类型">
                  <el-select :readonly="beInvalid" :disabled="beInvalid" v-model="itemTypeNameIndex" filterable collapse-tags size="small">
                    <el-option v-for="(item,index) in itemTypeNameType" :key="index" :label="item.baseName" :value="index">
                    </el-option>
                  </el-select>
              </el-form-item>
           </el-col>
            <el-col :span="4">
              <el-button type="goon" size="mini" @click="getItemName" :readonly="beInvalid" :disabled="beInvalid">过滤项目名称</el-button>
           </el-col>
           <el-col :span="2">
               <el-button icon="el-icon-refresh" type="info" plain size="mini" @click="reset" :readonly="beInvalid" :disabled="beInvalid">重置</el-button>
           </el-col>
         </el-row>
         <el-row>
           <el-col :span="8">
            <el-form-item prop="itemNameTypeIndex" label="项目名称">
              <el-select v-if="isTest" :readonly="beInvalid" :disabled="beInvalid" v-model="baseData.itemNameTypeIndex" filterable collapse-tags size="small"  @change="selectTriggerI(baseData.itemNameTypeIndex)">
                <el-option v-for="(item,index) in itemNameType" :key="index" :label="item.testItemName" :value="index">
                </el-option>
              </el-select>
              <el-select v-if="!isTest" :readonly="beInvalid" :disabled="beInvalid" v-model="baseData.itemNameTypeIndex" filterable collapse-tags size="small"  @change="selectTriggerI(baseData.itemNameTypeIndex)">
                <el-option v-for="(item,index) in itemNameType" :key="index" :label="item.anlyItemName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="itemCostOut" label="结算费用">
            <el-input v-model="baseData.itemCostOut" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="itemCostInn" label="成本费用">
            <el-input v-model="baseData.itemCostInn" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="8">
            <el-form-item prop="companyName" 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="8">
            <el-form-item prop="coeff" label="权重系统">
            <el-input v-model="baseData.coeff" 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'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增项目价格信息',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            companyNameTypeIndex: 0,
            deptNameTypeIndex: 0,
            sectionNameTypeIndex: 0,
            transportTypeNameTypeIndex: 0,
            itemAttrIndex: 0,
            itemTypeNameIndex: 0,
            isTest: false,
            itemAttrType: [
              {id: '0', itemAttr: '分析项目'},
              {id: '1', itemAttr: '测试项目'}
            ],
            itemTypeNameType: [],
            itemNameType: [],
            companyNameType: [],
            deptNameType: [],
            sectionNameType: [],
            sectionClass:[],
            transportTypeNameType: [
              {transportTypeCode: '483801', transportTypeName: '运输类型1'},
              {transportTypeCode: '483802', transportTypeName: '运输类型2'},
              {transportTypeCode: '483803', transportTypeName: '运输类型3'}
            ],
            tableData: [],
            baseData: {
              icNo: '',
              itemNo: '',
              itemCostOut: '',
              itemCostInn: '',
              coeff: '',
              itemNameTypeIndex: '',
            },
            ruleValidate: {
              //itemName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              itemNameTypeIndex: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // transportTypeName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      //获取公司
  this.axios.post("pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll",{}, {individualType: 'json'}).then(response => {
    this.companyNameType = response.data
  });
      this.getItemTypeName ()
    },
    methods: {
      cancel () {
        let _this = this;
        _this.itemNameType = [];
        // _this.itemNameTypeIndex = ''
        _this.companyNameTypeIndex = 0,
        _this.deptNameTypeIndex = 0,
        _this.sectionNameTypeIndex = 0,
        _this.transportTypeNameTypeIndex = 0,
        _this.itemAttrIndex = 0,
        _this.itemTypeNameIndex = 0,
        _this.baseData = {
              icNo: '',
              itemNo: '',
              itemCostOut: '',
              itemCostInn: '',
              coeff: '',
              itemNameTypeIndex: '',
        }
        _this.dialogTableVisible = false
        _this.$emit('refresh')
      },
      reset () {
        let _this = this;
        _this.itemNameType = [];
        // _this.itemNameTypeIndex = ''
        _this.companyNameTypeIndex = 0,
        _this.deptNameTypeIndex = 0,
        _this.sectionNameTypeIndex = 0,
        _this.transportTypeNameTypeIndex = 0,
        _this.itemAttrIndex = 0,
        _this.itemTypeNameIndex = 0,
        _this.baseData = {
              icNo: '',
              itemNo: '',
              itemCostOut: '',
              itemCostInn: '',
              itemNameTypeIndex: '',
              coeff: '',
        }
      },
      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
        }
      })
    },
       getItemName () {
        let _this = this;
        _this.itemTypeName = _this.itemTypeNameType[_this.itemTypeNameIndex].baseName;
        _this.itemNameTypeIndex = ''
        if (_this.itemAttrIndex === 0) {
           this.axios.get('pass/baseManagement/v1/limsstdanlyitems/' + '?itemTypeName=' + _this.itemTypeName)
            .then(res => {
              this.itemNameType = res.data.list;
              this.isTest = false;
            })
        } else if (_this.itemAttrIndex === 1) {
          this.axios.get('pass/baseManagement/v1/limsstdtestitems/' + '?itemTypeName=' + _this.itemTypeName)
            .then(res => {
              this.itemNameType = res.data.list;
              this.isTest = true;
            })
        }
      },
      selectTriggerI (val) {
        let _this = this;
        if (_this.isTest === true) {
          _this.baseData.itemNo = _this.itemNameType[val].testItemNo
        } else if (_this.isTest === false) {
          _this.baseData.itemNo = _this.itemNameType[val].anlyItemNo
        }
      },
      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.itemAttr = _this.itemAttrType[_this.itemAttrIndex].id;
             _this.baseData.itemTypeCode = _this.itemTypeNameType[_this.itemTypeNameIndex].baseCode;
             _this.baseData.itemTypeName = _this.itemTypeNameType[_this.itemTypeNameIndex].baseName;
             if (_this.isTest === true) {
               _this.baseData.itemName = _this.itemNameType[_this.baseData.itemNameTypeIndex].testItemName;
             } else if (_this.isTest === false) {
               _this.baseData.itemName = _this.itemNameType[_this.baseData.itemNameTypeIndex].anlyItemName;
             }
             _this.tableData.push(_this.baseData)
             _this.axios.post('pass/baseManagement/v1/limsbaseitemcosts/', _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.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.itemAttr = _this.itemAttrType[_this.itemAttrIndex].id;
        // _this.baseData.itemTypeCode = _this.itemTypeNameType[_this.itemTypeNameIndex].baseCode;
        // _this.baseData.itemTypeName = _this.itemTypeNameType[_this.itemTypeNameIndex].baseName;
        // if (_this.isTest === true) {
        //   _this.baseData.itemName = _this.itemNameType[_this.baseData.itemNameTypeIndex].testItemName;
        // } else if (_this.isTest === false) {
        //   _this.baseData.itemName = _this.itemNameType[_this.baseData.itemNameTypeIndex].anlyItemName;
        // }
        _this.tableData.push(_this.baseData)
        _this.axios.put('pass/baseManagement/v1/limsbaseitemcosts/', _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.itemNameType = [];
      // _this.itemNameTypeIndex = ''
      _this.companyNameTypeIndex = 0,
      _this.deptNameTypeIndex = 0,
      _this.sectionNameTypeIndex = 0,
      _this.transportTypeNameTypeIndex = 0,
      _this.itemAttrIndex = 0,
      _this.itemTypeNameIndex = 0,
      _this.baseData = {
            icNo: '',
            itemNo: '',
            itemCostOut: '',
            itemCostInn: '',
            itemNameTypeIndex: '',
            coeff: '',
      }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
       _this.$emit('refresh')
     },
      getItemTypeName () {
        this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4804}, {individualType: 'json'}).then(res => {
              this.itemTypeNameType = res.data
            })
     }
    },
    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.transportTypeNameType.length; j++) {
          // if (_this.transportTypeNameType[j].transportTypeName === _this.baseData.transportTypeName) {
          //    _this.transportTypeNameTypeIndex = j;
          // }
          // }
          // for (let j = 0; j < _this.itemNameType.length; j++) {
          // if (_this.itemNameType[j].itemName === _this.baseData.itemName) {
          //    _this.itemNameTypeIndex = j;
          // }
          // }
          _this.baseData.itemNameTypeIndex = _this.baseData.itemName;
         this.selectDepartment(this.baseData.companyNo)
          this.selectClass(this.baseData.deptNo)
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.tableTypeDsc = '新增项目价格信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_pro{
  width: 850px !important
}
</style>

