<template>
  <audit-form-top-banner :handel-save-form="saveForm" :form-data="mode" @handelSaveForm="saveForm">
    <sd-detail-form
      ref="iamExternalBaseDetail"
      form-id="iamExternalBase"
      page-id="audit/auditsource/extrnal/iamExternalBase"
      :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
      :class="$style.form"
      @close="close(true)"
      @saved="WPJGKSave"
      @sdFormReady="sdFormReady"
    >
      <template v-slot="{ model, fields }">
        <table>
          <tr>
            <td>
              <audit-advanced-group
                :expand="basicExpand"
                :expand-str="'basicExpand'"
                :group-label="'基本信息'"
                tablestyle="''"
                @changedClick="changedClick"
              ></audit-advanced-group>
            </td>
          </tr>
        </table>

        <table v-show="basicExpand">
          <tr>
            <!-- 统一社会信用代码 -->
            <sd-form-item-td name="code" />
            <!-- 企业名称 -->
            <sd-form-item-td name="name" />
          </tr>
          <tr>
            <!-- 类型 -->
            <sd-form-item-td name="category" />
            <!-- 住所 -->
            <sd-form-item-td name="address" />
          </tr>
          <tr>
            <!-- 法定代表人 -->
            <sd-form-item-td name="juridicalPerson" />
            <!-- 注册资本 -->
            <sd-form-item-td name="capital" />
          </tr>
          <tr>
            <!-- 成立日期 -->
            <sd-form-item-td name="foundTime" :colspan="3" :input-props="{ style: 'width:100%' }" />
          </tr>
          <tr>
            <!-- 简介 -->
            <sd-form-item-td name="introducation" :colspan="3">
              <a-textarea v-model="model.introducation" :rows="3" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 企业状态 -->
            <sd-form-item-td name="status" />
            <!-- 是否黑名单 -->
            <!-- <sd-form-item-td name="isBlackList" /> -->
          </tr>
          <tr>
            <!-- 是否入库 -->
            <sd-form-item-td name="isWarehousing" />
            <!-- 入库期限 -->
            <sd-form-item-td name="warehousingTerm" />
          </tr>
          <tr>
            <!-- 编制人员 -->
            <sd-form-item-td name="authName" />
            <!-- 编制日期 -->
            <sd-form-item-td name="authTime" />
          </tr>
          <tr>
            <!-- 编制单位 -->
            <sd-form-item-td name="authUnit" :colspan="3" />
          </tr>
          <tr>
            <!-- 附件 -->
            <sd-form-item-td name="attachment" :colspan="3" />
          </tr>
          <tr>
            <!-- id -->
            <sd-form-item-td name="id" :hidden="true" />
          </tr>
        </table>

        <table>
          <tr
            ><td :colspan="4">
              <sd-form-item name="iamExtrnalContactEntitys" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="contactExpand"
                    :expand-str="'contactExpand'"
                    :group-label="'联系人信息'"
                    @changedClick="changedClick"
                  >
                    <template>
                      <xm-child-table
                        v-if="contactExpand"
                        ref="tabaData"
                        v-model="model.iamExtrnalContactEntitys"
                        :class="$style.deptPlanChild"
                        label="联系人信息"
                        :read-only="!editable"
                        :fields="Array.from(fields.iamExtrnalContactEntitys.attr.dync)"
                        :columns="contactColumns"
                      >
                        <!-- input-form 表示这部分是子表组件的form插槽 -->
                        <template v-slot:input-form="{ model }">
                          <!-- 外聘机构id -->
                          <sd-form-item-td name="extrnalId" />
                          <!-- 序号 -->
                          <sd-form-item-td name="contactOrder" />
                          <!-- 姓名 -->
                          <sd-form-item-td name="contactName" />
                          <!-- 电话 -->
                          <sd-form-item-td name="phone" />
                          <!-- 邮箱 -->
                          <sd-form-item-td name="email" />
                          <!-- 职位 -->
                          <sd-form-item-td name="post" />
                          <!-- 备注 -->
                          <sd-form-item-td name="remark" />
                        </template>
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template> </sd-form-item
            ></td>
          </tr>
        </table>

        <table>
          <tr>
            <td>
              <audit-advanced-group
                :expand="qualificationExpand"
                :expand-str="'qualificationExpand'"
                :group-label="'资质信息'"
                tablestyle="''"
                @changedClick="changedClick"
              ></audit-advanced-group>
            </td>
          </tr>
        </table>

        <table v-show="qualificationExpand">
          <tr>
            <!-- 注册会计师人数 -->
            <sd-form-item-td name="accountingNumber" :input-props="{ style: 'width:100%' }" />
            <!-- 注册造价工程师人数 -->
            <sd-form-item-td name="engineerNumber" :input-props="{ style: 'width:100%' }" />
          </tr>
          <tr
            ><td :colspan="4">
              <sd-form-item name="iamExtrnalQualityEntitys" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="qualityExpand"
                    :expand-str="'qualityExpand'"
                    :group-label="'资质信息列表'"
                    @changedClick="changedClick"
                  >
                    <template>
                      <xm-child-table
                        v-if="qualityExpand"
                        ref="tabaData"
                        v-model="model.iamExtrnalQualityEntitys"
                        :class="$style.deptPlanChild"
                        label="资质信息"
                        :read-only="!editable"
                        :fields="Array.from(fields.iamExtrnalQualityEntitys.attr.dync)"
                        :columns="qualityColumns"
                      >
                        <!-- input-form 表示这部分是子表组件的form插槽 -->
                        <template v-slot:input-form="{ model }">
                          <!-- 外聘机构id -->
                          <sd-form-item-td name="extrnalId" />
                          <!-- 序号 -->
                          <sd-form-item-td name="qualityOrder" />
                          <!-- 资质名称 -->
                          <sd-form-item-td name="qualityName" />
                          <!-- 有效期限 -->
                          <sd-form-item-td name="effectiveDate" />
                          <!-- 资质证书 -->
                          <sd-form-item-td name="attachment" />
                        </template>
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template> </sd-form-item
            ></td>
          </tr>
        </table>

        <table>
          <tr
            ><td :colspan="4">
              <sd-form-item name="iamExtrnalProjectEntitys" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="projectExpand"
                    :expand-str="'projectExpand'"
                    :group-label="'项目经验'"
                    @changedClick="changedClick"
                  >
                    <template>
                      <sd-child-table
                        ref="tabaData"
                        v-model="model.iamExtrnalProjectEntitys"
                        :class="$style.deptPlanChild"
                        label="项目信息"
                        :read-only="!editable"
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'number',
                            },
                          ].concat(Array.from(fields.iamExtrnalProjectEntitys.attr.dync))
                        "
                        :columns="projectChildColumns"
                        :flagpage="true"
                        @change="changeProjectStartEndTime"
                      >
                        <!-- form 表示这部分是子表组件的form插槽 -->
                        <template v-slot:form="{ model }">
                          <table>
                            <tr>
                              <!-- 项目名称 -->
                              <sd-form-item-td name="projectName" />
                              <!-- 项目编号 -->
                              <sd-form-item-td name="projectNo" />
                            </tr>
                            <tr>
                              <!-- 审计机构 -->
                              <sd-form-item-td name="auditOrg" />
                              <!-- 被审计单位 -->
                              <sd-form-item-td name="auditedUnit" />
                            </tr>
                            <tr>
                              <!-- 项目组长 -->
                              <sd-form-item-td name="projectHeadman" />
                            </tr>
                            <tr>
                              <!-- 项目开始日期 -->
                              <!-- 项目结束日期 -->
                              <sd-form-item-td
                                label="项目起止日期"
                                :colspan="3"
                                :name="['projectStartTime', 'projectEndTime']"
                              />
                            </tr>
                            <tr>
                              <!-- 审计期间开始时间 -->
                              <!-- 审计期间结束时间 -->
                              <sd-form-item-td
                                label="审计期间"
                                :colspan="3"
                                :name="['auditStartTime', 'auditEndTime']"
                              />
                            </tr>
                            <tr>
                              <!-- 审计情况描述 -->
                              <sd-form-item-td name="auditCondition" :colspan="3">
                                <a-textarea v-model="model.auditCondition" :rows="3" />
                              </sd-form-item-td>
                            </tr>
                            <tr>
                              <!-- 维护人员 -->
                              <sd-form-item-td name="creatorName" />
                              <!-- 维护日期 -->
                              <sd-form-item-td name="creationTime" />
                            </tr>
                            <tr>
                              <!-- 附件 -->
                              <sd-form-item-td name="attachment" :colspan="3" />
                            </tr>
                          </table>
                        </template>
                      </sd-child-table>
                    </template>
                  </audit-advanced-group>
                </template> </sd-form-item
            ></td>
          </tr>
          <tr
            ><td :colspan="4">
              <sd-form-item name="iamExtrnalCheckEntitys" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="CheckExpand"
                    :expand-str="'CheckExpand'"
                    :group-label="'考评记录'"
                    @changedClick="changedClick"
                  >
                    <template>
                      <sd-child-table
                        ref="tabaData"
                        v-model="model.iamExtrnalCheckEntitys"
                        :class="$style.deptPlanChild"
                        label="考评记录"
                        :read-only="!editable"
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'number',
                            },
                          ].concat(Array.from(fields.iamExtrnalCheckEntitys.attr.dync))
                        "
                        :columns="checkChildColumns"
                        :flagpage="true"
                      >
                        <!-- form 表示这部分是子表组件的form插槽 -->
                        <template v-slot:form="{ model }">
                          <table>
                            <tr>
                              <td style="width:15%"></td>
                            </tr>
                            <tr>
                              <!-- 考评名称 -->
                              <sd-form-item-td name="checkName" :input-td-attrs="{ colspan: 3 }" />
                            </tr>
                            <tr>
                              <!-- 考评结果 -->
                              <sd-form-item-td name="checkResult" :colspan="3">
                                <a-textarea
                                  v-model="model.checkResult"
                                  :autosize="{ minRows: 3, maxRows: 3 }"
                                ></a-textarea>
                              </sd-form-item-td>
                            </tr>
                            <tr>
                              <!-- 附件 -->
                              <sd-form-item-td name="attachment" :colspan="3" />
                            </tr>
                            <tr>
                              <!-- 编制人员 -->
                              <sd-form-item-td name="creatorName" />
                              <!-- 编制时间 -->
                              <sd-form-item-td name="creationTime" />
                            </tr>
                          </table>
                        </template>
                      </sd-child-table>
                    </template>
                  </audit-advanced-group>
                </template> </sd-form-item
            ></td>
          </tr>
        </table>

        <table>
          <tr
            ><td :colspan="4">
              <sd-form-item name="iamExternalEvaluationEntitys" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="evaluationExpand"
                    :expand-str="'evaluationExpand'"
                    :group-label="'人员考评信息'"
                    @changedClick="changedClick"
                  >
                    <template>
                      <xm-child-table
                        v-if="evaluationExpand"
                        ref="tabaData"
                        v-model="model.iamExternalEvaluationEntitys"
                        :class="$style.deptPlanChild"
                        label="人员考评信息"
                        :read-only="!editable"
                        :fields="Array.from(fields.iamExternalEvaluationEntitys.attr.dync)"
                        :columns="evaluationChildColumns"
                      >
                        <!-- input-form 表示这部分是子表组件的form插槽 -->
                        <template v-slot:input-form="{ model }">
                          <!-- 外聘机构id -->
                          <sd-form-item-td name="externalId" />
                          <!-- 审计年度 -->
                          <!-- <sd-form-item-td name="auditYear"/> -->
                          <sd-form-item-td label="审计年度" prop="auditYear">
                            <a-date-picker
                              v-model="model.iamExternalEvaluationEntitys.auditYear"
                              mode="year"
                              picker="YYYY"
                              format="yyyy"
                              :allow-clear="false"
                              placeholder="选择年度"
                              :input-read-only="true"
                              :value="year"
                              :open="endOpen"
                              @panelChange="yearChange"
                              @openChange="handleEndOpenChange"
                            />
                          </sd-form-item-td>

                          <!-- 序号 -->
                          <sd-form-item-td name="evaluationOrder" />
                          <!-- 项目名称 -->
                          <sd-form-item-td name="projectName" />
                          <!-- 项目编号 -->
                          <sd-form-item-td name="projectNo" />
                          <!-- 考核对象 -->
                          <sd-form-item-td name="evaluationObject" />
                          <!-- 审计机构 -->
                          <sd-form-item-td name="auditOrg" />
                          <!-- 项目组长 -->
                          <sd-form-item-td name="projectHeadman" />
                          <!-- 本项目担任角色 -->
                          <sd-form-item-td name="projectRole" />
                          <!-- 人员考评结果 -->
                          <sd-form-item-td name="evaluationResult" />
                          <!-- 人员评分 -->
                          <sd-form-item-td name="evaluationGrade" />
                        </template>
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template> </sd-form-item
            ></td>
          </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 moment from 'moment'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import { Message } from 'ant-design-vue'
