<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"
      form-id="iamAuditEvidenceSheet"
      page-id="audit/evidence/iamAuditEvidenceSheet"
      :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
      :class="$style.form"
      :read-only="readOnly"
      @close="close(true)"
      @saved="saved"
    >
      <template v-slot="{ model, fields }">
        <table
          ><tr
            ><td style="border: none">
              <audit-advanced-group
                :expand="templateExpand"
                :expand-str="'templateExpand'"
                :group-label="'基本信息'"
                tablestyle="''"
                @changedClick="changedClick"
              ></audit-advanced-group> </td></tr
        ></table>

        <table v-show="templateExpand">
          <tr :class="$style.firstTr">
            <td class="ant-form-item-label ant-form-item-label"></td>
            <td></td>
            <td class="ant-form-item-label ant-form-item-label"></td>
            <td></td>
          </tr>
          <tr>
            <sd-form-item-td
              name="projectId"
              :colspan="3"
              :input-props="{ defaultValue: projectId }"
              :hidden="true"
            ></sd-form-item-td
          ></tr>
          <tr>
            <!-- 审计事项 -->
            <sd-form-item-td v-if="savecontent()" name="itemTitle" />
            <!-- 取证单编号 -->
            <sd-form-item-td name="itemCode">
              <template v-slot:read-and-edit="{ editable }">
                <a-input
                  v-if="editable"
                  v-model="model.itemCode"
                  placeholder="系统自动"
                  read-only
                ></a-input>
                <span v-else>{{ model.itemCode }}</span>
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 被审计单位 -->
            <sd-form-item-td name="auditedUnitMember">
              <sd-group-picker v-model="auditedUnitMember" />
            </sd-form-item-td>
            <!-- 被审计人员 -->
            <sd-form-item-td name="linkmanName">
              <template v-slot:read-and-edit="{ editable }">
                <a-input
                  v-if="editable"
                  v-model="model.linkmanName"
                  placeholder="系统自动"
                  read-only
                ></a-input>
                <span v-else>{{ model.linkmanName }}</span>
              </template>
            </sd-form-item-td>
          </tr>

          <tr>
            <!-- 审计（调查）事项摘要 -->
            <sd-form-item-td name="itemDesc" :colspan="3">
              <a-textarea v-model="model.itemDesc" :rows="3" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 编制人员 -->
            <sd-form-item-td v-if="initparams(model)" name="creatorName" />
            <!-- 编制日期 -->
            <sd-form-item-td name="creationTime" />
          </tr>
          <tr>
            <!-- 附件 -->
            <sd-form-item-td name="attachment" :colspan="3">
              <template v-slot:read-and-edit>
                <sd-attachment
                  v-model="model.attachment"
                  :group-id="JSON.parse(fields.attachment.value).value"
                  :read-only="readOnly"
                />
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <sd-form-item-td name="docStatus" :hidden="true" :colspan="3" />
          </tr> </table
        ><table>
          <tr
            ><td :colspan="4">
              <sd-form-item name="iamAuditEvidenceEntity" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="expandC"
                    :expand-str="'expandC'"
                    :group-label="'审计证据'"
                    @changedClick="changedClick"
                    ><template>
                      <xm-child-table
                        ref="evidenceForm"
                        v-model="model.iamAuditEvidenceEntity"
                        :class="{ [$style.sjEvidence]: !readOnly }"
                        :read-only="!editable"
                        label=""
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'number',
                              attr: {},
                            },
                          ].concat(Array.from(fields.iamAuditEvidenceEntity.attr.dync))
                        "
                        :columns="childColumns"
                        :width="1200"
                        @change="projectIdChange"
                      >
                        <template v-slot:pageNum="{ field, text, record, value, index }">
                          <a-input-number v-model="value[index].pageNum" :min="0"></a-input-number>
                        </template>
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template> </sd-form-item
            ></td>
          </tr>
        </table>
      </template>
    </sd-detail-form>
  </audit-form-top-banner>
</template>

<script>
import { Message } from 'ant-design-vue'
import { getUserInfo } from '@/common/store-mixin'
import axios from '@/common/services/axios-instance'
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/audit-evidence-from-import'

