<template>
  <el-dialog :visible.sync="visible" width="500px" :title="title" @open="onOpen">
    <el-form :model="form" :rules="rules" ref="form" label-width="80px">
      <el-row>
        <form-col :span="span" label="工序" prop="processId">
          <process-input v-model="form.processId" :disabled="processId != null" :query="processQuery"/>
        </form-col>
        <form-col :span="span" label="上报数量" prop="num">
          <el-input-number v-model="form.num" :min="0" controls-position="right" style="width: 100%;" placeholder="请输入上报数量"/>
        </form-col>
        <form-col :span="span" label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" :maxlength="200" show-word-limit/>
        </form-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import FormCol from '@/components/FormCol/index.vue'
import ProcessInput from '@/components/Business/Process/ProcessInput.vue'
import { addStore, getStore, updateStore } from '@/api/bst/store'
import { OrderStatus } from '@/utils/enums'

export default {
  name: 'StoreEditDialog',
  components: { ProcessInput, FormCol },
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    processId: {
      type: String,
      default: null,
    },
    id: {
      type: String,
      default: null,
    }
  },
  data() {
    return {
      span: 24,
      form: {},
      rules: {
        processId: [
          { required: true, message: '请选择工序', trigger: 'change' }
        ],
        num: [
          { required: true, message: '请输入上报数量', trigger: 'blur' }
        ]
      },
      title: null
    }
  },
  computed: {
    visible: {
      get() {
        return this.show;
      },
      set(val) {
        this.$emit('update:show', val);
      }
    },
    processQuery() {
      return {
        orderStatusList: [OrderStatus.RELEASED]
      }
    }
  },
  methods: {
    onOpen() {
      if (this.id == null) {
        this.title = "添加入库";
        this.reset();
      } else {
        getStore(this.id).then(response => {
          this.form = response.data;
          this.title = "修改入库";
        });
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        processId: this.processId,
        num: null,
        storeNum: null,
        createId: null,
        createTime: null,
        status: null,
        storeUserId: null,
        storeTime: null,
        deleted: null
      };
      this.resetForm("form");
    },
    cancel() {
      this.visible = false;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.visible = false;
              this.$emit('success');
            });
          } else {
            addStore(this.form).then(response => {
              this.$modal.msgSuccess("上报成功");
              this.visible = false;
              this.$emit('success');
            });
          }
        }
      });
    },
  }
}
</script>
<style scoped lang="scss">

</style>
