<template>
  <div>
    <el-dialog
      title="库位"
      :visible.sync="addFormVisible"
      :before-close="handleFormClose"
      width="60%"
    >
      <el-form
        :disabled="mode==='detail'"
        inline
        ref="form"
        label-position="top"
        :model="formData"
        :rules="rules"
        label-width="300px"
      >
        <el-form-item label="库位编码" prop="locCode" :rules="mode==='batch'?[]:rules.locCode">
          <el-input v-model="formData.locCode"></el-input>
        </el-form-item>
        <el-form-item label="库位名称" prop="def1">
          <el-input v-model="formData.def1"></el-input>
        </el-form-item>
        <el-form-item label="所属库区" prop="zoneCode">
          <PublicQuery
            title="所属库区"
            single
            :rowData.sync="zoneData"
            cfgKey="bdWmsZone"
            baseUrl="/bd_web/bdweb"
            :valueField.sync="formData.zoneCode"
            :textField.sync="formData.zoneCode"
          ></PublicQuery>
        </el-form-item>
        <el-form-item label="库位状态" prop="status" :rules="mode==='batch'?[]:rules.status">
          <el-select v-model="formData.status" disabled>
            <el-option label="正常" value="00"></el-option>
            <el-option label="冻结" value="99"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用类型" prop="locUseType" :rules="mode==='batch'?[]:rules.locUseType">
          <el-select v-model="formData.locUseType">
            <el-option label="箱件合并拣货库位" value="PC"></el-option>
            <el-option label="过渡库位" value="ST"></el-option>
            <el-option label="理货站" value="SS"></el-option>
            <el-option label="品质货位" value="QC"></el-option>
            <el-option label="加工台" value="KT"></el-option>
            <el-option label="越库库位" value="CD"></el-option>
            <el-option label="存储库位" value="RS"></el-option>
            <el-option label="箱拣货库位" value="CS"></el-option>
            <el-option label="件拣货库位" value="EA"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="固定/动态拣货位" prop="locPkType">
          <el-select v-model="formData.locPkType">
            <el-option label="固定拣货位" value="FPL"></el-option>
            <el-option label="动态拣货位" value="DPL"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上架顺序" prop="paSeq" :rules="mode==='batch'?[]:rules.paSeq">
          <el-input v-model="formData.paSeq"></el-input>
        </el-form-item>
        <el-form-item label="拣货顺序" prop="pkSeq" :rules="mode==='batch'?[]:rules.pkSeq">
          <el-input v-model="formData.pkSeq"></el-input>
        </el-form-item>
        <el-form-item label="库位组" prop="locGroup">
          <el-input v-model="formData.locGroup"></el-input>
        </el-form-item>
        <el-form-item label="库位ABC" prop="abc">
          <el-select v-model="formData.abc">
            <el-option label="A" value="A"></el-option>
            <el-option label="B" value="B"></el-option>
            <el-option label="C" value="C"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="长" prop="length">
          <el-input v-model="formData.length"></el-input>
        </el-form-item>
        <el-form-item label="宽" prop="width">
          <el-input v-model="formData.width"></el-input>
        </el-form-item>
        <el-form-item label="高" prop="height">
          <el-input v-model="formData.height"></el-input>
        </el-form-item>
        <el-form-item label="库位种类" prop="category">
          <el-select v-model="formData.category">
            <el-option label="平面仓库区" value="1"></el-option>
            <el-option label="货架仓库区" value="2"></el-option>
            <el-option label="高架仓库区" value="3"></el-option>
            <el-option label="重力式货架库区" value="4"></el-option>
            <el-option label="堆垛机货架库区" value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通道" prop="lane">
          <el-input v-model="formData.lane"></el-input>
        </el-form-item>
        <el-form-item label="序号" prop="seq">
          <el-input v-model="formData.seq"></el-input>
        </el-form-item>
        <el-form-item label="层" prop="floor">
          <el-input v-model="formData.floor"></el-input>
        </el-form-item>
        <el-form-item label="混商品" prop="isMixSku">
          <el-checkbox v-model="formData.isMixSku" true-label="Y" false-label="N"></el-checkbox>
        </el-form-item>
        <el-form-item label="最大混商品数" prop="maxMixSku" label-width="300x">
          <el-input v-model="formData.maxMixSku" :disabled="formData.isMixSku==='N'"></el-input>
        </el-form-item>
        <el-form-item label="X坐标" prop="x">
          <el-input v-model="formData.x"></el-input>
        </el-form-item>
        <el-form-item label="Y坐标" prop="y">
          <el-input v-model="formData.y"></el-input>
        </el-form-item>
        <el-form-item label="Z坐标" prop="z">
          <el-input v-model="formData.z"></el-input>
        </el-form-item>
        <el-form-item label="混批次" prop="isMixLot">
          <el-checkbox v-model="formData.isMixLot" true-label="Y" false-label="N"></el-checkbox>
        </el-form-item>
        <el-form-item label="最大混批次数" prop="maxMixLot">
          <el-input v-model="formData.maxMixLot" :disabled="formData.isMixLot==='N'"></el-input>
        </el-form-item>
        <el-form-item label="最大体积" prop="maxCubic">
          <el-input v-model="formData.maxCubic"></el-input>
        </el-form-item>
        <el-form-item label="最大重量" prop="maxWeight">
          <el-input v-model="formData.maxWeight"></el-input>
        </el-form-item>
        <el-form-item label="最大托盘数" prop="maxPl">
          <el-input v-model="formData.maxPl"></el-input>
        </el-form-item>
        <el-form-item label="忽略跟踪号" prop="isLoseId">
          <el-checkbox v-model="formData.isLoseId" true-label="Y" false-label="N"></el-checkbox>
        </el-form-item>
        <el-form-item label="是否启用" prop="isEnable">
          <el-checkbox v-model="formData.isEnable" true-label="Y" false-label="N"></el-checkbox>
        </el-form-item>
      </el-form>
      <hr style="background-color:#F5F5F5;height:1px;border:none" v-if="mode==='detail'">
      <div class="row-mrg-t" v-if="mode==='detail'">
        <div class="operation-info">
          <span>创建人登录名(姓名)：{{formData.creatorLoginName}}({{formData.creatorNameCn}})</span>
          <span>创建时间：{{formData.createTime}}</span>
          <span>修改人登录名(姓名)：{{formData.modifierLoginName}}({{formData.modifierNameCn}})</span>
          <span>修改时间：{{formData.modifyTime}}</span>
        </div>
      </div>
      <div v-if="mode!=='detail'&&mode!=='batch'">
        <el-button v-auth="50400030110" type="primary" @click="submitForm">保存</el-button>
        <el-button v-auth="50400030111" type="primary" @click="submitAddForm">保存并新增</el-button>
      </div>
      <div v-if="mode==='batch'">
        <el-button v-auth="50400030110" type="primary" @click="submitEdit">确定修改</el-button>
        <el-button v-auth="50400030111" type="primary" @click="submitAddForm">重置</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      formData: {
        status: '00',
        isMixSku: 'Y',
        isMixLot: 'Y',
        isLoseId: 'Y',
        isEnable: 'Y'
      },
      rules: {
        locCode: [
          { required: true, message: '库位编码必填', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '库位状态必填', trigger: 'blur' }
        ],
        locUseType: [
          { required: true, message: '使用类型必填', trigger: 'blur' }
        ],
        paSeq: [
          { required: true, message: '上架顺序必填', trigger: 'blur' }
        ],
        pkSeq: [
          { required: true, message: '拣货顺序必填', trigger: 'blur' }
        ]
      },
      zoneData: {},
      selectedIds: ''
    }
  },
  props: {
    addFormVisible: Boolean,
    rowData: Object,
    mode: String,
    rows: Array
  },
  methods: {

    // 发送保存数据
    psotList () {
      const url = this.$API.warehouseBasic.libraryPlace.saveLoc
      const locModel = this.formData
      const param = { locModel }
      const config = {
        headers: { 'Content-Type': 'application/json' },
        transformRequest: [],
      }
      // console.log(param)
      this.$https.post(url, param, config).then(res => {
        if (res.success || res.succeed) {
          this.$message.success('操作成功')
          this.$emit('saveOK')
        }
      })
    },

    // 关闭新增弹窗
    handleFormClose () {
      this.$emit('handleFormClose')
      this.$refs.form.resetFields()
    },

    // 保存
    submitForm () {
      this.$refs.form.validate((vaild) => {
        if (vaild) {
          this.psotList()
          this.$nextTick(() => {
            this.handleFormClose()
          })
        } else {
          return false
        }
      })
    },

    // 保存并新增
    submitAddForm () {
      this.$refs.form.validate((vaild) => {
        if (vaild) {
          this.psotList()
          this.$nextTick(() => {
            this.$refs.form.resetFields()
          })
        } else {
          return false
        }
      })
    },
    submitEdit () {
      const editLocBatch = this.$API.warehouseBasic.libraryPlace.editLocBatch
      const param = { updateLocModels: JSON.stringify(this.rows), editLocModel: JSON.stringify(this.formData) }
      this.$https.post(editLocBatch, param).then(res => {
        this.$emit('saveOK')
        this.handleFormClose()
      })
    }
  },
  watch: {
    rowData: {
      handler (item) {
        this.formData = { ...item }
      },
      deep: true
    },
    zoneData: {
      handler (item) {
        this.formData.zoneCode = item.zoneCode
        this.formData.zoneName = item.zoneName
      },
      deep: true
    }
  }
}
</script>
