<template>
  <span>
    <audit-form-top-banner
      :handel-save-form="saveForm"
      :form-data="mode"
      @handelSaveForm="saveForm"
      @handelPublish="handelPublish"
    >
      <div class="ant-card ant-card-small">
        <div :class="$style.antCardBody">
          <sd-detail-form
            ref="docform"
            page-id="audit/auditsource/problem/iamProblem"
            form-id="iamProblem"
            :record-id="this.$route.query.id ? parseInt(this.$route.query.id) : null"
            :class="[$style.form]"
            @close="close(true)"
            @saved="saved"
            @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" :class="$style.table">
                <colgroup>
                  <col :class="$style.label" />
                  <col :class="$style.input" />
                  <col :class="$style.label" />
                  <col :class="$style.input" />
                </colgroup>
                <tr>
                  <sd-form-item-td name="docTitle" />
                  <sd-form-item-td name="problemNumber"> </sd-form-item-td>
                </tr>
                <tr>
                  <sd-form-item-td name="problemDesc" :colspan="3">
                    <a-textarea
                      v-model="model.problemDesc"
                      placeholder="请输入"
                      :auto-size="{ minRows: 3, maxRows: 5 }"
                    />
                  </sd-form-item-td>
                </tr>

                <tr>
                  <!-- 作者 -->
                  <sd-form-item-td name="creatorName" />
                  <!-- 所属部门 -->
                  <sd-form-item-td name="creationTime" />
                  <sd-form-item-td v-if="initParam('parentId')" :hidden="true" name="parentId" />
                </tr> </table

              ><table>
                <tr
                  ><td :colspan="4">
                    <sd-form-item name="iamAuditLawsRelEntitys" :label="null">
                      <template v-slot:read-and-edit="{ editable }">
                        <audit-advanced-group
                          :expand="CheckExpand"
                          :expand-str="'CheckExpand'"
                          :group-label="'法律制度依据'"
                          @changedClick="changedClick"
                        >
                          <template>
                            <template>
                              <div :class="$style.wrapper">
                                <div v-show="CheckExpand && editable" :class="$style.btnselect">
                                  <a-button
                                    v-if="!isHidden"
                                    type="link"
                                    :class="$style.batchselect"
                                    @click="lawsRelSelect"
                                  >
                                    <a-icon type="check-circle" :theme="'filled'" />
                                    选择
                                  </a-button>
                                </div>
                              </div>
                            </template>
                            <xm-child-table
                              ref="tabaData"
                              v-model="model.iamAuditLawsRelEntitys"
                              :class="$style.deptPlanChild"
                              label=""
                              :read-only="!editable"
                              :fields="
                                [
                                  {
                                    caption: '序号',
                                    name: 'sortNum',
                                    dataType: 'number',
                                    attr: {},
                                  },
                                ].concat(Array.from(fields.iamAuditLawsRelEntitys.attr.dync))
                              "
                              :columns="checkChildColumns"
                              @change="changeNum"
                            >
                              <!-- input-form 表示这部分是子表组件的form插槽 -->
                              <template v-slot:form="{ model }">
                                <table>
                                  <tr>
                                    <!-- lawsTitle -->
                                    <sd-form-item-td name="lawsTitle" :colspan="3" />
                                  </tr>
                                  <tr>
                                    <!-- lawsDocNumber -->
                                    <sd-form-item-td name="lawsDocNumber" :colspan="3" />
                                  </tr>
                                  <tr>
                                    <!-- lawsChapter -->
                                    <sd-form-item-td name="lawsChapter" :colspan="3" />
                                  </tr>
                                  <tr>
                                    <!-- lawsArticle -->
                                    <sd-form-item-td name="lawsArticle" :colspan="3" />
                                  </tr>
                                  <tr>
                                    <!-- 编制人员 -->
                                    <sd-form-item-td :hidden="true" name="lawsLibraryId" />
                                    <!-- 编制时间 -->
                                    <sd-form-item-td
                                      name="lawsType"
                                      :hidden="true"
                                      :input-props="{ defaultValue: 'problem' }"
                                    />
                                  </tr>
                                </table>
                              </template>
                            </xm-child-table>
                          </template>
                        </audit-advanced-group>
                      </template> </sd-form-item
                  ></td>
                </tr>
              </table>
            </template>
          </sd-detail-form>
        </div>
      </div>
    </audit-form-top-banner>
    <audit-matters-select-modal
      :visible="visible"
      :org-id="orgId"
      @listSelected="listSelected"
    ></audit-matters-select-modal>
  </span>
