<template>
  <div>
    <sd-webflow
      ref="flow"
      :removed-tabs="['sdRelatedDoc']"
      @actionBtnClick="actionBtnClick"
      @saveproject="saveproject"
      @afterDispatch="afterDispatch"
    >
      <template v-slot:form="{ model, fields, FlowData }">
        <table>
          <tr>
            <td style="border: none">
              <audit-advanced-group
                :expand="Expand"
                :expand-str="'Expand'"
                :group-label="'基本信息'"
                tablestyle="''"
                @changedClick="changedClick"
              ></audit-advanced-group>
            </td>
          </tr>
        </table>

        <table v-show="Expand">
          <tr v-show="false">
            <!-- ID -->
            <sd-form-item-td name="id" />
            <!-- 随机ID -->
            <!-- <sd-form-item-td name="riskDutiesId" /> -->
          </tr>
          <tr>
            <!-- 发起日期 -->
            <sd-form-item-td name="happenTime" />
            <!-- 事件编号 -->
            <sd-form-item-td name="eventCode">
              <template v-slot:read-and-edit="{ editable }">
                <a-input
                  v-if="editable"
                  v-model="model.eventCode"
                  placeholder="系统自动"
                  read-only
                ></a-input>
                <span v-else>{{ model.eventCode }}</span>
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 事件名称 -->
            <sd-form-item-td name="eventName" />
            <!-- 事件类型 -->
            <sd-form-item-td name="eventType" />
          </tr>
          <tr>
            <!-- 事件性质 -->
            <sd-form-item-td name="eventNature" />
          </tr>
          <tr>
            <!-- 事件描述 -->
            <sd-form-item-td name="eventDesc" :colspan="3">
              <a-textarea v-model="model.eventDesc" :rows="3" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 是否重大风险事件 -->
            <sd-form-item-td name="isMajorEvent" :input-props="{ defaultValue: '02' }" />
            <!-- 损失金额(万元) -->
            <sd-form-item-td name="lossAmount" />
          </tr>
          <tr>
            <!-- 责任单位/部门 -->
            <sd-form-item-td
              name="responsibleDeptOpt"
              :label="'责任单位/部门'"
              @change="changedept"
            />
            <!-- 事件责任人 -->
            <sd-form-item-td name="responsibleUserOpt" />
          </tr>
          <tr v-show="false"><sd-form-item-td name="responsibleDeptName"/></tr>
          <tr>
            <!-- 是否涉诉 -->
            <sd-form-item-td name="isLitigation" :input-props="{ defaultValue: '02' }" />
            <!-- 是否涉外 -->
            <sd-form-item-td name="isOutside" :input-props="{ defaultValue: '02' }" />
          </tr>
          <tr>
            <!-- 处置进展 -->
            <sd-form-item-td name="disposalProgress" />
          </tr>
          <tr>
            <!-- 当前情况描述 -->
            <sd-form-item-td name="situationDesc" :colspan="3">
              <a-textarea v-model="model.situationDesc" :rows="3" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 应对措施 -->
            <sd-form-item-td name="countermeasures" :colspan="3">
              <a-textarea v-model="model.countermeasures" :rows="3" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 填报单位 -->
            <sd-form-item-td name="fillingUnitName" :label="'填报单位'">{{
              model.fillingUnitName ? model.fillingUnitName : fillingUnitName
            }}</sd-form-item-td>
            <!-- 填报人员 -->
            <sd-form-item-td name="creatorName" />
          </tr>
          <tr>
            <!-- 填报日期 -->
            <sd-form-item-td name="creationTime" />
          </tr>
          <tr v-show="false">
            <sd-form-item-td name="fillingUnitId" />
          </tr>
          <tr v-show="initData(model)">
            <!-- 附件 -->
            <sd-form-item-td name="attachment" :colspan="3">
              <template v-slot:read-and-edit="{ editable }">
                <sd-attachment
                  v-model="model.attachment"
                  :group-id="JSON.parse(fields.attachment.value).value"
                  :read-only="!editable"
                ></sd-attachment>
              </template>
            </sd-form-item-td>
          </tr>
        </table>

        <table>
          <tr
            ><td :colspan="4">
              <sd-form-item name="riskLossEvenItemList" :label="null">
                <template v-slot:read-and-edit="{ editable }">
                  <audit-advanced-group
                    :expand="glriskExpand"
                    :expand-str="'glriskExpand'"
                    :group-label="'关联风险事项'"
                    @changedClick="changedClick"
                  >
                    <template>
                      <template>
                        <div :class="$style.wrapper">
                          <div v-show="glriskExpand && editable" :class="$style.btnselect">
                            <a-button type="link" :class="$style.batchselect" @click="fnopensel">
                              <a-icon type="check-circle" :theme="'filled'" />
                              选择
                            </a-button>
                          </div>
                        </div>
                      </template>
                      <xm-child-table
                        ref="infoData"
                        v-model="model.riskLossEvenItemList"
                        :class="$style.deptPlanChild"
                        :addbuttonvisiable="false"
                        :label="null"
                        :read-only="!editable"
                        :fields="
                          [
                            {
                              caption: '序号',
                              name: 'sortNum',
                              dataType: 'number',
                            },
                          ].concat(Array.from(fields.riskLossEvenItemList.attr.dync))
                        "
                        :columns="glriskcolumns"
                      >
                        <template v-slot:riskEventName="{ text }">
                          {{ text }}
                        </template>
                        <template v-slot:riskEventCode="{ text }">
                          {{ text }}
                        </template>
                        <template v-slot:riskCategoryPath="{ text }">
                          {{ text }}
                        </template>
                        <template v-slot:riskVersion="{ text }">
                          {{ text }}
                        </template>
                      </xm-child-table>
                    </template>
                  </audit-advanced-group>
                </template>
              </sd-form-item>
            </td>
          </tr>
        </table>

        <!-- 选择风险组件 -->
        <div v-show="false">
          <ic-measure-picker
            ref="riskeventsel"
            v-model="measureSelect"
            :version-id="versionId"
            :org-id="orgId"
            :org-name="orgName + '风险库'"
            :types="'FK'"
            :single="false"
            :read-only="false"
            @change="fnqd"
          ></ic-measure-picker>
        </div>
      </template>
    </sd-webflow>
  </div>
