<template>
  <audit-form-top-banner :handel-save-form="saveForm" :form-data="mode" @handelSaveForm="saveForm">
    <sd-detail-form
      ref="iamOrgDetail"
      form-id="iamOrg"
      page-id="core/org/iamOrg"
      :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
      :class="$style.form"
      :read-only="readOnly"
      @close="close(true)"
      @saved="SJJGKSave"
      @sdFormReady="
        () => {
          this.sdFormReady()
        }
      "
    >
      <template v-slot="{ model }">
        <table>
          <colgroup>
            <col style="width: 210px" />
            <col />
            <col style="width: 210px" />
            <col />
          </colgroup>
          <tr>
            <!-- 机构编号 -->
            <sd-form-item-td name="orgId" />
            <!-- 机构名称 -->
            <sd-form-item-td name="orgDeptMember" label="机构名称" @change="getOrgId(model)" />
          </tr>
          <tr>
            <!-- 机构类型 -->
            <sd-form-item-td name="orgType" component="a-select" />
            <!-- 联系方式 -->
            <sd-form-item-td name="contract" />
          </tr>
          <tr>
            <!-- 地址 -->
            <sd-form-item-td name="address" :colspan="3" />
          </tr>
          <tr>
            <!-- 是否设立总审计师 -->
            <sd-form-item-td name="whetherEstablishAuditor" />
            <!-- 是否设立审计中心 -->
            <sd-form-item-td name="whetherEstablishAuditCenter" />
          </tr>
          <tr>
            <!-- 编制人数 -->
            <sd-form-item-td
              name="personNumber"
              :input-props="{ min: 0, precision: 0, style: 'width:100%' }"
            />
            <!-- 实有人数 -->
            <sd-form-item-td
              name="realNumber"
              :input-props="{ min: 0, precision: 0, style: 'width:100%' }"
            />
          </tr>
          <tr>
            <!-- 维护人员 -->
            <sd-form-item-td name="maintainPerson" />
            <!-- 维护日期 -->
            <sd-form-item-td name="maintainTime" />
          </tr>
          <tr>
            <!-- 排序号 -->
            <sd-form-item-td name="reservelong1" label="排序号" :colspan="3" />
          </tr>
          <tr>
            <!-- 父级ID -->
            <sd-form-item-td name="parentId" :hidden="true" />
            <sd-form-item-td name="orgLibraryCategory" :hidden="true" />
          </tr>
        </table>
      </template>
    </sd-detail-form>
  </audit-form-top-banner>
</template>

<script>
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import { Message } from 'ant-design-vue'
import axios from '@/common/services/axios-instance'
import auditFormTopBanner from '../../components/audit-form-top-banner'
import components from './_import-components/iam-audit-auditsource-org-form-import'

export default {
  name: 'IamAuditAuditsourceOrgForm',
  metaInfo: {
    title: '审计机构库',
  },
  components: {
    ...components,
    auditFormTopBanner,
  },
  data() {
    return {
      readOnly: false,
      mode: null,
      type: 'SJ',
    }
  },
  created() {
    this.mode = this.$route.query.mode
    if (this.$route.query.type !== undefined) {
      this.type = this.$route.query.type
    }
  },
  mounted() {
    setTimeout(() => {
      this.setParentId()
    }, 500)
  },
  methods: {
    // 页面加载完成后
    sdFormReady() {
      if (this.mode === 'VIEW') {
        this.readOnly = true
      } else {
        this.mode = this.$refs.iamOrgDetail.formData.mode
      }
    },
    SJJGKSave() {
      Message.success('保存成功', 1).then((res) => {
        this.close(true)
      })
    },
    // 保存
    saveForm() {
      var orgId = this.$refs.iamOrgDetail.getFieldValue('orgId')
      var recordId = this.$route.query.record ? this.$route.query.record : null
      if (orgId) {
        axios({
          url:
            'api/xcoa-mobile/v1/iamorg/validate/orgId?recordId=' +
            recordId +
            '&orgId=' +
            orgId +
            '&type=' +
            this.type,
          method: 'get',
        }).then((res) => {
          if (res.status === 200) {
            if (res.data) {
              this.setParentId()
              this.$refs.iamOrgDetail.setFieldValue('orgLibraryCategory', this.type)
              this.$refs.iamOrgDetail.saveBtnClick()
            } else {
              Message.error('该机构已存在', 1)
            }
          }
        })
      }
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    setParentId() {
      var parentId = this.$route.query.parentId
      if (parentId) {
        this.$refs.iamOrgDetail.setFieldValue('parentId', parentId)
      }
    },
    getOrgId(model) {
      var orgDeptMember = model.orgDeptMember
      var code = orgDeptMember[0].code
      this.$refs.iamOrgDetail.setFieldValue('orgId', code)
      if (orgDeptMember[0].props) {
        const orgType = orgDeptMember[0].props.lev
        this.$refs.iamOrgDetail.setFieldValue(
          'orgType',
          orgType === 'COMPANY'
            ? '01'
            : orgType === 'GROUP'
            ? '01'
            : orgType === 'DEPARTMENT'
            ? '02'
            : ''
        )
      } else {
        this.$refs.iamOrgDetail.setFieldValue('orgType', '01')
      }
    },
  },
}
</script>

<style module lang="scss">
@import '@/webflow/sd-flow-form.scss';

.ant-card-body {
  padding: 12px;

  :global(.buttons_sd-detail-form_common) {
    :global(.ant-form-item-control-wrapper) {
      padding-top: 4%;
      padding-left: 24%;
    }
  }

  .tr_border {
    box-sizing: border-box;
    border-right: 1px solid #e8e8e8;
    border-left: 1px solid #e8e8e8;
  }

  :global(.title_sd-detail-form_common) {
    font-size: 1.5em;
    text-align: center;
  }
}
</style>
