<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="工单分类ID" prop="categoryId">
                      <el-input v-model="formData.categoryId" placeholder="请输入工单分类ID" />
                    </el-form-item>
                    <el-form-item label="技术运维商ID" prop="techOperatorId">
                      <el-input v-model="formData.techOperatorId" placeholder="请输入技术运维商ID" />
                    </el-form-item>
                    <el-form-item label="用户ID" prop="userId">
                      <el-input v-model="formData.userId" placeholder="请输入用户ID" />
                    </el-form-item>
                    <el-form-item label="需求描述">
                      <Editor v-model="formData.requirement" :min-height="192"/>
                    </el-form-item>
                    <el-form-item label="资料描述">
                      <FileUpload v-model="formData.requiredInfo"/>
                    </el-form-item>
                    <el-form-item label="下单时间" prop="orderTime">
                      <el-date-picker clearable v-model="formData.orderTime" type="date" value-format="timestamp" placeholder="选择下单时间" />
                    </el-form-item>
                    <el-form-item label="工单状态" prop="status">
                      <el-radio-group v-model="formData.status">
                            <el-radio label="1">请选择字典生成</el-radio>
                      </el-radio-group>
                    </el-form-item>
                    <el-form-item label="打回原因" prop="rejectReason">
                      <el-input v-model="formData.rejectReason" placeholder="请输入打回原因" />
                    </el-form-item>
                    <el-form-item label="期望完成日期" prop="expectedCompletionDate">
                      <el-date-picker clearable v-model="formData.expectedCompletionDate" type="date" value-format="timestamp" placeholder="选择期望完成日期" />
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as WorkorderInfosApi from '@/api/server/WorkorderInfos';
  import FileUpload from '@/components/FileUpload';
  import Editor from '@/components/Editor';
      export default {
    name: "WorkorderInfosForm",
    components: {
          FileUpload,
          Editor,
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            categoryId: undefined,
                            techOperatorId: undefined,
                            userId: undefined,
                            requirement: undefined,
                            requiredInfo: undefined,
                            orderTime: undefined,
                            status: undefined,
                            rejectReason: undefined,
                            expectedCompletionDate: undefined,
        },
        // 表单校验
        formRules: {
                        categoryId: [{ required: true, message: '工单分类ID不能为空', trigger: 'blur' }],
                        userId: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
                        requirement: [{ required: true, message: '需求描述不能为空', trigger: 'blur' }],
                        orderTime: [{ required: true, message: '下单时间不能为空', trigger: 'blur' }],
                        status: [{ required: true, message: '工单状态不能为空', trigger: 'blur' }],
        },
                        };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await WorkorderInfosApi.getWorkorderInfos(id);
            this.formData = res.data;
            this.title = "修改工单";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增工单";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await WorkorderInfosApi.updateWorkorderInfos(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await WorkorderInfosApi.createWorkorderInfos(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            categoryId: undefined,
                            techOperatorId: undefined,
                            userId: undefined,
                            requirement: undefined,
                            requiredInfo: undefined,
                            orderTime: undefined,
                            status: undefined,
                            rejectReason: undefined,
                            expectedCompletionDate: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>
