<template>
  <a-modal
    centered
    class="warehouseEdit-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <!-- 表单 -->
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="warehouseEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="仓库名称" prop="name">
            <a-input
              id="warehouseEdit-name"
              :maxLength="30"
              v-model.trim="form.name"
              @change="filterEmpty"
              placeholder="请输入仓库名称(最多30个字符)"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="地址" prop="addressInfo">
            <AreaList
              id="warehouseEdit-areaList"
              :value="form.addressInfo"
              changeOnSelect
              ref="areaList"
              @change="areaChange"
              defValKey="id"></AreaList>
          </a-form-model-item>
          <a-form-model-item prop="address" :wrapper-col="{ span: 16, offset: 6 }">
            <a-input
              id="warehouseEdit-desc"
              v-model.trim="form.address"
              placeholder="请输入详细地址"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="仓库类型" prop="warehouseType">
            <v-select
              v-model="form.warehouseType"
              ref="state"
              id="warehouseEdit-state"
              code="WAREHOUSE_TYPE"
              placeholder="请选择仓库类型"
              allowClear
            ></v-select>
          </a-form-model-item>
          <a-form-model-item label="ERP仓库编码" prop="erpWarehouseCode">
            <a-input
              id="warehouseEdit-code"
              v-model.trim="form.erpWarehouseCode"
              placeholder="请输入ERP仓库编码"
              allowClear />
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button id="warehouse-edit-modal-back" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
          <a-button type="primary" id="warehouse-edit-modal-save" @click="handleSave">保存</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import AreaList from '@/views/common/areaList.js'
// 接口
import { warehouseSave } from '@/api/warehouse'
export default {
  name: 'WarehouseEditModal',
  mixins: [commonMixin],
  components: { STable, AreaList, VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    },
    nowData: {
      type: Object,
      default: null
    }
  },
  computed: {
    modalTit () {
      return (this.itemSn ? '编辑' : '新增') + '仓库'
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      // 表单 label 布局
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      // 提交参数
      form: {
        name: '', // 仓库名称
        address: '', // 详细地址
        addressInfo: undefined, // 选择省市区
        provinceSn: undefined, // 省份sn
        provinceName: undefined, // 省份名称
        citySn: undefined, // 市sn
        cityName: undefined, // 市名称
        districtSn: undefined, // 区sn
        districtName: undefined, // 区名称
        erpWarehouseCode: undefined, // ERP仓库编码
        warehouseType: undefined// 仓库类型
      },
      rules: {
        name: [
          { required: true, message: '请输入仓库名称', trigger: 'blur' }
        ],
        addressInfo: [
          { required: true, message: '请选择省市区', trigger: 'change' }
        ],
        address: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ],
        erpWarehouseCode: [
          { required: true, message: '请输入ERP仓库编码', trigger: 'blur' }
        ],
        warehouseType: [
          { required: true, message: '请选择仓库类型', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 仓库名称 去除空格
    filterEmpty () {
      let str = this.form.name
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.name = str
    },
    // 地区
    areaChange (val, row) {
      this.form.addressInfo = val
      this.form.provinceSn = val[0] ? val[0] : ''
      this.form.provinceName = row[0] ? row[0].name ? row[0].name : '' : ''
      this.form.citySn = val[1] ? val[1] : ''
      this.form.cityName = row[1] ? row[1].name ? row[1].name : '' : ''
      this.form.districtSn = val[2] ? val[2] : ''
      this.form.districtName = row[2] ? row[2].name ? row[2].name : '' : ''
    },
    //  详情
    getDetail () {
      if (this.nowData) {
        const addressVal = [this.nowData.provinceSn ? this.nowData.provinceSn : '', this.nowData.citySn ? this.nowData.citySn : '', this.nowData.districtSn ? this.nowData.districtSn : '']
        this.form = {
          name: this.nowData.name ? this.nowData.name : '',
          address: this.nowData.address ? this.nowData.address : '',
          provinceSn: this.nowData.provinceSn ? this.nowData.provinceSn : '',
          provinceName: this.nowData.provinceName ? this.nowData.provinceName : '',
          citySn: this.nowData.citySn ? this.nowData.citySn : '',
          cityName: this.nowData.cityName ? this.nowData.cityName : '',
          districtSn: this.nowData.districtSn ? this.nowData.districtSn : '',
          districtName: this.nowData.districtName ? this.nowData.districtName : '',
          erpWarehouseCode: this.nowData.erpWarehouseCode ? this.nowData.erpWarehouseCode : '', // ERP仓库编码
          warehouseType: this.nowData.warehouseType ? this.nowData.warehouseType : '',
          addressInfo: addressVal
        }
      }
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = {
            warehouseSn: this.itemSn ? this.itemSn : undefined
          }
          const newParams = Object.assign(this.form, params)
          delete newParams.addressInfo
          _this.spinning = true
          warehouseSave(newParams).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              setTimeout(function () {
                _this.isShow = false
                _this.$refs.ruleForm.resetFields()
                _this.spinning = false
              }, 300)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.form = {
          name: '',
          address: '',
          addressInfo: undefined,
          provinceSn: undefined,
          provinceName: undefined,
          citySn: undefined,
          cityName: undefined,
          districtSn: undefined,
          districtName: undefined,
          erpWarehouseCode: undefined, // ERP仓库编码
          warehouseType: undefined
        }
      }
    },
    itemSn (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.getDetail()
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .warehouseEdit-modal{
    .ant-modal-body {
    	padding: 40px 40px 24px;
    }
    .btn-cont {
    	text-align: center;
    	margin: 35px 0 10px;
    }
  }
</style>
