<template>
  <audit-form-top-banner :handel-save-form="saveForm" :form-data="mode" @handelSaveForm="saveForm">
    <sd-detail-form
      ref="docform"
      form-id="iamFinSysOrgMapMain"
      page-id="dm/finsysorgmap/iamFinSysOrgMapMain"
      :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
      :class="$style.form"
      :read-only="!saveFlag"
      @close="close(true)"
      @saved="saved"
      @sdFormReady="sdFormReady"
    >
      <template v-slot="{ model, fields }">
        <table>
          <tr>
            <td style="padding-left:5px;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">
          <tr>
            <!-- null -->
            <sd-form-item-td name="appCode" @change="setAppCode" />
            <!-- 接入系统组织机构代码 -->
            <sd-form-item-td name="appCodeName" />
          </tr>
          <tr>
            <!-- XX平台组织机构代码 -->
            <sd-form-item-td name="remark" :colspan="3">
              <a-textarea v-model="model.remark" :auto-size="{ minRows: 3, maxRows: 3 }" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- id -->
            <sd-form-item-td name="id" :hidden="true" />
          </tr>
        </table>

        <table>
          <tr
            ><td :colspan="4">
              <sd-form-item name="iamFinSysOrgMapEntitys" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="orgExpand"
                    :expand-str="'orgExpand'"
                    :group-label="'部门映射信息'"
                    @changedClick="changedClick"
                  >
                    <xm-child-table
                      v-if="orgExpand"
                      ref="tabaData"
                      v-model="model.iamFinSysOrgMapEntitys"
                      :class="$style.deptPlanChild"
                      :label="null"
                      :read-only="!editable"
                      :fields="Array.from(fields.iamFinSysOrgMapEntitys.attr.dync)"
                      :columns="orgChildColumns"
                      @change="changeNum"
                    >
                      <template v-slot:input-form="{ model }">
                        <!-- <sd-form-item name="appCode" :input-props="{ defaultValue: appCode }" /> -->
                        <sd-form-item name="mainId" />
                        <!-- 接入系统组织机构代码 -->
                        <sd-form-item name="appUnitCode" />
                        <!-- XX平台组织机构代码 -->
                        <sd-form-item name="systemUnitCode" />
                        <!-- 接入系统组织机构名称 -->
                        <sd-form-item name="appUnitName" />
                        <!-- XX平台组织机构名称 -->
                        <sd-form-item name="systemUnitName" />
                      </template>
                    </xm-child-table>
                  </audit-advanced-group>
                </template>
              </sd-form-item>
            </td>
          </tr>
        </table>
      </template>
    </sd-detail-form>
  </audit-form-top-banner>
</template>

<script>
import axios from '@/common/services/axios-instance'
import { message } from '@/common/one-ui'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditFormTopBanner from '@product/iam/components/audit-form-top-banner'
import auditAdvancedGroup from '@product/iam//components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '@product/iam//components/audit-advanced-group-mixins'
import components from './_import-components/iam-dm-finsysorgmap-form-import'

export default {
  name: 'IamDmFinsysorgmapForm',
  metaInfo: {
    title: '接入系统组织机构映射',
  },
  components: {
    ...components,
    auditAdvancedGroup,
    auditFormTopBanner,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      saveFlag: true,
      mode: null,
      basicExpand: true,
      orgExpand: true,
      orgChildColumns: [
        {
          title: '序号',
          dataIndex: 'indexNum',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'appCode', sdHidden: true },
        { dataIndex: 'appUnitCode', sdHidden: false },
        { dataIndex: 'systemUnitCode', sdHidden: false },
        { dataIndex: 'appUnitName', sdHidden: false },
        { dataIndex: 'systemUnitName', sdHidden: false },
      ],
      appCode:'',
    }
  },
  methods: {
    // 页面加载完成后
    sdFormReady() {
      this.mode = this.$refs.docform.formData.mode
    },
    // 保存
    saveForm() {
      const appCode = this.$refs.docform.getFieldValue('appCode')
      const data = this.$refs.docform.getFieldValue('iamFinSysOrgMapEntitys')
      data.forEach((d)=>{
        d.appCode = appCode
      })
      this.$refs.docform.setFieldValue('iamFinSysOrgMapEntitys',data)
      this.$refs.docform.saveBtnClick()
    },
    saved() {
      message.success({ content: '保存成功' }).then(() => {
        this.close(true)
      })
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    setAppCode() {
      const appCode = this.$refs.docform.getFieldValue('appCode')
      if (appCode) {
        this.appCode = appCode
        return appCode
      }
    },
    // 序号赋值
    changeNum(data) {
      data.forEach((item, index) => {
        item.indexNum = index + 1
        // item.appCode = this.setAppCode
        item.appCode = this.appCode
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
@import '@/webflow/sd-flow-form.scss';
</style>
