<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: fzq
 * @LastEditTime: 2022-03-15 09:54:52
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="130px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item :label="$t('module.basicData.cache.LocationName')" prop="locationName">
        <el-input v-model="dataForm.locationName" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.LocationName')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.cache.LocationCode')" prop="code">
        <el-input v-model="dataForm.code" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.LocationCode')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.cache.anotherName')" prop="locationNameAlias">
        <el-input v-model="dataForm.locationNameAlias" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.anotherName')])" clearable />
      </el-form-item>
      <!-- <el-form-item :label="$t('module.basicData.cache.rowMark')" prop="layers">
        <el-input v-model="dataForm.layers" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.rowMark')])" clearable />
      </el-form-item> -->
      <el-form-item :label="$t('module.basicData.cache.rowMark')" prop="layers">
        <el-input-number v-model="dataForm.layers" :step="1" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.rowMark')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.cache.columnMark')" prop="columns">
        <el-input-number v-model="dataForm.columns" :step="1" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.columnMark')])" clearable />
      </el-form-item>
      <!-- <el-form-item :label="$t('module.basicData.cache.locationType')" prop="locationType">
        <el-input v-model="dataForm.locationType" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.locationType')])" clearable />
      </el-form-item> -->
      <!-- <el-form-item :label="$t('module.basicData.cache.status')" prop="status">
        <el-input v-model="dataForm.status" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.status')])" clearable />
      </el-form-item> -->
      <el-form-item :label="$t('module.basicData.cache.status')" prop="status">
        <el-select v-model="dataForm.status">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ 'btn.confirm' | i18nFilter }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { locationDetail, locationUpdate, locationAdd, locationCode } from '@/api/basicData/Cache/location'

export default {
  props: {
    shelfId: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        locationName: '',
        code: '',
        locationNameAlias: '',
        rowNum: '',
        status: null,
        locationType: ''
      },
      options: [
        {
          value: 1,
          label: 'Working Port'
        },
        {
          value: 2,
          label: 'Buffer Port'
        },
        {
          value: 3,
          label: 'Exception Port'
        }
      ],
      dataRule: {
        name: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.cache.LocationName')]), trigger: 'blur' }
        ],
        code: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.cache.LocationCode')]), trigger: 'blur' }
        ],
        layers: [
          { pattern: /^[1-9]\d*$/, message: this.$t('module.basicData.cache.warning'), trigger: 'blur' }
        ],
        columns: [
          { pattern: /^[1-9]\d*$/, message: this.$t('module.basicData.cache.warning'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          locationDetail(this.dataForm.id).then(res => {
            this.dataForm = res.data
            // console.log(this.dataForm)
          })
        } else {
          locationCode().then(res => {
            this.dataForm.code = res.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            'locationName': this.dataForm.locationName,
            'code': this.dataForm.code,
            'locationNameAlias': this.dataForm.locationNameAlias,
            'columns': this.dataForm.columns,
            'layers': this.dataForm.layers,
            'shelfId': this.shelfId,
            'id': this.dataForm.id,
            'status': this.dataForm.status,
            'locationType': this.dataForm.locationType
          }
          if (this.dataForm.id) {
            locationUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            locationAdd(data).then(res => {
              console.log(data)
              console.log(res)
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
    // isInt(value){
    //   for(var i = 0; i < value.length; i++) {
    //     if(charAt[i] == '.'){
    //     }
    //   }
    // }
    // isInt(value) {
    //   let zero = /^0+\d*$/ // 过滤以0开头的数据(不含小数点)
    //   let dublue = /^0{2,}\.\d+$/ // 过滤小数点前有两个以上0的数字
    //   let point = /^\d+\.?\d+$/ //  以数字开头,可以允许出现一次或0次小数点,以数字结尾(这里的数字必须有两个)
    //   let reg = /^[1-9]{1}$/ // 匹配只有一个数字的情况
    //   if (!value) {
    //     return false
    //   }
    //   if (zero.test(value) || dublue.test(value)) { // 首先过滤掉错误的数据
    //     return false
    //   } else if (point.test(value) || reg.test(value)) { // 匹配数据,如果输入的数字只有一位数时用reg匹配
    //     return true
    //   } else {
    //     return false
    //   }
    // }
  }
}
</script>