export default {
  name: 'AuditEvidenceFrom',
  metaInfo: {
    title: '审计取证表',
  },
  components: {
    ...components,
    auditAdvancedGroup,
    auditFormTopBanner,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      isIni: false,
      message: '',
      templateExpand: true,
      expandC: true,
      readOnly: false,
      publishflag: true,
      saveflag: true,
      projectId: this.$root.$route.query.projectId,
      childColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'id', sdHidden: true },
        { dataIndex: 'projectId', sdHidden: true },
        { dataIndex: 'evidenceTitle', title: '名称' },
        { dataIndex: 'evidenceCode', width: '180px' },
        { dataIndex: 'pageNum', width: '110px' },
      ],
      auditedUnitMember: [],
      linkmanName: '',
      flag: true,
    }
  },

  created() {},

  methods: {
    savecontent() {
      if (this.linkmanName !== '') {
        this.$refs.docform.setFieldValue('linkmanName', this.linkmanName)
      }
      if (this.auditedUnitMember.length > 0) {
        this.$refs.docform.setFieldValue('auditedUnitMember', this.auditedUnitMember)
      }
      return true
    },
    initparams(model) {
      if (this.isIni) return
      this.isIni = true
      const userInfo = getUserInfo()
      if (model.docStatus === '已结束' || model.creatorName !== userInfo.name) {
        this.readOnly = true
        this.saveflag = false
        this.publishflag = false
      } else if (model.docStatus === '已结束') {
        this.readOnly = false
        this.saveflag = true
        this.publishflag = false
      }
      if (model.docStatus === '已结束' && model.creatorName === userInfo.name) {
        this.readOnly = false
        this.saveflag = true
        this.publishflag = true
      }
      if (this.flag) {
        this.setAuditUnit(model)
        this.setLinkmanName(model)
        this.flag = false
      }
      return true
    },

    projectIdChange(data) {
      data.forEach((res) => {
        res.projectId = this.projectId
      })
    },

    // 被审计单位 被审计单位是一个的时候，直接默认显示；被审计单位是多个的时候，手动选择
    setAuditUnit(model) {
      const params = {
        projectId: this.projectId,
      }
      if (this.projectId) {
        const auditedUnitMember = model.auditedUnitMember
        if (
          auditedUnitMember === null ||
          auditedUnitMember === undefined ||
          auditedUnitMember === ''
        ) {
          axios({
            url: 'api/xcoa-mobile/v1/iamauditevidencesheet/findAuditUnit',
            method: 'post',
            params,
          }).then((res) => {
            this.auditedUnitMember = res.data
          })
        }
      } else {
        this.auditedUnitMember = model.auditedUnitMember
      }
    },
    // 经责类自动读取项目被审计人员
    setLinkmanName(model) {
      const params = {
        projectId: this.projectId,
      }
      const linkmanName = model.linkmanName
      if (linkmanName === null || linkmanName === '' || linkmanName === undefined) {
        if (this.projectId) {
          axios({
            url: 'api/xcoa-mobile/v1/iamauditevidencesheet/findLinkmanName',
            method: 'post',
            params,
          }).then((res) => {
            this.linkmanName = res.data
          })
        }
      } else {
        this.linkmanName = model.linkmanName
      }
    },

    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    // 保存
    saveForm() {
      this.message = '保存成功'
      this.$refs.docform.saveBtnClick()
      return true
    },
    handelPublish() {
      // 发布，保存
      this.message = '发布成功'
      this.$refs.docform.setFieldValue('docStatus', '已结束')
      this.$refs.docform.saveBtnClick()
      return true
    },
    saved() {
      Message.success(this.message).then(() => {
        this.close(true)
      }, 1)
    },
  },
}
</script>

<style module lang="scss">
@import '@/webflow/sd-flow-form.scss';
.first-tr {
  td {
    background: #fff !important;
    border: none !important;
  }
}
.sj-evidence {
  :global(.ant-table-thead) {
    tr:first-child th:nth-child(3) {
      :global(.ant-table-header-column) div {
        :global(.ant-table-column-title)::before {
          position: relative;
          top: 5px;
          padding: 6px;
          font-size: 22px;
          color: #f55d5d;
          content: '*';
        }
      }
    }
  }
}
</style>
