<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: gtz
 * @LastEditTime: 2021-04-16 15:01:45
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.areaId ? '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.ShelfName')" prop="name">
        <el-input v-model="dataForm.shelfName" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.ShelfName')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.cache.ShelfCode')" prop="code">
        <el-input v-model="dataForm.code" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.ShelfCode')])" clearable />
      </el-form-item>
      <el-form-item v-if="isPage" :label="$t('module.basicData.cache.AreaName')" prop="areaId">
        <el-select v-model="dataForm.areaId" filterable :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.AreaName')])" clearable>
          <el-option
            v-for="item in areaArr"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('module.basicData.cache.StorageQuantity')" prop="shelfNumber">
        <el-input-number v-model="dataForm.shelfNumber" :step="1" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.StorageQuantity')])" 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 { shelfDetail, shelfUpdate, shelfAdd, shelfCode } from '@/api/basicData/Cache/shelf'
import { areaList } from '@/api/basicData/Cache/area'

export default {
  props: {
    areaId: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  data() {
    return {
      visible: false,
      isPage: false,
      dataForm: {
        abbr: 'n',
        id: 0,
        areaId: '',
        shelfName: '',
        code: '',
        shelfNumber: '',
        total: ''
      },
      areaArr: [],
      dataRule: {
        shelfName: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.cache.ShelfName')]), trigger: 'blur' }
        ],
        code: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.cache.ShelfCode')]), trigger: 'blur' }
        ],
        areaId: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.cache.AreaName')]), trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init(id, isPage) {
      this.isPage = isPage || false
      this.dataForm.id = id || ''
      if (!this.isPage) {
        this.dataForm.areaId = this.areaId
      }
      this.areaArr.splice(0, this.areaArr.length)
      const params = {
        current: 1,
        size: 500
      }
      areaList(params).then(response => {
        if (response.data.records) {
          this.areaArr = response.data.records
        }
      })
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          shelfDetail(this.dataForm.id).then(res => {
            this.dataForm = res.data
          })
        } else {
          shelfCode().then(res => {
            this.dataForm.code = res.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = this.dataForm
          console.log(data)
          if (this.dataForm.id) {
            shelfUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            shelfAdd(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
