<template>
  <div :class="$style.rectifyForm">
    <audit-form-top-banner :handel-save-form="edit ? saveForm : null" @handelSaveForm="saveForm">
      <sd-detail-form
        ref="docform"
        page-id="audit/work/rectify/iamRectifyCancel"
        :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
        :class="$style.form"
        @close="close(true)"
        @saved="saved"
      >
        <template v-slot="{ model, fields }">
          <table>
            <tr>
              <td style="padding-left:5px;border:none">
                <audit-advanced-group
                  :expand="rectify"
                  :expand-str="'rectify'"
                  :group-label="'审计发现_整改确认'"
                  tablestyle="''"
                  @changedClick="changedClick"
                ></audit-advanced-group>
              </td>
            </tr>
          </table>

          <table v-if="rectify && edit">
            <colgroup>
              <col style="width: 20%;"/>
              <col style="width: 30%;"/>
              <col style="width: 20%;"/>
              <col style="width: 30%;"
            /></colgroup>
            <tr>
              <!-- 确认状态 -->
              <sd-form-item-td :hidden="true" name="id" />
              <sd-form-item-td :hidden="true" name="carryId" />
              <sd-form-item-td name="passStatus" :colspan="3" />
            </tr>
            <tr>
              <!-- 确认意见 -->
              <sd-form-item-td name="confirmOpinion" :colspan="3">
                <a-textarea v-model="model.confirmOpinion" :rows="3" />
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 确认人名称 -->
              <sd-form-item-td name="confirmName">
                <a-input v-model="confirmName" :disabled="true" allow-clear />
              </sd-form-item-td>
              <!-- 确认时间 -->
              <sd-form-item-td name="confirmTime" :label="'确认时间'">
                <a-date-picker
                  v-model="confirmTime"
                  :placeholder="''"
                  format="YYYY-MM-DD"
                  style="width:100%;"
                />
              </sd-form-item-td>
            </tr>
          </table>

          <table v-show="rectify">
            <colgroup>
              <col style="width: 20%;"/>
              <col style="width: 30%;"/>
              <col style="width: 20%;"/>
              <col style="width: 30%;"
            /></colgroup>
            <tr>
              <td colspan="4">
                <a @click="fnopenhistoryrecify">历史确认记录</a>
              </td>
            </tr>
            <tr>
              <td class="ant-form-item-label ant-form-item-label"
                ><label title="确认状态"> 确认状态 </label></td
              >
              <td colspan="3">{{
                fngetlabel(
                  fields.passStatus,
                  fngetnr(model.iamRectifyCancelLogEntities, 'passStatus')
                )
              }}</td>
            </tr>
            <tr>
              <td class="ant-form-item-label ant-form-item-label"
                ><label title="确认意见"> 确认意见 </label></td
              >
              <td colspan="3">{{
                fngetnr(model.iamRectifyCancelLogEntities, 'confirmOpinion')
              }}</td>
            </tr>
            <tr>
              <td class="ant-form-item-label ant-form-item-label"
                ><label title="确认人姓名"> 确认人姓名 </label></td
              >
              <td>{{ fngetnr(model.iamRectifyCancelLogEntities, 'confirmName') }}</td>
              <td class="ant-form-item-label ant-form-item-label"
                ><label title="确认时间"> 确认时间 </label></td
              >
              <td>{{
                fngetnr(model.iamRectifyCancelLogEntities, 'confirmTime')
                  | sdDateFormat('YYYY-MM-DD')
              }}</td>
            </tr>
          </table>

          <table>
            <tr>
              <td style="padding-left:5px;border:none">
                <audit-advanced-group
                  :expand="basic"
                  :expand-str="'basic'"
                  :group-label="'审计发现_基本信息'"
                  tablestyle="''"
                  @changedClick="changedClick"
                ></audit-advanced-group>
              </td>
            </tr>
          </table>

          <table v-show="basic">
            <colgroup>
              <col style="width: 20%;"/>
              <col style="width: 30%;"/>
              <col style="width: 20%;"/>
              <col style="width: 30%;"
            /></colgroup>
            <tr>
              <!-- 审计发现标题 -->
              <sd-form-item-td name="findTitle"></sd-form-item-td>
              <!-- 审计发现编号 -->
              <sd-form-item-td name="findCode"></sd-form-item-td>
            </tr>
            <tr>
              <!-- 审计发现类别 -->
              <sd-form-item-td name="findType" :colspan="3"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 审计发现描述 -->
              <sd-form-item-td name="findDesc" :colspan="3"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 审计建议描述 -->
              <sd-form-item-td name="suggestion"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 问题责任部门 -->
              <sd-form-item-td name="responsibleDept"> </sd-form-item-td>
              <!-- 所属业务域 -->
              <sd-form-item-td name="businessProcess"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 是否问责 -->
              <sd-form-item-td name="whetherOrderDuty"> </sd-form-item-td>
              <!-- 问责要求 -->
              <sd-form-item-td name="orderDutyAsk"> </sd-form-item-td>
            </tr>
          </table>

          <table>
            <tr>
              <td style="padding-left:5px;border:none">
                <audit-advanced-group
                  :expand="project"
                  :expand-str="'project'"
                  :group-label="'审计发现_整改计划信息'"
                  tablestyle="''"
                  @changedClick="changedClick"
                ></audit-advanced-group>
              </td>
            </tr>
          </table>

          <table v-show="project">
            <colgroup>
              <col style="width: 20%;"/>
              <col style="width: 30%;"/>
              <col style="width: 20%;"/>
              <col style="width: 30%;"
            /></colgroup>
            <tr>
              <!-- 整改责任部门 -->
              <sd-form-item-td name="responsibleDeptName"> </sd-form-item-td>
              <!-- 直接责任人 -->
              <sd-form-item-td name="directResponsible"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 领导责任人 -->
              <sd-form-item-td name="leaderResponsible"> </sd-form-item-td>
              <!-- 共同责任部门 -->
              <sd-form-item-td name="commonDutyDepart"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 拟整改完成日期 -->
              <sd-form-item-td name="finishDate" :colspan="3"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 拟整改措施 -->
              <sd-form-item-td name="measure" :colspan="3"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 说明 -->
              <sd-form-item-td name="remark" :colspan="3"> </sd-form-item-td>
            </tr>
          </table>

          <table>
            <tr>
              <td style="padding-left:5px;border:none">
                <audit-advanced-group
                  :expand="carry"
                  :expand-str="'carry'"
                  :group-label="'审计发现_整改落实信息'"
                  tablestyle="''"
                  @changedClick="changedClick"
                ></audit-advanced-group>
              </td>
            </tr>
          </table>

          <table v-show="carry">
            <colgroup>
              <col style="width: 20%;"/>
              <col style="width: 30%;"/>
              <col style="width: 20%;"/>
              <col style="width: 30%;"
            /></colgroup>
            <tr>
              <!-- 整改状态 -->
              <sd-form-item-td name="rectStatus" :colspan="model.rectStatus === '03' ? 1 : 3">
              </sd-form-item-td>
              <!-- 整改完成日期 -->
              <sd-form-item-td v-if="model.rectStatus === '03'" name="rectFinishDate">
              </sd-form-item-td>
            </tr>
            <tr v-show="model.rectStatus === '01'">
              <!-- 是否超期未整改 -->
              <sd-form-item-td name="isTrue" :colspan="3">{{ model.isTrue }}</sd-form-item-td>
            </tr>
            <tr v-if="model.isTrue === '1' || model.isTrue === 1">
              <!-- 超期原因 -->
              <sd-form-item-td name="overdueReason" :colspan="3">{{
                model.overdueReason
              }}</sd-form-item-td>
            </tr>
            <tr v-if="model.rectStatus !== '04' && model.rectStatus !== '03'">
              <!-- 预计整改完成日期 -->
              <sd-form-item-td name="expectedCompleteDate" :colspan="3">{{
                model.expectedCompleteDate
              }}</sd-form-item-td>
            </tr>
            <tr>
              <!-- 整改情况说明 -->
              <sd-form-item-td name="description" :colspan="3"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 整改问题金额分类 -->
              <sd-form-item-td name="rectMoneyType"> </sd-form-item-td>
              <!-- 整改问题金额 -->
              <sd-form-item-td name="rectMoneySum"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 成果利用 -->
              <sd-form-item-td name="resultUse"> </sd-form-item-td>
              <!-- 成果利用数量 -->
              <sd-form-item-td name="resultUseSum"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 处分信息 -->
              <sd-form-item-td name="punishInfo"> </sd-form-item-td>
              <!-- 处分人数 -->
              <sd-form-item-td name="punishSum"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 向司法机关移送或报案线索 -->
              <sd-form-item-td name="transferToJustice"> </sd-form-item-td>
              <!-- 涉案人员 -->
              <sd-form-item-td name="personInvolved"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 附件 -->
              <sd-form-item-td name="attachment" :colspan="3">
                <template v-slot:read-and-edit="{ editable }">
                  <div v-if="groupId">
                    <sd-attachment :group-id="groupId" :read-only="!editable"></sd-attachment>
                  </div>
                  <div v-else></div>
                </template>
              </sd-form-item-td>
            </tr>
          </table>

          <table>
            <tr>
              <td style="padding-left:5px;border:none">
                <audit-advanced-group
                  :expand="change"
                  :expand-str="'change'"
                  :group-label="'审计发现_变更信息'"
                  tablestyle="''"
                  @changedClick="changedClick"
                ></audit-advanced-group>
              </td>
            </tr>
          </table>

          <table
            v-for="(bg, index) in fndeal(model.iamDetailFormModifyLogEntities)"
            v-show="change"
            :key="index"
            :class="$style.tablexm"
          >
            <colgroup>
              <col style="width: 20%;" />
              <col style="width: 30%;" />
              <col style="width: 20%;" />
              <col style="width: 30%;" />
            </colgroup>
            <tr>
              <td style="width:160px;text-align:right;vertical-align:top">{{ bg.creatorName }}</td
              ><td colspan="3"
                ><div
                  style="
    width: 18px;margin-left:2px;
    font-size: 8px;
    color: #fff;
    text-align:center;
    background-color: lightgreen;
    border-radius: 100%;
