<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: fzq
 * @LastEditTime: 2022-03-04 18:59:53
 * @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.columnMark')" prop="columns">
        <el-input v-model="dataForm.columns" :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>
    <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: '',
        columns: '',
        layers: '',
        status: '',
        locationType: ''
      },
      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' }
        ]
      }
    }
  },
  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
          })
        } 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')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
