<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: fzq
 * @LastEditTime: 2022-03-10 19:38:31
 * @Description:
-->
<template>
  <!-- 新增/编辑识别 :title="!dataForm.areaId ? 'btn.add' : 'btn.edit' | i18nFilter" -->
  <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.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>
      <!-- 选择货架code的下拉框
      <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="total">
        <el-input-number v-model="dataForm.total" :step="1" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.StorageQuantity')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.cache.rowNum')" prop="rowNum">
        <el-input-number v-model="dataForm.rowNum" :step="1" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.rowNum')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.cache.columnNum')" prop="columnNum">
        <el-input-number v-model="dataForm.columnNum" :step="1" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.cache.columnNum')])" 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'

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
    //   }
    //   shelfList(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
    //       })
    //     }
    //   })
    // },
    init(id) {
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          // console.log(this.dataForm)
          shelfDetail(this.dataForm.id).then(res => {
            // console.log(res)
            this.dataForm = res.data
          })
        } else {
          this.dataForm.shelfName = ''
          shelfCode().then(res => {
            this.dataForm.code = res.data
          })
        }
      })
      // console.log(this.dataForm)
    },
    // getClose() {
    //   this.dataForm.shelfName = ''
    //   console.log(this.dataForm)
    // },
    // 表单提交
    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 => {
              // console.log(res)
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