</template>

<script>
import { Modal, message } from 'ant-design-vue'
import { getUserInfo } from '@/common/store-mixin'
import icMeasurePicker from '@product/iam/components/ic-picker/ic-measure-picker.vue'
// import riskTasksService from '../risk-tasks-service'
import auditAdvancedGroup from '../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import riskService from '../risklib/risk-service'
import riskTasksService from '../task/risk-tasks-service'
import components from './_import-components/risk-loss-event-form-import'

export default {
  name: 'RiskLossEventForm',
  metaInfo: {
    title: '风险损失事件',
  },
  components: {
    ...components,
    auditAdvancedGroup,
    icMeasurePicker,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      Expand: true,
      glriskExpand: true,
      glriskcolumns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '50px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        { dataIndex: 'riskEventId', sdHidden: true },
        {
          dataIndex: 'riskEventName',
          title: '风险事项名称',
          width: '30%',
        },
        { dataIndex: 'riskEventCode', title: '风险事项编号', width: '30%' },
        {
          title: '风险分类',
          dataIndex: 'riskCategoryPath',
        },
        {
          title: '风险版本库编号',
          dataIndex: 'riskVersion',
        },
      ],
      orgId: null,
      versionId: null,
      orgName: '',
      flag: true,
      occurPossibility: [],
      riskImpactDegree: [],
      riskLevel: [],
      taskId: '',
      fxknprops: '',
      fxyxprops: '',
      fillingUnitName: '',
      fillingUnitId: '',
      responseCompletionStateoptions: [],
      measureSelect: [],
      measureSellist: [],
      initDept: [],
    }
  },
  created() {
    this.fillingUnitName = getUserInfo().deptName
  },
  methods: {
    initData(model) {
      if (this.flag) {
        riskService.getUserDept().then((res) => {
          this.fillingUnitName = res.data.name
          this.fillingUnitId = res.data.id
          this.initDept = res.data
          // const userInfo = getUserInfo()
          const userInfo = this.initDept
          let depId = model.fillingUnitId
          if (!depId) {
            depId = userInfo.id
          }
          this.orgName = model.fillingUnitName
          if (!this.orgName) {
            this.orgName = userInfo.name
          }
          // 获取机构ID
          riskTasksService.findIamOrgId(depId, 'riskLibraryVersion').then((res) => {
            if (res.data) {
              this.orgId = res.data.toString()
              this.fillingUnitId = res.data.toString()
              // 获取版本
              riskService.getversion(this.orgId).then((res) => {
                if (res.data.id) {
                  this.versionId = res.data.id.toString()
                }
              })
            }
          })
        })
        this.measureSellist = model.riskLossEvenItemList
        this.flag = false
      }
      return true
    },
    changedept(value) {
      let deptname = ''
      if (value) {
        value.forEach((d) => {
          deptname += ',' + d.name
        })
        if (deptname !== '') deptname = deptname.substring(1, deptname.length)
        this.$refs.flow.setFieldValue('responsibleDeptName', deptname)
      }
    },
    fnopensel() {
      if (this.versionId) {
        this.$refs.riskeventsel.openPicker()
      } else {
        Modal.info({
          content: '请配置机构对应的风险库',
        })
      }
    },
    fnqd(data) {
      const ids = []
      let Events = []
      if (this.$refs.flow.getFieldValue('riskLossEvenItemList')) {
        Events = this.$refs.flow.getFieldValue('riskLossEvenItemList')
      }
      data.forEach((i) => {
        let id = i.id.toString()
        if (id.indexOf('1000') > -1) {
          id = id.substring(4, id.length)
        }
        ids.push(parseInt(id))
      })
      riskService.getFxlist(ids).then((data) => {
        if (data) {
          data.forEach((element) => {
            if (
              Events.findIndex(
                (i) => i.riskEventCode.toString() === element.eventCode.toString()
              ) === -1
            ) {
              const mmc = {}
              mmc.riskEventId = element.id
              mmc.lossEventId = null
              mmc.riskCategoryPath = element.riskCategoryPath
              mmc.riskEventName = element.eventName
              mmc.riskEventCode = element.eventCode
              mmc.riskVersion = element.versionCode
              Events.push(mmc)
            }
          })
          this.$refs.flow.setFieldValue('riskLossEvenItemList', Events)
        }
      })
    },
    actionBtnClick(evt, { button, FlowData }) {
      if (this.$refs.flow.getFieldValue('fillingUnitName') === undefined) {
        this.$refs.flow.setFieldValue('fillingUnitName', this.fillingUnitName)
      }
      if (this.$refs.flow.getFieldValue('fillingUnitId') === undefined) {
        this.$refs.flow.setFieldValue('fillingUnitId', this.fillingUnitId.toString())
      }

      this.$refs.flow.setFieldValue('title', this.$refs.flow.getFieldValue('eventName'))

      if (button.fakeId === 'save') {
      }
    },
    // 保存操作，记录标识位
    saveproject() {
      this.isSave = true
    },
    /**
     * 提交后事件
     */
    afterDispatch() {
      // 提交也认为是保存
      this.isSave = true
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.btnselect {
  position: relative;
  top: 4px;
  right: 80px;
  float: right;
  .batchselect {
    z-index: 100;
    margin-left: 10px;
  }
}
</style>
