<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="1200px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-collapse>
        <el-collapse-item title="基本信息" name="1">
          <el-row>
            <el-col :span="8">
              <el-form-item label="父级设备" prop="parentId">
                <el-tree-select
                  v-model="formData.parentId"
                  :data="machineComponentTree"
                  :props="defaultProps"
                  check-strictly
                  default-expand-all
                  placeholder="请选择父级"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="设备类型" prop="componentType">
                <el-radio-group v-model="formData.componentType">
                  <el-radio
                    v-for="dict in getIntDictOptions(DICT_TYPE.MES_MACHINE_TYPE)"
                    :key="dict.value"
                    :label="dict.value"
                  >
                    {{ dict.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="显示顺序" prop="sort">
                <el-input v-model="formData.sort" placeholder="请输入显示顺序" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="编码" prop="code">
                <el-input disabled v-model="formData.code" placeholder="保存自动生成" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="设备名称" prop="name">
                <el-input v-model="formData.name" placeholder="请输入设备名称" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="装备SN号" prop="serialCode">
                <el-input v-model="formData.serialCode" placeholder="请输入装备SN号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="位置" prop="position">
                <el-input v-model="formData.position" placeholder="请输入位置" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="规格" prop="standard">
                <el-input v-model="formData.standard" placeholder="请输入规格" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark" placeholder="请输入备注" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item v-if="formData.componentType == 1|| formData.componentType == 2" label="机台类型" prop="machineType">
                <el-select v-model="formData.machineType" placeholder="请选择机台类型">
                  <el-option
                    v-for="dict in getStrDictOptions(DICT_TYPE.MES_ORG_TYPE)"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="formData.status">
                  <el-radio
                    v-for="dict in getIntDictOptions(DICT_TYPE.MES_MACHINE_STATUS)"
                    :key="dict.value"
                    :label="dict.value"
                  >
                    {{ dict.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
        <el-collapse-item title="模具维保" name="3">
<!--          <el-row>-->
<!--            <el-col :span="8">-->
<!--              <el-form-item label="建议维保类型" prop="maintainType">-->
<!--                <el-radio-group v-model="formData.status">-->
<!--                  <el-radio-->
<!--                    v-for="dict in getStrDictOptions(DICT_TYPE.MES_MAINTAIN_TYPE)"-->
<!--                    :key="dict.value"-->
<!--                    :label="dict.value"-->
<!--                  >-->
<!--                    {{ dict.label }}-->
<!--                  </el-radio>-->
<!--                </el-radio-group>-->
<!--              </el-form-item>-->
<!--            </el-col>-->
<!--            <el-col :span="8">-->
<!--              <el-form-item label="建议维保时间" prop="maintainTime">-->
<!--                <el-input v-model="formData.maintainTime" placeholder="请输入建议维保时间" />-->
<!--              </el-form-item>-->
<!--            </el-col>-->
<!--            <el-col :span="8">-->
<!--              <el-form-item label="建议时间单位" prop="maintainTimeUnit">-->
<!--                <el-radio-group v-model="formData.status">-->
<!--                  <el-radio-->
<!--                    v-for="dict in getStrDictOptions(DICT_TYPE.MES_MAINTAIN_TIME_UNIT)"-->
<!--                    :key="dict.value"-->
<!--                    :label="dict.value"-->
<!--                  >-->
<!--                    {{ dict.label }}-->
<!--                  </el-radio>-->
<!--                </el-radio-group>-->
<!--              </el-form-item>-->
<!--            </el-col>-->
<!--          </el-row>-->
          <el-row>
            <el-col :span="8">
              <el-form-item label="维保类型" prop="trueMaintainTimeType">
                <el-radio-group v-model="formData.trueMaintainTimeType">
                  <el-radio
                    v-for="dict in getStrDictOptions(DICT_TYPE.MES_MAINTAIN_TYPE)"
                    :key="dict.value"
                    :label="dict.value"
                  >
                    {{ dict.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="维保时间" prop="trueMaintainTime">
                <el-input v-model="formData.trueMaintainTime" placeholder="请输入维保时间" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="时间单位" prop="trueMaintainTimeUnit">
                <el-radio-group v-model="formData.trueMaintainTimeUnit">
                  <el-radio
                    v-for="dict in getStrDictOptions(DICT_TYPE.MES_MAINTAIN_TIME_UNIT)"
                    :key="dict.value"
                    :label="dict.value"
                  >
                    {{ dict.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="上次维保时间" prop="lastMaitainTime">
                <el-date-picker
                  v-model="formData.lastMaitainTime"
                  type="date"
                  value-format="x"
                  placeholder="选择上次维保时间"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="关联模具" prop="maintainType">
                <el-select v-model="formData.maintainType" placeholder="">
                  <el-option label="请选择字典生成" value="" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="出厂日期" prop="outgoingTime">
                <el-date-picker
                  v-model="formData.outgoingTime"
                  type="date"
                  value-format="x"
                  placeholder="选择出厂日期"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
      <el-form-item label="出厂报告" prop="outgoingReport" style="margin-top:20px">
        <Editor v-model="formData.outgoingReport" height="180px" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { MachineComponentApi, MachineComponentVO } from '@/api/mes/machine'
import { defaultProps, handleTree } from '@/utils/tree'

/** 机台 表单 */
defineOptions({ name: 'MachineComponentForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  code: undefined,
  name: undefined,
  parentId: undefined,
  sort: undefined,
  orgId: undefined,
  serialCode: undefined,
  outgoingTime: undefined,
  outgoingReport: undefined,
  position: undefined,
  standard: undefined,
  remark: undefined,
  status: undefined,
  componentType: undefined,
  machineType: undefined,
})
const formRules = reactive({
  name: [{ required: true, message: '装备名称不能为空', trigger: 'blur' }],
  parentId: [{ required: true, message: '父id不能为空', trigger: 'blur' }],
  componentType: [{ required: true, message: '组织类型不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref
const machineComponentTree = ref() // 树形结构

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MachineComponentApi.getMachineComponent(id)
      if(formData.value.outgoingReport===null || formData.value.outgoingReport===undefined){
        formData.value.outgoingReport = ''
      }
    } finally {
      formLoading.value = false
    }
  }
  await getMachineComponentTree()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MachineComponentVO
    if (formType.value === 'create') {
      await MachineComponentApi.createMachineComponent(data)
      message.success(t('common.createSuccess'))
    } else {
      await MachineComponentApi.updateMachineComponent(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    parentId: undefined,
    sort: undefined,
    orgId: undefined,
    serialCode: undefined,
    outgoingTime: undefined,
    outgoingReport: undefined,
    position: undefined,
    standard: undefined,
    remark: undefined,
    status: undefined,
    componentType: undefined,
    machineType: undefined,
  }
  formRef.value?.resetFields()
}

/** 获得机台树 */
const getMachineComponentTree = async () => {
  machineComponentTree.value = []
  const data = await MachineComponentApi.getMachineComponentList()
  const root: Tree = { id: 0, name: '顶级组织', children: [] }
  root.children = handleTree(data, 'id', 'parentId')
  machineComponentTree.value.push(root)
}
</script>
