<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="merchName" label="客商名称">
              <el-input v-model="baseData.merchName" style="width: 100%" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="merchChnAbbr" label="客商简称">
              <el-input v-model="baseData.merchChnAbbr" style="width: 100%" size="small"></el-input>
            </el-form-item>
          </el-col>
           </el-row>
          <el-row>
          <el-col :span="12">
            <el-form-item prop="merchTypeName" label="客商类型">
              <el-select v-model="merchTypeNameTypeIndex" filterable style="width: 100%" collapse-tags size="small">
                <el-option v-for="(item,index) in merchTypeNameType" :key="index" :label="item.baseName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="merchAtrrName" label="客商属性">
             <el-select v-model="merchAtrrNameTypeIndex" filterable style="width: 100%" collapse-tags size="small">
                <el-option v-for="(item,index) in merchAtrrNameType" :key="index" :label="item.baseName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         </el-row>
          <el-row>
          <el-col :span="12">
            <el-form-item prop="merchLinkman" label="联系人">
              <el-input v-model="baseData.merchLinkman" style="width: 100%" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="merchTel" label="联系电话">
              <el-input v-model="baseData.merchTel" 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'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增客商信息',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            merchTypeNameTypeIndex: 0,
            merchAtrrNameTypeIndex: 0,
            merchTypeNameType: [],
            merchAtrrNameType: [],
            tableData: [],
            baseData: {
              merchName: '',
              merchChnAbbr: '',
              merchLinkman: '',
              merchTel: ''
            },
            ruleValidate: {
              //merchTypeName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              merchName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // merchAtrrName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      this.getMerchTypeNameType (),
      this.getMerchAtrrNameType ()
    },
    methods: {
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.merchTypeNameTypeIndex = 0,
        _this.merchAtrrNameTypeIndex = 0,
        _this.baseData = {
              merchName: '',
              merchChnAbbr: '',
              merchLinkman: '',
              merchTel: ''
        }
        _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.tableData = [];
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.dialogTableVisible = false;
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             _this.baseData.merchAtrrCode = _this.merchAtrrNameType[_this.merchAtrrNameTypeIndex].baseCode;
             _this.baseData.merchAtrrName = _this.merchAtrrNameType[_this.merchAtrrNameTypeIndex].baseName;
             _this.baseData.merchTypeCode = _this.merchTypeNameType[_this.merchTypeNameTypeIndex].baseCode;
             _this.baseData.merchTypeName = _this.merchTypeNameType[_this.merchTypeNameTypeIndex].baseName;
             _this.tableData.push(_this.baseData)
             _this.axios.post('pass/baseManagement/v1/limsmerchantsinfos/', _this.tableData, {individualType: 'json'}).then(res => {
                if (res.succeed) {
                //  this.$message.success('添加成功')
                 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.tableData = [];
        _this.$refs['baseData'].validate((valid) => {
        if (valid) {
        _this.dialogTableVisible = false;
        _this.baseData.updateMan = userName;
        _this.baseData.updateNo = userId;
        _this.baseData.merchAtrrCode = _this.merchAtrrNameType[_this.merchAtrrNameTypeIndex].baseCode;
        _this.baseData.merchAtrrName = _this.merchAtrrNameType[_this.merchAtrrNameTypeIndex].baseName;
        _this.baseData.merchTypeCode = _this.merchTypeNameType[_this.merchTypeNameTypeIndex].baseCode;
        _this.baseData.merchTypeName = _this.merchTypeNameType[_this.merchTypeNameTypeIndex].baseName;
        _this.tableData.push(_this.baseData)
        _this.axios.put('pass/baseManagement/v1/limsmerchantsinfos/', _this.baseData, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            // this.$message.success('修改成功')
            this.open('修改成功', 'success', 1200)
            _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 () {
       let _this = this;
       _this.tableData = [];
       _this.baseData = {
              merchName: '',
              merchChnAbbr: '',
              merchLinkman: '',
              merchTel: ''
        }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
       _this.$emit('refresh')
     },
      getMerchTypeNameType () {
        this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4832}, {individualType: 'json'}).then(res => {
              this.merchTypeNameType = res.data;
            }).catch(e => {
              // this.dataLoading = false;
            });
     },
    getMerchAtrrNameType () {
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4831}, {individualType: 'json'}).then(res => {
              this.merchAtrrNameType = res.data;
            }).catch(e => {
              // this.dataLoading = false;
            });
     },
      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.merchAtrrNameType.length; j++) {
          if (_this.merchAtrrNameType[j].baseName === _this.baseData.merchAtrrName) {
             _this.merchAtrrNameTypeIndex = j;
          }
          }
          for (let j = 0; j < _this.merchTypeNameType.length; j++) {
          if (_this.merchTypeNameType[j].baseName === _this.baseData.merchTypeName) {
             _this.merchTypeNameTypeIndex = j;
          }
          }
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.tableTypeDsc = '新增客商信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>