"
                  >√</div
                ><div style="padding-left:10px;margin-left:9px;border-left:1px solid gray"
                  >{{ bg.creationTime | sdDateFormat('YYYY-MM-DD HH:mm:ss') }}
                  <table
                    ><tr v-for="(x, index) in bg.xx" :key="index">
                      <td>[{{ x.fieldDesc }}]</td
                      ><td
                        >{{ x.oldValue !== '' ? '"' : ''
                        }}{{
                          x.oldValue.indexOf('[') !== -1 ||
                          x.oldValue.indexOf('{') !== -1 ||
                          x.newValue.indexOf('[') !== -1 ||
                          x.newValue.indexOf('{') !== -1
                            ? ''
                            : x.oldValue === 'null'
                            ? ''
                            : x.oldValue
                        }}{{ x.oldValue !== '' ? '" 改为 ' : ''
                        }}<span v-if="x.oldValue === ''" style="color:green">
                          {{
                            x.newValue.indexOf('[') !== -1 || x.newValue.indexOf('{') !== -1
                              ? '修改动态表格数据'
                              : x.newValue
                          }}</span
                        >
                        <span v-else>{{
                          x.newValue.indexOf('[') !== -1 || x.newValue.indexOf('{') !== -1
                            ? '修改动态表格数据'
                            : '"' + x.newValue + '"'
                        }}</span>
                      </td></tr
                    ></table
                  >
                </div></td
              >
            </tr>
          </table>
        </template>
      </sd-detail-form>
    </audit-form-top-banner>
  </div>
