<template>
  <span>
    <audit-form-top-banner
      :handel-save-form="saveForm"
      :form-data="mode"
      @handelSaveForm="saveForm"
    >
      <sd-detail-form
        ref="docform"
        page-id="audit/work/reportFind/spicAuditReportFind"
        :record-id="recodeId"
        :class="$style.form"
        :read-only="readOnly"
        @close="close(true)"
        @saved="saved"
        @sdFormReady="
          () => {
            this.sdFormReady()
          }
        "
      >
        <template v-slot="{ model, fields }">
          <table
            ><tr
              ><td style="border: none">
                <audit-advanced-group
                  :expand="basicExpand"
                  :expand-str="'basicExpand'"
                  :group-label="'基本信息'"
                  tablestyle="''"
                  @changedClick="changedClick"
                ></audit-advanced-group> </td></tr
          ></table>

          <table v-show="basicExpand">
            <colgroup>
              <col style="width: 220px;"/>
              <col />
              <col style="width: 220px;"/>
              <col
            /></colgroup>
            <tr v-show="initData(model)">
              <!-- projectId -->
              <sd-form-item-td name="projectId" :colspan="3" />
            </tr>
            <tr class="bklabeloverflow">
              <!-- 审计发现标题 -->
              <sd-form-item-td label="名称" name="findTitle" :colspan="3">
                <a-input v-model="model.findTitle" />
                <!-- <span style="padding: 5px">
                  <a-button @click="showfind">选择</a-button>
                </span> -->
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 审计发现编号 -->
              <sd-form-item-td label="编号" name="findCode">
                <template v-slot>
                  <a-input v-model="model.findCode" placeholder="自动生成" read-only></a-input>
                </template>
              </sd-form-item-td>
              <!-- 被审计单位 -->
              <sd-form-item-td name="auditedUnit"> </sd-form-item-td>
            </tr>
            <tr>
              <sd-form-item-td label="描述" name="findDesc" :colspan="3">
                <template v-slot>
                  <a-textarea
                    v-model="model.findDesc"
                    :auto-size="{ minRows: 3, maxRows: 3 }"
                  ></a-textarea>
                </template>
              </sd-form-item-td>
            </tr>
            <tr class="iamauditfindform">
              <!-- 是否下达审计意见书 -->
              <sd-form-item-td name="giveOpinion" :label="'是否属于审计意见书问题'" />
              <!-- 问题性质 -->
              <!-- <sd-form-item-td name="problemNature"> </sd-form-item-td> -->
              <sd-form-item-td name="problemNature">
                <a-select v-model="model.problemNature" :allow-clear="true">
                  <a-select-option
                    v-for="item in fields.problemNature.attr.selectListItem.filter((itc) =>
                      ['fx', 'yj'].includes(itc.value)
                    )"
                    :key="item.value"
                    :value="item.value"
                  >
                    {{ item.label }}
                  </a-select-option>
                </a-select>
              </sd-form-item-td>
            </tr>

            <!-- <tr class="iamauditfindform">
              <sd-form-item-td name="problemCategoryFunction" :label="'问题分类'">
                <template v-slot>
                  <a-input v-model="problemCategoryFunction" read-only @click="showfindcat" />
                  <a-input v-model="model.problemCategoryFunction" :hidden="true" />
                </template>
                <template v-slot:read>
                  {{ JSON.parse(model.problemCategoryFunction).text }}
                </template></sd-form-item-td
              >
            </tr> -->
            <!-- <tr> -->
            <!-- 整改责任主体-->
            <!-- <sd-form-item-td name="respUnitName" /> -->
            <!-- 发送整改状态 -->
            <!-- <sd-form-item-td name="sendStatus" /> -->
            <!-- </tr> -->
            <!-- 备注 -->
            <!-- <tr>
              <sd-form-item-td name="findRemark" :colspan="3">
                <template v-slot>
                  <a-textarea
                    v-model="model.findRemark"
                    :auto-size="{ minRows: 3, maxRows: 3 }"
                  ></a-textarea>
                </template>
              </sd-form-item-td>
            </tr> -->
          </table>
        </template>
      </sd-detail-form>
    </audit-form-top-banner>
  </span>
</template>

<script>
import { Message, Modal } from 'ant-design-vue'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditMattersSelectModal from '../../../matters/audit-matters-select-modal.vue'