import axios from '@/common/services/axios-instance'
import sdDateFormat from '@/common/filters/sd-date-format'
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/iam-audit-extrnal-form-import'

export default {
  name: 'IamAuditExtrnalForm',
  metaInfo: {
    title: '外聘机构',
  },
  components: {
    ...components,
    auditFormTopBanner,
    auditAdvancedGroup,
  },
  mixins: [auditAdvancedGroupMixins],
  props: {
    defaultTime: {
      type: [Date, String],
      default: null,
    },
  },
  data() {
    return {
      endOpen: false,
      year: null,
      basicExpand: true,
      mode: null,
      contactExpand: true,
      qualificationExpand: true,
      qualityExpand: true,
      projectExpand: true,
      CheckExpand: true,
      evaluationExpand: true,
      contactColumns: [
        {
          title: '序号',
          dataIndex: 'contactOrder',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'email', width: '20%' },
        { dataIndex: 'remark', width: '20%' },
      ],
      qualityColumns: [
        {
          title: '序号',
          dataIndex: 'qualityOrder',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'effectiveDate', width: '200px' },
      ],
      projectChildColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'projectName', sdHidden: false },
        { dataIndex: 'projectNo', sdHidden: false, width: '150px' },
        { dataIndex: 'auditOrg', sdHidden: false, width: '180px' },
        { dataIndex: 'projectHeadman', sdHidden: false, width: '120px' },
        {
          dataIndex: 'projectStartEndTime',
          sdHidden: false,
          customRender: (text, record, index) => {
            var startTime = sdDateFormat(record.projectStartTime, 'yyyy-MM-DD')
            var endTime = sdDateFormat(record.projectEndTime, 'yyyy-MM-DD')
            return startTime + '~' + endTime
          },
        },
        { dataIndex: 'projectStartTime', sdHidden: true },
        { dataIndex: 'projectEndTime', sdHidden: true },
        { dataIndex: 'auditStartTime', sdHidden: true },
        { dataIndex: 'auditEndTime', sdHidden: true, title: '审计结束日期', width: '150px' },
        { dataIndex: 'auditCondition', sdHidden: false, width: '15%' },
        { dataIndex: 'creatorName', sdHidden: true },
        { dataIndex: 'creationTime', sdHidden: true },
        { dataIndex: 'attachment', sdHidden: true },
        { dataIndex: 'auditedUnit', sdHidden: true },
      ],
      checkChildColumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'creatorName', sdHidden: true },
        { dataIndex: 'creationTime', sdHidden: true },
      ],
      evaluationChildColumns: [
        {
          title: '序号',
          dataIndex: 'evaluationOrder',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },

        { dataIndex: 'externalId', sdHidden: true },
        { dataIndex: 'auditYear', sdHidden: false },
        { dataIndex: 'evaluationOrder', sdHidden: false },
        { dataIndex: 'projectName', sdHidden: false },
        { dataIndex: 'projectNo', sdHidden: false },
        { dataIndex: 'evaluationObject', sdHidden: false },
        { dataIndex: 'auditOrg', sdHidden: false },
        { dataIndex: 'projectHeadman', sdHidden: false },
        { dataIndex: 'projectRole', sdHidden: false },
        { dataIndex: 'evaluationResult', sdHidden: false },
        { dataIndex: 'evaluationGrade', sdHidden: false },
        { dataIndex: 'creatorName', sdHidden: true },
        { dataIndex: 'creationTime', sdHidden: true },
      ],
    }
  },
  methods: {
    moment,
    // 页面加载完成后
    handleEndOpenChange(open) {
      this.endOpen = open
    },
    sdFormReady() {
      this.mode = this.$refs.iamExternalBaseDetail.formData.mode
    },
    yearChange(value) {
      this.year = value
      this.advSearchForm.planYear = value.format('YYYY')
      if (document.getElementsByClassName('ant-calendar-picker-container').length > 0) {
        document.getElementsByClassName('ant-calendar-picker-container')[0].style.display = 'none'
      }
    },
    // 项目起止日期赋值
    changeProjectStartEndTime(data) {
      data.forEach((item, index) => {
        item.sortNum = index + 1
        var startTime = sdDateFormat(item.projectStartTime, 'yyyy-MM-DD')
        var endTime = sdDateFormat(item.projectEndTime, 'yyyy-MM-DD')
        item.projectStartEndTime = startTime + '~' + endTime
      })
    },
    // 保存
    saveForm() {
      var id = this.$refs.iamExternalBaseDetail.getFieldValue('id')
      var code = this.$refs.iamExternalBaseDetail.getFieldValue('code')
      if (code && id == null) {
        axios({
          url: 'api/xcoa-mobile/v1/iamexternalbase/validate/code?code=' + code,
          method: 'get',
        }).then((res) => {
          if (res.status === 200) {
            if (res.data) {
              this.$refs.iamExternalBaseDetail.saveBtnClick()
            } else {
              Message.error('信用代码已存在', 1)
            }
          }
        })
      } else {
        this.$refs.iamExternalBaseDetail.saveBtnClick()
      }
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    WPJGKSave(model) {
      Message.success('保存成功', 1).then((res) => {
        this.close(true)
      })
    },
  },
}
</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-top: 4%;
      padding-left: 24%;
    }
  }

  .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;
  }
}
</style>
