<template>
  <audit-form-top-banner :handel-save-form="saveForm" @handelSaveForm="saveForm">
    <sd-detail-form
      ref="docform"
      :page-id="this.$route.query.pageId ? this.$route.query.pageId : null"
      :record-id="this.id"
      :read-only="false"
      :class="[$style.form]"
      @close="close(true)"
      @saved="saved"
    >
      <template v-slot="{ formData, model, fields }">
        <table :class="$style.detailForm">
          <tr>
            <td colspan="4" style="border:none">
              <sd-form-by-builder :form-data="initDaF(formData.pageFormData)"></sd-form-by-builder>
            </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 auditFormTopBanner from '@product/iam/components/audit-form-top-banner'
// 注意此处，需要将-import之前，修改为与文件同名
import components from './_import-components/contract-edit-import'

export default {
  name: 'ContractEdit',
  metaInfo: {
    title: '合同签订',
  },
  components: {
    ...components,
    auditFormTopBanner,
  },
  data() {
    return {
      initialled: false,
      id: this.$route.query.id ? this.$route.query.id : '',
      flag: true,
    }
  },
  methods: {
    saved(t) {
      // 保存后执行
      const idobj = t.pageFormData.pageFieldInfos.find((i) => i.name === 'id').value
      this.id = idobj
      const id = this.$route.params.id ? this.$route.params.id : this.$route.query.id
      if (id === null || id === undefined || id === '') {
        let path = this.$route.fullPath
        path = path.replace('?id=&pageId', '?id=' + this.id + '&pageId')
        window.location.href = '#' + path
      }
    },
    // 保存
    saveForm() {
      // this.wjsdRuleCheck();
      this.droolsRuleCheck()
    },
    droolsRuleCheck() {
      var ruleServiceUrl = 'http://localhost:8082/applyContractRules'
      const contractData = this.getContractData()
      axios({
        method: 'post',
        url: ruleServiceUrl,
        data: contractData,
      }).then((res) => {
        if (res.status === 200) {
          if (res.data.status === '提醒') {
            message.warning('提醒:' + res.data.message)
          }
          if (res.data.status === '阻断') {
            message.error('阻断:' + res.data.message)
          }
          if (res.data.status == null) {
            this.$refs.docform.saveBtnClick()
            return true
          }
        }
      })
      return false
    },
    getContractData() {
      const companyList = [
        '北京东方联合投资管理有限公司',
        '网信集团有限公司',
        '普信资产管理有限公司',
        '北京经讯时代科技有限公司',
        '太极股份',
        '河南**燃气管道有限公司',
        '新乡**发电有限公司',
      ]
      var contractData = {
        companyName:
          companyList[this.$refs.docform.getFieldValue('childTableInline0')[0].xdfmc - 1],
        contractAmount: this.$refs.docform.getFieldValue('htje'),
        netCapital: this.$refs.docform.getFieldValue('szk'),
      }
      return contractData
    },
    wjsdRuleCheck() {
      var ruleServiceUrl = 'http://172.20.98.64:9999/rule/callRule/service'
      var ruleData = {
        user: 'admin',
        password: '1',
        request: {
          ruleName: 'POC-001',
          ruleVersion: '1',
          ruleFact: [],
        },
      }
      const contractData = this.getContractData()
      ruleData.request.ruleFact = [contractData]
      console.log(ruleData)
      console.log(JSON.stringify(ruleData))
      const fd = new FormData()
      fd.append('req', JSON.stringify(ruleData))
      axios({
        method: 'post',
        url: ruleServiceUrl,
        data: fd,
      }).then((res) => {
        console.log(res)
        const data = res.data
        if (data.response.ruleCode === '0000') {
          if (data.response.ruleFact.status === '提醒') {
            message.warning('提醒:' + data.response.ruleFact.message)
          }
          if (data.response.ruleFact.status === '阻断') {
            message.error('阻断:' + data.response.ruleFact.message)
          }
          if (data.response.ruleFact.status === 'OK') {
            this.$refs.docform.saveBtnClick()
            return true
          }
        } else {
          message.error('数据请求错误:' + data.response.ruleMsg)
        }
      })
      return false
    },
    close(flag) {
      window.close()
    },
    initDaF(array) {
      if (array) {
        array.attrFD.keepHiddenCell = false
      }
      if (this.flag) {
        setTimeout(() => {
          this.$refs.docform.setFieldValue('BASE_INFO', 'true')
          this.$refs.docform.setFieldValue('SUPPORT_LIST', 'true')
        }, 1000)
        this.flag = false
      }
      return array
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
@import '@/webflow/sd-flow-form.scss';
.detailForm {
  width: 100% !important;
  margin: 0;
  :global(.sd-form-table) {
    // width: 100% !important;
  }
}
</style>