import auditFormTopBanner from '../../../../components/audit-form-top-banner.vue'
import auditAdvancedGroup from '../../../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../../../components/audit-advanced-group-mixins'
import AuditFindCatTree from '../../../../components/audit-find-cat-tree.vue'
import AuditFindCatTreeMixins from '../../../../components/audit-find-cat-tree-mixins'
import iamAuditService from './iam-audit-find-service'
import components from './_import-components/xm-audit-find-form-import'

export default {
  name: 'XmAuditFindForm',
  metaInfo: {
    title: '审计建议及风险表单',
  },
  components: {
    ...components,
    auditFormTopBanner,
    auditAdvancedGroup,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      basicExpand: true,
      expressions: [],
      findcatvisible: false,
      findcat: '',
      mode: null,
      readOnly: false,
      findType: '',
      // problemCategoryFunction: '',
      projectId: this.$route.query.projectId, // 项目ID
      projectInfo: null, // 项目信息
      recodeId: this.$route.query.record ? parseInt(this.$route.query.record) : null,

      findvisible: false,
      flag: true,
      visible: false,
    }
  },
  mounted() {},
  methods: {
    showfindcat() {
      this.findcatvisible = !this.findcatvisible
    },
    // 页面加载完成后
    sdFormReady() {
      // debugger
      const projectStatus = this.$root.$route.query.projectStatus
      if (projectStatus === '05') {
        this.mode = 'VIEW'
        this.readOnly = true
      } else {
        const edit = this.$route.query.edit
        // 控制编辑
        if (edit === 'false') {
          this.mode = 'VIEW'
          this.readOnly = true
        } else {
          this.mode = this.$refs.docform.formData.pageFormData.pageFieldInfos.find(
            (item) => item.name === 'findTitle'
          ).readonly
            ? 'VIEW'
            : 'EDIT'
        }
      }
    },
    // 增加序号列
    getFields(fields) {
      const xh = [
        {
          caption: '序号',
          dataType: 'string',
          index: 0,
          mobileVisible: true,
          name: 'xh',
          width: '30px',
        },
      ]
      // fields.reverse()
      fields = fields.filter((item) => {
        return item.name !== 'projectId'
      })
      // return [...fields]
      return [...xh, ...fields]
    },
    initData(model) {
      if (this.flag) {
        if (this.recodeId != null) {
          this.projectId = model.projectId
        } else {
          // 设置审计单位默认值
          iamAuditService.getAuditedUnitById(this.projectId).then((res) => {
            setTimeout(() => {
              this.$refs.docform.setFieldValue('auditedUnit', res.data)
            }, 1000)
            // 设置projectId
            setTimeout(() => {
              this.$refs.docform.setFieldValue('projectId', parseInt(this.projectId))
            }, 1000)
          })
        }

        // const findcatstr = model.problemCategoryFunction
        // if (findcatstr !== '' && findcatstr !== undefined) {
        //   const findcatobj = JSON.parse(findcatstr)
        //   this.problemCategoryFunction = findcatobj.text
        // }
        this.flag = false
      }
      return false
    },

    handleBeforeAdd(val, callback) {
      const fileCode = this.$refs.docform.getFieldValue('findCode')
      if (fileCode && fileCode !== '') {
        callback(false)
      } else {
        // 生成文件编号
        iamAuditService.getFileCode(this.projectId).then((res) => {
          this.$refs.docform.setFieldValue('findCode', res.data)
          callback(false)
        })
      }
    },
    // 保存
    saveForm() {
      this.handleBeforeAdd(null, (res) => {
        this.$refs.docform.saveBtnClick()
      })
    },
    saved() {
      Message.success('保存成功', 1).then((res) => {
        this.close(true)
      })
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
  },
}
</script>

<style module lang="scss">
@import '@/webflow/sd-flow-form.scss';
.first-tr {
  td {
    background: #fff !important;
    border: none !important;
  }
}

.btnselect {
  position: relative;
  float: right;
  .batchselect {
    top: 4px;
    z-index: 100;
    margin-right: 200px;
  }
}
.form {
  :global .iamauditfindform > td.ant-form-item-label > label {
    width: 220px;
    white-space: break-spaces !important;
  }
}
</style>
