<template>
  <audit-form-top-banner :handel-save-form="saveForm" :form-data="mode" @handelSaveForm="saveForm">
    <sd-detail-form
      ref="docform"
      form-id="iamCubeContract"
      page-id="cube/contract/iamCubeContract"
      :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
      :class="$style.form"
      @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="cubeExpand"
                :expand-str="'cubeExpand'"
                :group-label="'基本信息'"
                tablestyle="''"
                @changedClick="changedClick"
              ></audit-advanced-group> </td></tr
        ></table>

        <table v-show="cubeExpand">
          <colgroup>
            <col style="width: 200px;" />
            <col />
            <col style="width: 200px;" />
            <col />
          </colgroup>
          <tr>
            <!-- 合同名称 -->
            <sd-form-item-td name="contractName" :colspan="3" />
          </tr>
          <tr>
            <!-- 合同正文 -->
            <sd-form-item-td name="contractFile" :colspan="3" />
          </tr>
          <tr>
            <!-- 合同编号 -->
            <sd-form-item-td name="contractCode" />
            <!-- 合同类型 -->
            <sd-form-item-td name="contractTypeCode" label="合同类型">
              <template v-slot:read-and-edit>
                <a-select
                  v-model="model.contractTypeCode"
                  :options="typeOptions"
                  @change="typeChange"
                ></a-select>
              </template>
            </sd-form-item-td>
            <!-- 合同类型中文 -->
            <sd-form-item-td name="contractType" :hidden="true"> </sd-form-item-td>
          </tr>
          <tr>
            <!-- 合同金额 -->
            <sd-form-item-td name="contractAmount">
              <a-input-number
                v-model="model.contractAmount"
                :min="0"
                :precision="2"
                style="width: 100%;"
              ></a-input-number>
              <template v-slot:read>
                {{
                  model.contractAmount === undefined
                    ? model.contractAmount
                    : model.contractAmount.toFixed(2)
                }}
              </template>
            </sd-form-item-td>
            <!-- 签订日期 -->
            <sd-form-item-td name="signDate" />
          </tr>
          <tr>
            <!-- 合作方名称 -->
            <sd-form-item-td name="partnerName" :colspan="3" />
          </tr>
        </table>
      </template>
    </sd-detail-form>
  </audit-form-top-banner>
</template>

<script>
import { sdLocalStorage } from '@/common/services/storage-service'
import { Modal } from '@/common/one-ui'
import cubeServices from '@product/iam/cube/cube-services'
import axios from '@/common/services/axios-instance'
import { Message } from 'ant-design-vue'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditFormTopBanner from '../../components/audit-form-top-banner'
import auditAdvancedGroup from '../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import components from './_import-components/cube-contract-form-import'

export default {
  name: 'CubeContractForm',
  metaInfo: {
    title: '合同信息录入',
  },
  components: {
    ...components,
    auditAdvancedGroup,
    auditFormTopBanner,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      mode: null,
      cubeExpand: true,
      typeOptions: [],
    }
  },
  created() {},
  methods: {
    // 合同类型选择后，赋值中文
    typeChange(val) {
      const text = this.typeOptions.find((item) => {
        return item.value === val
      }).label
      this.$refs.docform.setFieldValue('contractType', text)
    },
    // 页面加载完成后
    sdFormReady() {
      this.mode = this.$refs.docform.formData.mode
      cubeServices.checkCubeToken('review').then((res) => {
        if (!res) {
          Modal.warning({
            title: '提示',
            content: '未开启CUBE集成功能参数或单点认证失败，请联系管理员',
          })
          return false
        }
        const cubeJson = JSON.parse(sdLocalStorage.getItem('cube') || '{}')
        axios({
          url:
            'api/xcoa-mobile/v1/iamcubecontract/findFileCode?accessToken=' + cubeJson.review_token,
        }).then((res) => {
          this.typeOptions = res.data.map((item) => {
            return {
              value: item.code,
              label: item.text,
            }
          })
        })
      })
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    // 保存
    saveForm() {
      this.$refs.docform.validateFields().then(() => {
        Message.success('保存成功').then(() => {
          this.$refs.docform.saveBtnClick()
        })
      })
    },
    saved() {
      this.close(true)
    },
  },
}
</script>

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