<template>
  <div class="mod-partspare">
    <el-dialog
      @close="closeDialog"
      v-drag
      :title=" !this.addOrUpdate ? '新增' : '修改'"
      :close-on-click-modal="false"
      width="500px"
      :visible.sync="visible">
      <el-form :inline="true" label-position="top" :model="dataForm" ref="dataForm"
               @keyup.enter.native="dataFormSubmit()"
               label-width="120px">
        <el-form-item required label="备品备件编码" >
          <el-input :disabled="addOrUpdate" v-model="dataForm.partNo"></el-input>
        </el-form-item>
        <el-form-item required label="备品备件名称">
          <el-input v-model="dataForm.partDescription"></el-input>
        </el-form-item>
        <el-form-item required label="规格型号">
          <el-input v-model="dataForm.spec"></el-input>
        </el-form-item>
        <el-form-item required label="产品组编码">
          <el-input v-model="dataForm.groupId"></el-input>
        </el-form-item>
        <el-form-item required label="计价单位">
          <el-input v-model="dataForm.averagePrice"></el-input>
        </el-form-item>
        <el-form-item required label="单价单位">
          <el-input v-model="dataForm.umid"></el-input>
        </el-form-item>
        <el-form-item required label="预计使用寿命">
          <el-input-number :min="0" style="width: 146px" v-model="dataForm.estUseQty" :step="2"
                           step-strictly></el-input-number>
        </el-form-item>
        <el-form-item required label="最高库存数量">
          <el-input-number :min="0" style="width: 146px" v-model="dataForm.maxStock" :step="2"
                           step-strictly></el-input-number>
        </el-form-item>
        <el-form-item required label="最低库存数量">
          <el-input-number :min="0" style="width: 146px" v-model="dataForm.minStock" :step="2"
                           step-strictly></el-input-number>
        </el-form-item>
        <el-form-item required label="状态">
          <el-select v-model="dataForm.active">
            <el-option value="Y" label="启用"></el-option>
            <el-option value="N" label="禁用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item required >
          <span slot="label" style="" @click="getBaseList(110)"><a herf="#">供应商编码</a></span>
          <el-input v-model="dataForm.supplierId"></el-input>
        </el-form-item>
        <el-form-item required>
          <span slot="label" style="" @click="getBaseList(111)"><a herf="#">仓库编码</a></span>
          <el-input v-model="dataForm.warehouseId"></el-input>
        </el-form-item>
        <el-form-item required>
          <el-link v-if="dataForm.warehouseId" slot="label" @click="getBaseList(112)">货位编码</el-link>
          <span v-if="!dataForm.warehouseId" slot="label">货位编码</span>
          <el-input v-model="dataForm.locationId"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-button type="primary" @click="visible = false">取消</el-button>
    </span>
    </el-dialog>
    <Chooselist ref="baseList" @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
import Chooselist from '@/views/modules/common/Chooselist_eam'

