<template>
  <audit-form-top-banner
    :handel-publish="handelPublish"
    :handel-save-form="saveForm"
    :form-data="mode"
    :hiden-or-show="hidenOR0Show"
    @handelPublish="handelPublish"
    @handelSaveForm="saveForm"
  >
    <sd-detail-form
      ref="iamAuditSurveyDetail"
      form-id="iamAuditSurvey"
      page-id="audit/survey/iamAuditSurvey"
      :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
      :class="$style.form"
      @close="close(true)"
      @saved="surveySave"
      @sdFormReady="sdFormReady"
    >
      <template v-slot="{ model }">
        <table>
          <tr :class="$style.displayTr">
            <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="title" :colspan="3" />
          </tr>
          <tr>
            <!-- 被审计单位 -->
            <sd-form-item-td name="auditedUnit" />
            <!-- <sd-form-item-td name="auditedUnitNames">
                <template v-slot:read-and-edit="{ editable }">
                  <sd-list-picker
                    v-model="model.auditedUnitNames"
                    :read-only="!editable"
                    :option-value="'code'"
                    :load-list-data="getPikerData(model.projectId)"
                  />
                </template>
              </sd-form-item-td> -->
            <!-- 文件编号 -->
            <sd-form-item-td name="code">
              <!-- <template v-slot:read-and-edit="{ editable }">
                <a-input
                  v-if="editable"
                  v-model="model.code"
                  placeholder="编号自动生成"
                  read-only
                ></a-input>
                <span v-else>{{ model.code }}</span>
              </template> -->
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 文件描述 -->
            <sd-form-item-td name="description" :colspan="3" />
          </tr>
          <tr>
            <!-- 附件 -->
            <sd-form-item-td name="attachment" :colspan="3" />
          </tr>
          <tr>
            <!-- 备注 -->
            <sd-form-item-td name="docRemarks" :colspan="3">
              <a-textarea v-model="model.docRemarks" :rows="3" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 编制人员 -->
            <sd-form-item-td name="drawUpName" />
            <!-- 编制日期 -->
            <sd-form-item-td name="drawUpTime" />
          </tr>
          <tr>
            <!-- 项目id -->
            <sd-form-item-td name="projectId" :hidden="true" />
            <!-- 编制人员账号 -->
          </tr>
        </table>
      </template>
      <span slot="AppointmentTime" slot-scope="appointmentTime">
        {{ appointmentTime | sdDateFormat('YYYY-MM-DD HH:mm') }}
      </span>
    </sd-detail-form>
  </audit-form-top-banner>
</template>

<script>
import axios from '@/common/services/axios-instance'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import { Message } from 'ant-design-vue'
import auditFormTopBanner from '../../components/audit-form-top-banner'
import components from './_import-components/iam-work-survey-form-import'

export default {
  name: 'IamWorkSurveyForm',
  metaInfo: {
    title: '审计调查',
  },
  components: {
    ...components,
    auditFormTopBanner,
  },
  data() {
    return {
      projectId: this.$route.query.projectId, // 项目ID
      publish: 0, // 为1 时是发布
      mode: null,
      hidenOR0Show: true,
    }
  },
  mounted() {
    setTimeout(() => {
      const record = this.$route.query.record
      if (!record) {
        // this.setCode()
        this.setAuditUnit()
        this.$refs.iamAuditSurveyDetail.setFieldValue('projectId', parseFloat(this.projectId))
      }
    }, 500)
  },
  methods: {
    handelPublish() {
      // 发布，保存
      this.$refs.iamAuditSurveyDetail.setFieldValue('flileStatus', 2)
      this.publish = 1
      this.saveForm()
    },
    // 保存
    saveForm() {
      this.$refs.iamAuditSurveyDetail.validateFields().then(() => {
        this.$refs.iamAuditSurveyDetail.saveBtnClick()
      })
    },
    // 页面加载完成后
    sdFormReady() {
      this.mode = this.$refs.iamAuditSurveyDetail.formData.mode
      if (!this.mode) {
        this.mode = this.$refs.iamAuditSurveyDetail.formData.pageFormData.pageFieldInfos.find(
          (item) => {
            return item.name === 'title'
          }
        ).readonly
          ? 'VIEW'
          : 'EDIT'
      }
      const docStatus = this.$refs.iamAuditSurveyDetail.getFieldValue('flileStatus')
      if (docStatus === '2') {
        this.hidenOR0Show = false
      }
    },
    surveySave(model) {
      if (this.publish === 1) {
        Message.success('发布成功', 1).then((res) => {
          this.close(true)
        })
      } else {
        Message.success('保存成功', 1).then((res) => {
          this.close(true)
        })
      }
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    setCode(model) {
      const params = {
        projectId: this.projectId,
      }
      if (this.projectId) {
        axios({
          url: 'api/xcoa-mobile/v1/iamauditsurvey/findCode',
          method: 'post',
          params,
        }).then((res) => {
          this.$refs.iamAuditSurveyDetail.setFieldValue('code', res.data)
        })
      }
    },
    setAuditUnit(model) {
      const params = {
        projectId: this.projectId,
      }
      if (this.projectId) {
        axios({
          url: 'api/xcoa-mobile/v1/iamauditsurvey/findAuditUnit',
          method: 'post',
          params,
        }).then((res) => {
          this.$refs.iamAuditSurveyDetail.setFieldValue('auditedUnit', res.data)
        })
      }
    },
    getPikerData(projectId) {
      return function() {
        return new Promise((resolve) => {
          axios({
            url: 'api/xcoa-mobile/v1/iamauditsurvey/findAuditedUnit?projectId=' + projectId,
            method: 'get',
          }).then((res) => {
            const dataSource = []
            res.data.forEach((item) => {
              dataSource.push({
                name: item.name,
                code: item.account,
                type: 'Group',
              })
            })
            resolve(dataSource)
          })
        })
      }
    },
  },
}
</script>

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

.ant-card-body {
  padding: 12px;

  :global(.buttons_sd-detail-form_common) {
    :global(.ant-form-item-control-wrapper) {
      padding-left: 24%;
      padding-top: 4%;
    }
  }

  .tr_border {
    box-sizing: border-box;
    border-right: 1px solid #e8e8e8;
    border-left: 1px solid #e8e8e8;
  }

  :global(.title_sd-detail-form_common) {
    font-size: 1.5em;
    text-align: center;
  }
}
.display-tr {
  td {
    border: none !important;
  }
}
</style>
