<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" customClass="customWidth_standa">
        <div slot="title" class="titleBox">
        <i :class="tableType === 0? 'el-icon-document':'el-icon-edit-outline'" style="font-size: 20px"></i>
        <span style="margin-left:10px;font-size:18px">{{tableTypeDsc}}</span>
        </div>
        <el-form :model="formData"  label-width="90px" :rules="monitorForm.monitorRules" ref="monitorForm">
          <el-row>
            <el-col :span='12'>
              <el-form-item prop="stdItemName" label="标准项目名称">
              <el-input v-model="formData.stdItemName" style="width:100%" size="small"></el-input>
             </el-form-item>
            </el-col>
            <el-col :span='12'>
              <el-form-item prop="stdItemDesc" label="标准项目描述">
                <el-input v-model="formData.stdItemDesc" style="width:100%" size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='12'>
              <el-form-item prop="dspOrder" label="显示顺序">
                <el-input v-model="formData.dspOrder" style="width:100%" size="small" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span='12'>
              <el-form-item prop="unitNameTypeIndex" label="度量单位">
               <el-select v-model="formData.unitNo" clearable filterable style="width:100%" collapse-tags size="small">
                  <el-option v-for="(item,index) in unitNameType" :key="index" :label="item.unitName"
                    :value="item.unitNo">
                  </el-option>
                </el-select>
                </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='12'>
              <el-form-item prop="dataTypeCode" label="数据类型">
               <el-select v-model="formData.dataTypeCode" clearable filterable style="width:100%" collapse-tags size="small">
                  <el-option v-for="(item,index) in dataTypeNameType" :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="defaultValue" label="默认值">
               <el-input v-model="formData.defaultValue" style="width:100%" size="small" placeholder="请输入"></el-input>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='12'>
              <el-form-item prop="colNo" label="列字段">
               <el-input v-model="formData.colNo" size="small" style="width:100%" placeholder="请输入"></el-input>
               </el-form-item>
            </el-col>
            <el-col :span='12'>
              <el-form-item prop="colName" label="列字段名">
               <el-input v-model="formData.colName" size="small" style="width:100%" placeholder="请输入"></el-input>
               </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='24'>
              <el-form-item prop="colName" label="备注">
                <el-input v-model="formData.memo" size="small" style="width:100%" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button type="goon" icon="el-icon-check" size="mini" @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,
          stdItemDesc: '',
          dspOrder: '',
          defaultValue: '',
          colNo: '',
          colName: '',
          formData: {
          },
          unitNameTypeIndex: 0,//度量单位
          dataTypeNameTypeIndex: 0,//数据类型
          unitNameType: [],
          dataTypeNameType: [],
          monitorForm: {
          tableData: [{}],
          monitorRules: {
            stdItemName: [{ required: true, message: '该项不能为空', trigger:'blur'}],
            dataTypeCode: [{ required: true, message: '该项不能为空', trigger:'change'}]
          }
          }
        }
    },
    mounted () {
      this.axios.post("pass/baseManagement/v1/limsbaseunits/queryLimsBaseUnitPage",{object:{validFlag:1},pageIndex:1,pageSize:9999}, {individualType: 'json'}).then(res => {
        this.unitNameType = res.data.list;
      })
      this.axios.post('pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode',{validFlag:1,baseCode:4825},{individualType: 'json'}).then(res => {
        this.dataTypeNameType = res.data;
      })
    },
    methods: {
      cancel () {
        let _this = this;
        this.$refs['monitorForm'].resetFields();
        this.formData = {}
        this.tableTypeDsc = '新增标准内容信息',
        this.tableType= 0
        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] : '';
      },
      addData () {
        let _this = this;
         _this.$refs['monitorForm'].validate((valid) => {
          if (valid) {
            this.formData.unitName = this.getCode(this.formData.unitNo,this.unitNameType,'unitNo','unitName')
            this.formData.dataTypeName = this.getCode(this.formData.dataTypeCode,this.dataTypeNameType,'baseCode','baseName')
            _this.axios.post('pass/baseManagement/v1/limsstditems/saveLimsStdItem', this.formData, {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['monitorForm'].validate((valid) => {
        if (valid) {
        this.formData.unitName = this.getCode(this.formData.unitNo,this.unitNameType,'unitNo','unitName')
        this.formData.dataTypeName = this.getCode(this.formData.dataTypeCode,this.dataTypeNameType,'baseCode','baseName')
        _this.axios.post('pass/baseManagement/v1/limsstditems/saveLimsStdItem', this.formData, {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:20px" onClick={() => this.addRow()}>
             <i class="el-icon-plus" style="font-size:20px; color:rgb(18, 52, 243)"></i>
             </div>
         )
      },
    closeDialog () {
      this.$refs['monitorForm'].resetFields();
      this.formData = {}
      this.tableTypeDsc = '新增标准项目',
      this.tableType= 0
    },
      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.formData = Object.assign({},_this.Params[0])
          _this.tableType = 1;
          _this.tableTypeDsc = '修改标准项目';
        } else {
          _this.tableType = 0;
          _this.tableTypeDsc = '新增标准项目';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_standa{
  width: 600px !important
}
</style>
