<template>
  <!-- 添加或修改项目信息对话框 -->
  <el-dialog title="工序表单" height="400px" v-model="visible" width="400px" append-to-body draggable>
    <div class="form-container">
      <el-form ref="processRef" v-loading="loading" class="master-container" :model="form" :rules="rules"
        label-width="80px">
        <el-form-item label="工序编码" prop="processCode">
          <el-input v-model.trim="form.processCode" placeholder="请输入工序编码" />
        </el-form-item>
        <el-form-item label="工序简称" prop="processAlias">
          <el-input v-model.trim="form.processAlias" placeholder="请输入工序简称" />
        </el-form-item>
        <el-form-item label="工序状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择工序状态">
            <el-option v-for="item in process_status" :key="item.value" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" placeholder="请输入工序描述" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button type="primary" icon="Check" @click="handleSave">确 定</el-button>
      <el-button icon="Close" @click="handleCancel">取 消</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { getProcess, saveProcess } from '@/api/business/process'
const { proxy } = getCurrentInstance()
const emit = defineEmits(['handleSaveSuccess'])
/** 字典数组区 */
const { process_status } = proxy.useDict('process_status')
/** 工序表单变量 */
const loading = ref(false)
const visible = ref(false)
const data = reactive({
  form: {},
  rules: {
    processCode: [{ required: true, message: '工序编码不能为空', trigger: 'blur' }],
    processAlias: [{ required: true, message: '工序简称不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '工序状态不能为空', trigger: 'blur' }]
  }
})
const { form, rules } = toRefs(data)
/***********************  方法区  ****************************/
/** 打开抽屉 */
function open(id) {
  reset()
  visible.value = true
  getProcess(id || 0).then((res) => {
    if (res.data) {
      form.value = res.data
    }
  })
}

/** 取消按钮 */
function handleCancel() {
  visible.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    processCode: '',
    processAlias: '',
    status: '',
    remark: ''
  }
  proxy.resetForm('processRef')
}

/** 提交按钮 */
function handleSave() {
  proxy.$refs['processRef'].validate((valid) => {
    if (valid) {
      saveProcess(form.value).then((response) => {
        if (response.code == 200) {
          proxy.$modal.msgSuccess('操作成功')
          emit('handleSaveSuccess')
          visible.value = false
        }
      })
    }
  })
}

/** 暴露给父组件的方法 */
defineExpose({
  open
})
</script>
