<template>
    <div>
        <el-dialog
          title="加工工序"
          v-if="stepVisible"
          :visible.sync="stepVisible"
          :before-close="close"
          width="66%">
          <el-form
            inline
            ref="stepForm"
            label-position="top"
            :model="formData"
            :rules="rules">
            <div>
              <el-form-item
                label="行号"
                prop="lineNo">
                <el-input v-model="formData.lineNo"></el-input>
              </el-form-item>
              <el-form-item
                label="加工描述"
                prop="step">
                <el-input v-model="formData.step"></el-input>
              </el-form-item>
            </div>
          </el-form>
          <div class="more-query-btn">
            <el-button v-auth="50300030101" type="primary" @click="handleConfirm">确认</el-button>
            <el-button v-auth="50300030101" type="primary" @click="handleConfirmAdd">确认并添加</el-button>
          </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
  data () {
    return {
      formData: {},
      rules: {
        lineNo: [{ required: true, message: '行号必填', trigger: 'change' }],
        step: [{ required: true, message: '加工描述必填', trigger: 'change' }],
      }
    }
  },
  props: {
    stepVisible: Boolean,
    currentRow: {
      type: Object,
      default: () => ({})
    },
    rowIndex: '',
    stepBool: Boolean
  },
  mounted () {

  },
  methods: {
    close () {
      this.$emit('close')
      this.$refs.stepForm.resetFields()
    },

    // 确认
    handleConfirm () {
      let data = Object.assign({ isChecked: 'false' }, this.formData)
      if (this.stepBool) {
        this.$emit('editStepOK', data, this.rowIndex)
        this.$refs.stepForm.resetFields()
      } else {
        this.$refs.stepForm.validate((valid) => {
          if (valid) {
            this.$emit('handleStepConfirm', data)
            this.$refs.stepForm.resetFields()
          }
        })
      }
    },

    // 确认并添加
    handleConfirmAdd () {
      let data = Object.assign({ isChecked: 'false' }, this.formData)
      if (this.stepBool) {
        this.$emit('editStepOK', data, this.rowIndex)
        this.$refs.stepForm.resetFields()
      } else {
        this.$refs.stepForm.validate((valid) => {
          if (valid) {
            this.$emit('handleStepConfirmAdd', data)
            this.$refs.stepForm.resetFields()
          }
        })
      }
    },
  },
  watch: {
    currentRow: {
      handler (item) {
        this.formData = { ...item }
      },
      deep: true
    }
  },
}
</script>