import {getPartSpareInfo, savePartSpare, updatePartSpare} from '@/api/partspare/partspare.js'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        active: "Y",
        averagePrice: '',
        createdBy: '',
        estUseQty: 0,
        id: '',
        locationId: '',
        maxStock: 0,
        minStock: 0,
        partDescription: "",
        partNo: '',
        sCodeControlFlag: '',
        site: this.$store.state.user.site,
        spec: '',
        supplierId: '',
        umid: '',
        warehouseId: '',
        groupId:'',
      },
      addOrUpdate: false,
      tagNo: 0,
    }
  },
  components: {
    Chooselist
  },
  methods: {
    // 获取基础数据列表S
    getBaseList(val, type) {
      this.tagNo = val
      this.$nextTick(() => {
        let strVal = ''
        let conSql = ''
        if (val === 110) {
          strVal = this.dataForm.supplierId
        }
        if (val === 111) {
          strVal = this.dataForm.warehouseId
        }
        if (val === 112) {
          strVal = this.dataForm.locationId
          let whereValue = this.dataForm.warehouseId ? "'" + this.dataForm.warehouseId + "'" : "'*'"
          conSql = ' and warehouse_id = ' + whereValue
        }
        this.$refs.baseList.init(val, strVal, conSql)
      })
    },
    /* 列表方法的回调 */
    getBaseData(val) {
      if (this.tagNo === 110) {
        this.dataForm.supplierId = val.supplier_id
      }
      if (this.tagNo === 111) {
        this.dataForm.warehouseId = val.warehouse_id
      }
      if (this.tagNo === 112) {
        this.dataForm.locationId = val.location_id
      }
    },
    init(row) {
      this.addOrUpdate = row ? true : false
      this.visible = true
      this.$nextTick(() => {
        if (this.addOrUpdate) {
          this.dataForm.site = row.site
          this.dataForm.partNo = row.partNo
          getPartSpareInfo(this.dataForm).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.active = data.partSpare.active
              this.dataForm.averagePrice = data.partSpare.averagePrice
              this.dataForm.createdBy = data.partSpare.createdBy
              this.dataForm.umid = data.partSpare.umid
              this.dataForm.minStock = data.partSpare.minStock
              this.dataForm.maxStock = data.partSpare.maxStock
              this.dataForm.supplierId = data.partSpare.supplierId
              this.dataForm.createDate = data.partSpare.createDate
              this.dataForm.lastUpdateBy = data.partSpare.lastUpdateBy
              this.dataForm.estUseQty = data.partSpare.estUseQty
              this.dataForm.sCodeControlFlag = data.partSpare.sCodeControlFlag
              this.dataForm.locationId = data.partSpare.locationId
              this.dataForm.warehouseId = data.partSpare.warehouseId
              this.dataForm.partDescription = data.partSpare.partDescription
              this.dataForm.partNo = data.partSpare.partNo
              this.dataForm.site = data.partSpare.site
              this.dataForm.spec = data.partSpare.spec
              this.dataForm.groupId = data.partSpare.groupId

            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      if (!this.dataForm.partNo) {
        this.$message.warning("备品备件编码不能为空!")
        return;
      }
      if (!this.dataForm.partDescription) {
        this.$message.warning("备品备件名称不能为空!")
        return;
      }
      if (!this.dataForm.partNo) {
        this.$message.warning("规格型号不能为空!")
        return;
      }
      if (!this.dataForm.groupId) {
        this.$message.warning("产品组编码不能为空")
        return;
      }
      if (!this.dataForm.averagePrice) {
        this.$message.warning("计价单位不能为空")
        return;
      }
      if (!this.dataForm.umid) {
        this.$message.warning("单价单位不能为空")
        return;
      }
      if (!this.dataForm.estUseQty) {
        this.$message.warning("预计使用寿命必须大于0")
        return;
      }
      if (!this.dataForm.maxStock) {
        this.$message.warning("最高库存数量必须大于0")
        return;
      }
      if (this.dataForm.minStock != '') {
        this.$message.warning("最低库存数量不能为空")
        return;
      }
      if (!this.dataForm.supplierId) {
        this.$message.warning("供应商编码不能为空")
        return;
      }
      if (!this.dataForm.warehouseId) {
        this.$message.warning("仓库编码不能为空")
        return;
      }
      if (!this.dataForm.locationId) {
        this.$message.warning("货位编码不能为空")
        return;
      }
      if (this.addOrUpdate) {
        updatePartSpare(this.dataForm).then(({data}) => {
          if (data && data.code == 0) {
            this.$message.success(data.msg)
            this.visible = false
          } else {
            this.$message.warning(data.msg)
          }
        })
      } else {
        savePartSpare(this.dataForm).then(({data}) => {
          if (data && data.code == 0) {
            this.$message.success(data.msg)
            this.visible = false
          } else {
            this.$message.warning(data.msg)
          }
        })
      }
    },
    closeDialog() {
      this.$nextTick(() => {
        this.$emit('refreshDataList')
        Object.assign(this.$data, this.$options.data.call(this));
      })
    },
  }
}
</script>
