<template>
  <audit-form-top-banner
    :handel-publish="publishflag ? handelPublish : null"
    :handel-save-form="saveflag ? saveForm : null"
    @handelSaveForm="saveForm"
    @handelPublish="handelPublish"
  >
    <sd-detail-form
      ref="docform"
      :page-id="this.$route.query.pageId ? this.$route.query.pageId : null"
      :record-id="this.$route.query.id ? this.$route.query.id : null"
      :class="[$style.form]"
      :read-only="readOnly"
      @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 auditFormTopBanner from '@product/iam/components/audit-form-top-banner'
import components from './_import-components/law-record-eval-form-import'
import axios from '@/common/services/axios-instance'
import { message } from '@/common/one-ui'
import { getUserInfo } from '@/common/store-mixin'

export default {
  name: 'LawRecordEvalForm',
  metaInfo: {
    title: '服务记录评价',
  },
  components: {
    ...components,
    auditFormTopBanner,
  },
  data() {
    return {
      flag: true,
      publishflag: false,
      saveflag: false,
      readOnly: true,
    }
  },

  methods: {
    initDaF(array) {
      if (array) {
        array.attrFD.keepHiddenCell = false

        // 发布状态
        if (this.flag) {
          setTimeout(() => {
            this.$refs.docform.setFieldValue('JBXX', 'true')
            this.$refs.docform.setFieldValue('LAW_FIRM_LSPJ', 'true')
            this.$refs.docform.setFieldValue('LAW_LAWYER_LSPJ', 'true')
            const publishStatus = this.$refs.docform.getFieldValue('PUBLISH_STATUS')
            const evaluatorCode = this.$refs.docform.getFieldValue('EVALUATOR_CODE')
            const userInfo = getUserInfo()
            if (publishStatus !== '已评价' && evaluatorCode === userInfo.account) {
              this.publishflag = true
              this.saveflag = true
              this.readOnly = false
            }
          }, 0)
          this.flag = false
        }
      }
      return array
    },

    handelPublish() {
      // 发布，保存
      this.$refs.docform.setFieldValue('PUBLISH_STATUS', '已评价')
      this.$refs.docform.setFieldValue('EVALUATE_DATA', new Date().getTime())

      this.saveForm()

      //  律所评价信息回写到服务记录
      const firmList = this.$refs.docform.getFieldValue('LAW_FIRM_EVAL')
      if (firmList.length > 0) {
        axios({
          url: 'api/xcoa-mobile/v1/law-service-record/saveFirmData',
          method: 'post',
          data: firmList,
        })
      }

      //  律师评价信息回写到服务记录
      const lawyerList = this.$refs.docform.getFieldValue('LAW_LAWYER_EVAL')
      if (lawyerList.length > 0) {
        axios({
          url: 'api/xcoa-mobile/v1/law-service-record/saveLawyerData',
          method: 'POST',
          data: lawyerList,
        })
      }
    },

    saved() {
      let content = '保存成功！'
      const publishStatus = this.$refs.docform.getFieldValue('PUBLISH_STATUS')
      if (publishStatus === '已评价') {
        content = '发布成功！'
      }
      message.success({ content: content }, 1).then(() => {
        this.close(true)
      })
    },

    // 保存
    saveForm() {
      //  律所评价信息赋值服务记录id
      const firmObj = this.$refs.docform.getFieldValue('LAW_FIRM_EVAL')
      if (firmObj.length > 0) {
        firmObj.forEach((firms) => {
          const lawServiceId = this.$refs.docform.getFieldValue('LAW_SERVICE_ID')
          firms.SERVICE_ID = lawServiceId
        })
        this.$refs.docform.setFieldValue('LAW_FIRM_EVAL', firmObj)
      }
      //  律师评价信息赋值服务记录id
      const lawyerObj = this.$refs.docform.getFieldValue('LAW_LAWYER_EVAL')
      if (lawyerObj.length > 0) {
        lawyerObj.forEach((lawyers) => {
          const lawServiceId = this.$refs.docform.getFieldValue('LAW_SERVICE_ID')
          lawyers.SERVICE_ID = lawServiceId
        })
        this.$refs.docform.setFieldValue('LAW_LAWYER_EVAL', lawyerObj)
      }
      this.$refs.docform.saveBtnClick()
    },
    close(flag) {
      window.close()
    },
  },
}
</script>

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