<!--
  @description: 库存地址编辑录入
  @Modifier: 郝定康
  @routerPath:
  @date: 2021-02-23 16:01:33
-->
<template>
  <el-dialog
    :title="textMap[dialogStatus]"
    :visible.sync="dialogFormVisible"
    :close-on-click-modal="false"
    width="500px"
  >
    <el-form
      ref="dataForm"
      :rules="stockRules"
      :model="stockData"
      label-width="98px"
      style="width: 400px; margin-left:50px;"
    >
      <el-form-item label="库存地址" prop="depotSite">
        <gx-input v-model="stockData.depotSite" gx-maxlength="varchar50" />
        <!-- <cascader
          @regionData="stockAdderssData"
        /> -->
      </el-form-item>
      <el-form-item label="联系人" prop="linkman">
        <gx-input v-model="stockData.linkman" gx-maxlength="varchar20" />
      </el-form-item>
      <el-form-item label="电话" prop="linkmanMobile">
        <gx-input v-model="stockData.linkmanMobile" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="createData('dataForm')">
        确 定
      </el-button>
      <el-button @click="dialogFormVisible = false">
        取 消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
/* api*/
import { insertOrUpdateAreaDepotMapped } from '@/api/procurement/purchasplan'
/* 公用方法*/
import { characters1_auto, linkerName, checkLinktel } from '@/assets/js/validate'// 校验输入内容

export default {
  name: 'StockAddDialog',
  data() {
    return {
      dialogFormVisible: false, // 字典分类录入弹框是否打开
      dialogStatus: '', // 新增/编辑状态
      // dialog title显示
      textMap: {
        update: '库存地址编辑',
        create: '库存地址录入'
      },
      // 库存管理录入数据
      stockData: {
        depotSite: '', // 仓库地址
        linkman: '', // 联系人
        linkmanMobile: '' // 联系电话
      },
      stockRules: { // 字典分类录入校验
        depotSite: [{ required: true, validator: characters1_auto, trigger: 'change' }], // 库存地址
        linkman: [{ required: true, validator: linkerName, trigger: 'blur' }], // 联系人
        linkmanMobile: [{ required: true, validator: checkLinktel, trigger: 'blur' }] // 电话
      }
    }
  },
  methods: {
    // 编辑
    openStockEdit(dialogStatus, row) {
      this.dialogStatus = dialogStatus
      // dialogStatus为create 清空初始化表单
      if (dialogStatus === 'create') {
        // 清空
        Object.keys(this.stockData).forEach((val, inx) => {
          this.stockData[val] = ''
        })
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else {
        // 修改
        this.stockData = {
          id: row.id,
          depotSite: row.depotSite, // 仓库地址
          linkman: row.linkman, // 联系人
          linkmanMobile: row.linkmanMobile // 联系电话
        }
      }
      this.dialogFormVisible = true
    },
    // 库存管理录入确定-保存、编辑
    createData(formName) {
      this.$refs[formName].validate((valid, field) => {
        if (valid) { // 校验通过
          insertOrUpdateAreaDepotMapped(this.stockData).then(res => {
            this.$message.success('操作成功')
            this.$emit('typeGroupLoadData') // 获取列表数据
            this.dialogFormVisible = false
          }).catch(() => {
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