</template>

<script>
import { Message, Modal } from 'ant-design-vue'
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 auditMattersSelectModal from '../../matters/audit-matters-select-modal.vue'
import components from './_import-components/iam-source-problem-form-import'

export default {
  name: 'IamSourceProblemForm',
  metaInfo() {
    return {
      title: '新建问题',
    }
  },
  components: { auditFormTopBanner, ...components, auditAdvancedGroup, auditMattersSelectModal },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      isHidden: true,
      defaultCategory: '',
      labelArray: [],
      mode: null,
      publishFlag: false,
      basicExpand: true,
      visible: false,
      CheckExpand: true,
      orgId: null,
      checkChildColumns: [
        { dataIndex: 'lawsLibraryId', sdHidden: true },
        { dataIndex: 'lawsType', sdHidden: true },
        { dataIndex: 'lawsTitle', sdHidden: false, width: '40%' },
        { dataIndex: 'lawsDocNumber', sdHidden: false, width: '15%' },
        { dataIndex: 'lawsChapter', sdHidden: false },
        {
          title: '序号',
          dataIndex: 'sortNum',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`,
        },
      ],
    }
  },
  created() {},
  mounted() {},
  methods: {
    changeNum(data) {
      data.forEach((item, index) => {
        item.sortNum = index + 1
      })
    },
    // 页面加载完成后
    sdFormReady() {
      this.mode = this.$refs.docform.formData.mode
    },
    handleChange(model, event) {
      model.label = event.join('、')
      this.labelArray = [...event]
    },
    handelPublish() {
      this.$refs.docform.setFieldValue('releaseType', 1)
      this.saveForm()
    },
    saveForm() {
      this.$refs.docform.saveBtnClick()
      return true
    },
    saved() {
      Message.success({ content: '保存成功！' }).then(() => {
        this.close(true)
      })
    },
    initParam(val) {
      setTimeout(() => {
        var flow = this.$refs.docform
        if (
          flow.getFieldValue('parentId') === null ||
          flow.getFieldValue('parentId') === undefined ||
          flow.getFieldValue('parentId') === ''
        ) {
          if (this.$route.query?.typeId) {
            this.$refs.docform.setFieldValue('parentId', this.$route.query.typeId)
          }
        }
      }, 0)
      return true
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.path, flag)
      window.close()
    },
    validForm() {
      return new Promise((resolve, reject) => {
        resolve(true)
      })
    },

    // 选择法律制度
    lawsRelSelect() {
      // 选择事项分类
      this.visible = true
    },
    listSelected(keys, keyinfos) {
      let iamAuditLaws = []
      if (this.$refs.docform.getFieldValue('iamAuditLawsRelEntitys')) {
        iamAuditLaws = this.$refs.docform.getFieldValue('iamAuditLawsRelEntitys')
      }

      keyinfos.forEach((element) => {
        const mmc = {}
        mmc.lawsTitle = element.docTitle
        mmc.lawsDocNumber = element.dispatchWord ? element.dispatchWord : ''
        mmc.lawsChapter = ''
        mmc.lawsArticle = ''
        iamAuditLaws.push(mmc)
      })
      this.$refs.docform.setFieldValue('iamAuditLawsRelEntitys', iamAuditLaws)
    },
  },
}
</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;
  }
}
.btnselect {
  position: relative;
  top: 4px;
  right: 200px;
  float: right;
  .batchselect {
    z-index: 100;
    margin-left: 10px;
  }
}
</style>