</template>

<script>
import moment from 'moment'
import { Message } from 'ant-design-vue'
import axios from '@/common/services/axios-instance'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import { getUserInfo } from '@/common/store-mixin'
import auditAdvancedGroup from '../../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../../components/audit-advanced-group-mixins'
import auditFormTopBanner from '../../../components/audit-form-top-banner'
import components from './_import-components/iam-audit-rectify-form-import'

export default {
  name: 'IamAuditRectifyForm',
  metaInfo: {
    title: '整改销号',
  },
  components: {
    ...components,
    auditFormTopBanner,
    auditAdvancedGroup,
    // SdDetailForm,
    // sd-form-item-td,
    // SdQuillEditor,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      // 审计发现整改确认
      rectify: true,
      // 审计发现_基本信息
      basic: true,
      // 审计发现_整改计划信息
      project: true,
      // 审计发现_整改落实信息
      carry: true,
      // 审计发现_变更信息
      change: true,
      edit: false,
      confirmTime: null,
      confirmName: '',
      groupId: null,
    }
  },
  created() {
    if (this.$route.query.edit === 'true') {
      this.edit = true
      this.confirmName = getUserInfo().name
      this.confirmTime = moment(new Date())
    } else {
      this.edit = false
    }
    // 获取整改反馈的附件
    const _this = this
    const ini = setInterval(() => {
      if (_this.$refs.docform) {
        const id = _this.$refs.docform.getFieldValue('carryId')
        clearInterval(ini)
        axios({
          method: 'get',
          url: `api/framework/v1/page/wp/audit/rectFeedback/iamWorkRectCarryOut?id=${id}`,
        }).then((res) => {
          let groupId = res.data.pageFormData.pageFieldInfos.find((item) => {
            return item.name === 'attachment'
          }).value
          groupId = JSON.parse(groupId).value
          _this.groupId = groupId
        })
      }
    }, 100)
  },
  methods: {
    fngetlabel(obj, val) {
      if (obj.attr.selectListItem) {
        const o = obj.attr.selectListItem.find((item) => item.value === val)
        if (o) {
          return o.label
        } else {
          return val
        }
      } else {
        return val
      }
    },
    fndeal(obj) {
      const newobj = []
      if (obj !== undefined) {
        obj.forEach((item) => {
          item.xt = item.creatorName + '_' + item.creationTime
          const i = newobj.find((n) => n.xt === item.xt)
          if (i !== undefined) {
            i.xx.push({
              fieldDesc: item.fieldDesc,
              fieldName: item.fieldName,
              newValue: item.newValue,
              oldValue: item.oldValue,
            })
          } else {
            item.xx = [
              {
                fieldDesc: item.fieldDesc,
                fieldName: item.fieldName,
                newValue: item.newValue,
                oldValue: item.oldValue,
              },
            ]
            newobj.push(item)
          }
        })
      }
      return newobj
    },

    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    saved() {
      axios({
        url:
          'api/xcoa-mobile/v1/iam-rectify-cancel/changeDaft?recordId=' + this.$route.query.record,
        method: 'get',
      })
      Message.success('保存成功!', 1).then((res) => {
        this.close(true)
      })
    },
    saveForm() {
      this.$refs.docform.setFieldValue('confirmTime', this.confirmTime)
      this.$refs.docform.setFieldValue('confirmName', this.confirmName)
      this.$refs.docform.saveBtnClick()
    },
    fnopenhistoryrecify() {
      return false
    },
    fngetnr(obj, item) {
      if (obj === undefined) {
        return ''
      } else {
        if (obj.length > 0) {
          return obj[obj.length - 1][item]
        } else {
          return ''
        }
      }
    },
  },
}
</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;
  }
}
.tablexm tr td {
  border: none !important;
}
.rectifyForm {
  width: 100%;
  height: 100%;
  :global(span.toptitle.toptitle_audit-form-top-banner_product::after) {
    content: '整改确认';
  }
  :global(span.toptitle.toptitle_audit-form-top-banner_product span) {
    display: none;
  }
}
</style>
