<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="70%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="160px" :disabled="true">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="约谈数量" prop="interviewCount" class="long-label-item">
              <el-input v-model="formData.interviewCount" placeholder="请输入约谈数量"
                               :min="0"
                               :max="999999"
                               :step="1"
                               style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="书面检查数量" prop="writtenExaminationCount" class="long-label-item">
              <el-input v-model="formData.writtenExaminationCount" placeholder="请输入书面检查数量"
                               :min="0"
                               :max="999999"
                               :step="1"
                               style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="通报批评数量" prop="criticismCount" class="long-label-item">
              <el-input v-model="formData.criticismCount" placeholder="请输入通报批评数量"
                               :min="0"
                               :max="999999"
                               :step="1"
                               style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="警告或记过处分数量" prop="warningCount" class="long-label-item">
              <el-input v-model="formData.warningCount" placeholder="请输入警告或记过处分数量"
                               :min="0"
                               :max="999999"
                               :step="1"
                               style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="调离岗位或免职数量" prop="dismissalCount" class="long-label-item">
              <el-input v-model="formData.dismissalCount" placeholder="请输入调离岗位或免职数量"
                               :min="0"
                               :max="999999"
                               :step="1"
                               style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他数量" prop="otherCount" class="long-label-item">
              <el-input v-model="formData.otherCount" placeholder="请输入其他数量"
                               :min="0"
                               :max="999999"
                               :step="1"
                               style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属季度" prop="quarter" class="long-label-item">
              <el-select v-model="formData.quarter" placeholder="请选择所属季度">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.TRS_QUARTER)"
                           :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上报时间" prop="reportTime" class="long-label-item">
              <el-date-picker clearable v-model="formData.reportTime" type="date"
                              value-format="timestamp" placeholder="选择上报时间" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="上报人" prop="reporterName" class="long-label-item">
              <el-input v-model="formData.reporterName" placeholder="请输入上报人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上报人电话" prop="reporterPhone" class="long-label-item">
              <el-input v-model="formData.reporterPhone" placeholder="请输入上报人电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-col :span="24">
          <el-form-item label="整改报告" prop="attachment">
            <FilePreview v-model="formData.attachment"/>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="备注信息" prop="reportRemark">
            <el-input
              v-model="formData.reportRemark"
              type="textarea"
              :rows="3" />
          </el-form-item>
        </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as RectifyInfoReportApi from '@/api/server/rectifyinforeport';
import FileUpload from '@/components/FileUpload';
import FilePreview from '@/components/FilePreview';

export default {
  name: "RectifyInfoReportForm",
  components: {
    FileUpload,FilePreview
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        interviewCount: undefined,
        writtenExaminationCount: undefined,
        criticismCount: undefined,
        warningCount: undefined,
        dismissalCount: undefined,
        otherCount: undefined,
        quarter: undefined,
        reporterName: undefined,
        reporterPhone: undefined,
        reportTime: undefined,
        departmentId: undefined,
        reportStatus: undefined,
        reportRemark: undefined,
        attachment: undefined,
      },
      // 表单校验
      formRules: {
        interviewCount: [{ required: true, message: '约谈数量不能为空', trigger: 'blur' }],
        writtenExaminationCount: [{ required: true, message: '书面检查数量不能为空', trigger: 'blur' }],
        criticismCount: [{ required: true, message: '通报批评数量不能为空', trigger: 'blur' }],
        warningCount: [{ required: true, message: '警告或记过处分数量不能为空', trigger: 'blur' }],
        dismissalCount: [{ required: true, message: '调离岗位或免职数量不能为空', trigger: 'blur' }],
        otherCount: [{ required: true, message: '其他数量不能为空', trigger: 'blur' }],
        quarter: [{ required: true, message: '所属季度不能为空', trigger: 'blur' }],
        reporterName: [{ required: true, message: '上报人不能为空', trigger: 'blur' }],
        reporterPhone: [{ required: true, message: '上报人电话不能为空', trigger: 'blur' }],
        reportTime: [{ required: true, message: '上报时间不能为空', trigger: 'blur' }],
        attachment: [{required: true, message: '附件不能为空', trigger: 'blur'}]

      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await RectifyInfoReportApi.getRectifyInfoReport(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 RectifyInfoReportApi.updateRectifyInfoReport(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await RectifyInfoReportApi.createRectifyInfoReport(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        interviewCount: undefined,
        writtenExaminationCount: undefined,
        criticismCount: undefined,
        warningCount: undefined,
        dismissalCount: undefined,
        otherCount: undefined,
        quarter: undefined,
        reporterName: undefined,
        reporterPhone: undefined,
        reportTime: undefined,
        departmentId: undefined,
        reportStatus: undefined,
        reportRemark: undefined,
        attachment: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
<style scoped>
.long-label-item >>> .el-form-item__content {
  display: flex;
}
.long-label-item >>> .el-input {
  flex: 1;
  min-width: 180px;
}
</style>
