<template>
  <div>
    <sd-webflow ref="flow" :validate-form="validForm" :removed-tabs="['sdRelatedDoc']">
      <template v-slot:form="{ model, fields }">
        <table>
          <tr v-show="initData()">
            <!-- taskID -->
            <sd-form-item-td name="taskId" />
          </tr>
          <tr>
            <!-- 报告名称 -->
            <sd-form-item-td name="title" />
            <!-- 报告文号 -->
            <sd-form-item-td name="reportNumber" />
          </tr>
          <tr>
            <!-- 文书编号 -->
            <sd-form-item-td name="docNumber" />
            <!-- 编制日期 -->
            <sd-form-item-td name="creationTime" />
          </tr>
          <tr>
            <!-- 报告内容 -->
            <sd-form-item-td name="reportDesc" :colspan="3">
              <a-textarea v-model="model.reportDesc" :auto-size="{ maxRows: 3, minRows: 3 }" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 创建人 -->
            <sd-form-item-td name="creatorName" />
            <!-- 创建单位 -->
            <sd-form-item-td name="createDeptName" />
          </tr>
          <tr>
            <!-- 附件 -->
            <sd-form-item-td name="attachment" :colspan="3">
              <template v-slot:read-and-edit="{ editable }">
                <sd-attachment
                  v-model="model.attachment"
                  :group-id="JSON.parse(fields.attachment.value).value"
                  :read-only="!editable"
                ></sd-attachment>
              </template>
            </sd-form-item-td>
          </tr>
        </table>
      </template>
    </sd-webflow>
  </div>
</template>

<script>
import riskTasksService from '../risk-tasks-service'
import components from './_import-components/risk-task-report-form-import'
export default {
  name: 'RiskTaskReportForm',
  metaInfo: {
    title: '风险任务报告',
  },
  components,
  data() {
    return { taskId: null }
  },
  created() {
    // 获取taskId
    if (this.$route.query.extParams) {
      this.taskId = JSON.parse(this.$route.query.extParams).taskId
    }
  },
  methods: {
    initData() {
      if (this.taskId !== null) {
        this.$refs.flow?.setFieldValue('taskId', parseInt(this.taskId))
      }
      return false
    },
    validForm() {
      return new Promise((resolve, reject) => {
        var serialNumber = this.$refs.flow.getFieldValue('reportNumber')
        if (serialNumber === '' || serialNumber === undefined) {
          const params = {
            taskId: this.taskId,
          }
          riskTasksService.getNextNumber(params).then((res) => {
            this.$refs.flow.setFieldValue('reportNumber', res.data)
            resolve(true)
          })
        } else {
          resolve(true)
        }
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
