<template>
  <sd-webflow
    ref="flow"
    :class="$style.tylc"
    :header-buttons="[
      'admin-ops',
      'workflow$myCollection',
      'workflow$myCollectionCancel',
      'we-doc$print',
      'workflow$openTodo',
      'we-doc$close',
      'save',
    ]"
    :default-print-blocks="['sd-opinion-log', 'sd-process-info']"
  >
    <template v-slot:print-content-bottom>
      <!-- sd-webflow-print-block组件的内容会被显示项控制，在显示项中勾选时才显示 -->
      <sd-webflow-print-block key="xm-ext-log" title="扩展内容" :data-order="230">
        <div>扩展的内容1</div>
      </sd-webflow-print-block>
    </template>
    <template v-slot:form="{ model, fields, FlowData }">
      <table :class="$style.table">
        <sd-form-colgroup :columns="4" />
        <tr>
          <th :colspan="4">基本信息</th>
        </tr>
        <tr>
          <sd-form-item-td name="title" :colspan="3" />
        </tr>
        <tr>
          <sd-form-item-td name="contractName" :colspan="3" />
        </tr>
        <tr>
          <sd-form-item-td name="currentNumber" />
          <sd-form-item-td name="flowNumber" />
        </tr>
        <tr>
          <sd-form-item-td name="issueOpinion" :colspan="3" />
        </tr>
        <tr>
          <sd-form-item-td name="contractType" />
          <sd-form-item-td name="contractNumber" />
        </tr>
        <tr>
          <sd-form-item-td name="contractProject" />
          <sd-form-item-td name="contractDeadlineType"> </sd-form-item-td>
        </tr>
        <tr>
          <sd-form-item-td name="startTime" />
          <sd-form-item-td name="endTime" range-after="startTime" />
        </tr>
        <tr>
          <sd-form-item-td name="price" />
          <sd-form-item-td name="currencyType" />
        </tr>
        <tr>
          <sd-form-item-td name="signGroupMember" />
          <sd-form-item-td name="operateUserMember" />
        </tr>
        <tr>
          <!-- 会签部门-->
          <sd-form-item-td name="jointSignGroup" :colspan="3" label="会签部门" />
        </tr>
        <tr>
          <!-- 会签人员-->
          <sd-form-item-td name="jointSignUser" :colspan="3" label="会签人员" />
        </tr>
        <tr>
          <sd-form-item-td name="content" :colspan="3">
            <a-textarea v-model="model.content" :rows="4" />
          </sd-form-item-td>
        </tr>
        <tr>
          <sd-form-item-td name="attachment" :colspan="3">
            <template v-slot:read-and-edit="{ editable }">
              <sd-attachment-ex
                v-model="model.attachment"
                :group-id="JSON.parse(fields.attachment.value).value"
                :read-only="!editable"
                :get-taoda-value="() => getTaodaValue()"
                :get-template-url="() => getTemplateUrl()"
                :get-seal-url="() => getYinzhangUrl()"
                :red-header="FlowData.attrs && FlowData.attrs['we-doc$redheader']"
                :put-seal-on="FlowData.attrs && FlowData.attrs['we-doc$putSealOn']"
                :change-file-format="FlowData.attrs && FlowData.attrs['we-doc$changeFileFormat']"
                :clean-copy="FlowData.attrs && FlowData.attrs['we-doc$cleanCopy']"
                :categories="[{ label: '扩展图片分类', id: 'xmcate', order: 170 }]"
                :get-category="
                  (item) => {
                    if (item.FileName.includes('.jpg')) {
                      return 'xmcate'
                    }
                  }
                "
                :max="parseInt(fields.attachment.attr.maxLength) || Infinity"
                :accept="fields.attachment.attr.allowType"
                :max-file-size="parseInt(fields.attachment.attr.maxFileSize) || Infinity"
                :uploadable="fields.attachment.attr.uploadable"
                :zw-uploadable="fields.attachment.attr.zwUploadable"
                :sortable="fields.attachment.attr.sortable"
              />
            </template>
          </sd-form-item-td>
        </tr>
        <tr>
          <sd-form-item-td name="attachmentExtra" :colspan="3">
            <template v-slot:read-and-edit="{ editable }">
              <sd-attachment-ex
                v-model="model.attachmentExtra"
                :group-id="JSON.parse(fields.attachmentExtra.value).value"
                :read-only="!editable"
                :get-taoda-value="() => getTaodaValue()"
                :get-template-url="() => getTemplateUrl()"
                :get-seal-url="() => getYinzhangUrl()"
                :red-header="FlowData.attrs && FlowData.attrs['we-doc$redheader']"
                :put-seal-on="FlowData.attrs && FlowData.attrs['we-doc$putSealOn']"
                :change-file-format="FlowData.attrs && FlowData.attrs['we-doc$changeFileFormat']"
                :clean-copy="FlowData.attrs && FlowData.attrs['we-doc$cleanCopy']"
                :max="parseInt(fields.attachmentExtra.attr.maxLength) || Infinity"
                :accept="fields.attachmentExtra.attr.allowType"
                :max-file-size="parseInt(fields.attachmentExtra.attr.maxFileSize) || Infinity"
                :uploadable="fields.attachmentExtra.attr.uploadable"
                :zw-uploadable="fields.attachmentExtra.attr.zwUploadable"
                :sortable="fields.attachmentExtra.attr.sortable"
              />
            </template>
          </sd-form-item-td>
        </tr>
        <tr>
          <sd-form-item-td name="oaOppositesideEntities" :colspan="4" :label="null">
            <template v-slot:read-and-edit="{ editable }">
              <sd-child-table
                v-model="model.oaOppositesideEntities"
                :read-only="!editable"
                label="相对方信息"
                :fields="Array.from(fields.oaOppositesideEntities.attr.dync)"
              />
            </template>
          </sd-form-item-td>
        </tr>
        <tr>
          <sd-form-item-td
            name="oaCertificateEntities"
            :colspan="4"
            :label="null"
            :input-props="{
              showSelection(record) {
                return record.certificateName !== 'YYZH'
              },
            }"
          >
          </sd-form-item-td>
        </tr>
      </table>
    </template>
  </sd-webflow>
