<template>
  <audit-form-top-banner :handel-save-form="saveForm" :form-data="mode" @handelSaveForm="saveForm">
    <sd-detail-form
      ref="docform"
      form-id="iamFinSysEtlConfig"
      page-id="dm/finsysetlconfig/iamFinSysEtlConfig"
      :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>
            <!-- 配置名称 -->
            <sd-form-item-td name="configName"> </sd-form-item-td>
            <!-- 数据源ID -->
            <sd-form-item-td name="databaseId">
              <template v-slot:read-and-edit>
                <a-select
                  v-model="model.databaseId"
                  :options="configOptions"
                  allow-clear
                ></a-select>
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 清洗转换,需要执行的ETL文件 -->
            <sd-form-item-td name="transferEtlFile" :label="'ETL\u2002文件名称'" :colspan="3" />
          </tr>

          <tr>
            <!-- 上次执行导入的日期 -->
            <sd-form-item-td name="lastImportTime" label="末次导入日期" />
            <!-- 接入系统代码 -->
            <sd-form-item-td name="appCode">
              <template v-slot:read-and-edit>
                <a-select v-model="model.appCode" :options="orgMapOptions" allow-clear></a-select>
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 默认采集某年开始的数据 -->
            <sd-form-item-td name="defaultBeginYear" label="默认采集年">
              <template v-slot:read-and-edit>
                <iam-dm-date-picker
                  v-model="model.defaultBeginYear"
                  field="defaultBeginYear"
                  @valChange="valChange"
                ></iam-dm-date-picker>
              </template>
            </sd-form-item-td>
            <!-- 默认采集某年开始的数据 -->
            <sd-form-item-td name="collYears" label="采集开始年">
              <template v-slot:read-and-edit>
                <iam-dm-date-picker
                  v-model="model.collYears"
                  field="collYears"
                  @valChange="valChange"
                ></iam-dm-date-picker>
              </template>
            </sd-form-item-td>
          </tr>

          <tr>
            <!-- 财务系统版本信息 -->
            <sd-form-item-td name="financialSystemId" label="财务系统版本" :colspan="3">
              <template v-slot:read-and-edit>
                <a-select
                  v-model="model.financialSystemId"
                  :options="systemOptions"
                  allow-clear
                ></a-select>
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 维护人员 -->
            <sd-form-item-td name="creatorName" />
            <!-- 维护日期 -->
            <sd-form-item-td name="creationTime" />
          </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-finsysetlconfig-form-import'

export default {
  name: 'IamDmFinsysetlconfigForm',
  metaInfo: {
    title: '财务转换配置',
  },
  components: {
    ...components,
    auditAdvancedGroup,
    auditFormTopBanner,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      dateopen: false, // 日历组件打开
      saveFlag: true,
      mode: null,
      basicExpand: true,
      configOptions: [], // 数据源下拉框
      systemOptions: [], // 财务版本下拉
      orgMapOptions: [], // 接入系统代码下拉框
    }
  },
  mounted() {
    this.getOptions()
  },
  methods: {
    // 页面加载完成后
    sdFormReady() {
      this.mode = this.$refs.docform.formData.mode
    },
    // 保存
    saveForm() {
      this.$refs.docform.saveBtnClick()
    },
    saved() {
      message.success({ content: '保存成功' }).then(() => {
        this.close(true)
      })
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    // 时间选择之后赋值
    valChange(field, val) {
      this.$refs.docform.setFieldValue(field, val)
    },
    // 选择数据源之后，赋值ID
    configChange(val) {
      this.$refs.docform.setFieldValue('databaseId', Number(val))
    },
    // 获取数据源及财务版本号
    getOptions() {
      axios({
        url: 'api/xcoa-mobile/v1/iamfinsysetlconfig/getOptions',
        method: 'get',
      }).then((res) => {
        if (res) {
          // 展示name, 字段赋值id
          // res.data.databaseOptions 数据源
          // res.data.finSystemOptions  财务系统版本信息
          this.configOptions = [...res.data.databaseOptions].map((item) => {
            return {
              value: item.id,
              label: item.name,
            }
          })
          this.systemOptions = [...res.data.finSystemOptions].map((item) => {
            return {
              value: item.id,
              label: item.name,
            }
          })
          this.orgMapOptions = [...res.data.orgMapOptions].map((item) => {
            return {
              value: item.appCode,
              label: item.appUnitName,
            }
          })
        }
      })
    },
  },
}
</script>

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