</template>

<script>
import axios from '@/common/services/axios-instance'
import pickValues from '@/common/services/pick-values'
import sdListPickerVue from '@/common/components/sd-list-picker.vue'
import { Message } from 'ant-design-vue'
import components from './_import-components/sd-tylc-import'
export default {
  name: 'SdTylc',
  metaInfo: {
    title: 'SdTylc',
  },
  components,
  data() {
    return {}
  },
  methods: {
    getTemplateUrl() {
      return axios({
        url: `api/framework/v1/redhead-manager/doctype/${this.$refs.flow.getFieldValue(
          'businessTypeId'
        )}/redheads`,
      })
        .then((res) => {
          return pickValues(sdListPickerVue, {
            optionValue: 'attachment',
            optionLabel: 'redheadName',
            singleColumn: true,
            single: true,
            loadListData: () => res.data,
          }).then((value) => {
            return axios
              .get('api/framework/v1/attachment-extend/attachments-info/' + value[0].attachment)
              .then((res) => {
                const code = res.data[0]?.code
                if (code) {
                  return `${location.origin}${location.pathname}api/framework/v1/task-form-process/download-attachments/${code}?fileName=${res.data[0].name}`
                }
              })
          })
        })
        .catch(() => {
          // eslint-disable-next-line prefer-promise-reject-errors
          return Promise.reject(false)
        })
    },
    getTaodaValue() {
      return this.$refs.flow.getDisplayValues().then((data) => {
        const wordlabel = {}
        for (const x in data) {
          wordlabel[
            this.$refs.flow.flowData.processFormData.processFormPropertyValues.find(
              (field) => field.name === x
            )?.caption
          ] = data[x]
        }
        return wordlabel
      })
    },
    getYinzhangUrl() {
      Message.warn('不支持盖章')
      // eslint-disable-next-line prefer-promise-reject-errors
      return Promise.reject('不支持')
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
