<template>
  <div>
    <sd-webflow
      ref="docform"
      :key="key"
      :validate-form="validForm"
      :removed-tabs="['sdRelatedDoc']"
      :class="$style.form"
      @actionBtnClick="actionBtnClick"
    >
      <template v-slot:form="{ model, fields, FlowData }">
        <table>
          <colgroup>
            <col style="width: 15%;"/>
            <col style="width: 35%;"/>
            <col style="width: 15%;"/>
            <col style="width: 35%;"
          /></colgroup>
          <tr v-show="false">
            <!-- 标题 -->
            <sd-form-item-td name="title" :colspan="3" />
          </tr>
          <tr>
            <td colspan="4" style="border:none">
              <audit-advanced-group
                :expand="expandJ"
                :expand-str="'expandJ'"
                :group-label="'基本信息'"
                @changedClick="changedClick"
              />
            </td>
          </tr>
          <tr v-show="expandJ">
            <!-- 申请人 -->
            <sd-form-item-td name="creatorName" />
            <!-- 模型名称 -->
            <sd-form-item-td name="modelName" />
          </tr>
          <tr v-show="expandJ">
            <!-- 申请时间 -->
            <sd-form-item-td name="creationTime" />
            <!-- 授权类型 -->
          </tr>

          <tr v-show="expandJ">
            <!-- 申请原因 -->
            <sd-form-item-td name="empowerReason" :colspan="3">
              <a-textarea v-model="model.empowerReason" :rows="3" />
            </sd-form-item-td>
          </tr>
          <tr
            ><td
              v-if="getmaintaindata(model, fields)"
              :colspan="4"
              style="line-height:40px;border:none1;"
              :class="$style.maintaintable"
            >
              <audit-advanced-group
                :expand="expandModel"
                :expand-str="'expandY'"
                :group-label="'模型列表'"
                @changedClick="changedClick"
                ><template>
                  <sd-table
                    ref="maintaintable"
                    :loading="false"
                    class="maintaintable"
                    :columns="columns"
                    :data-source="modelTableList"
                    :pagination="false"
                    :row-key="(record, index) => index"
                    :bordered="true"
                  >
                    <template slot="link" slot-scope="text, record">
                      <a @click="onlink(record.id)">{{ text }}</a>
                    </template>
                  </sd-table></template
                ></audit-advanced-group
              >
            </td>
          </tr>
        </table>
      </template>
    </sd-webflow>
  </div>
</template>

<script>
import components from './_import-components/audit-model-sqapply-import'
import { Modal, message } from 'ant-design-vue'
import auditAdvancedGroup from '../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import axios from '@/common/services/axios-instance'
import auditModelService from './audit-model-service'
import auditMaintainService from '../maintain/audit-maintain-service'
import moment from 'moment'
export default {
  name: 'AuditModelGxapply',
  metaInfo: {
    title: 'AuditModelGxapply',
  },
  components: {
    ...components,
    auditAdvancedGroup,
  },
  mixins: [auditAdvancedGroupMixins],

  data() {
    return {
      key: 0,
      expandJ: true,
      expandModel: true,
      modelTableList: [],
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNum',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        // 模型名称
        // 模型编号
        // 模型描述
        {
          title: '模型名称',
          dataIndex: 'modelName',
          scopedSlots: { customRender: 'link' },
        },
        {
          title: '模型编号',
          dataIndex: 'modelCode',
        },
        {
          title: '模型描述',
          dataIndex: 'modelDesc',
        },
      ],
      selectedRowKeys: [],
      rules: [
        {
          required: true,
          trigger: ['change', 'blur'],
        },
      ],
      maintaineditable: false,
      modelFlag: true,
      maintaindata: [],
      tableFilter: ['modelId', 'modelCode', 'modelName', 'modelDesc'],
    }
  },
  created() {
    this.initModelData()
  },
  methods: {
    onlink(id) {
      window.open('#/audit-model-view?record=' + id)
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    changeType() {
      const empowerType = this.$refs.docform.getFieldValue('empowerType')
      if (empowerType === '1') {
        this.$refs.docform.setFieldValue('startDate', '')
        this.$refs.docform.setFieldValue('endDate', '')
      }
      if (empowerType === '2') {
        // 设置开始时间 为当前时间
        // 结束时间为当年最后一天
        // 使用moment格式
        const start = moment()
        const end = moment().endOf('year')
        this.$refs.docform.setFieldValue('startDate', start)
        this.$refs.docform.setFieldValue('endDate', end)
      }
      this.saveForm()
    },
    actionBtnClick(evt, { button, FlowData }) {
      if (button.fakeId === 'save' || button.fakeId === 'workflow-push') {
        // 保存前先校验计划是否已经存在
        evt.waitUntil(
          new Promise((resolve, reject) => {
            this.saveForm().then((res) => {
              if (!res) {
                evt.preventDefault()
              }
              resolve()
            })
          })
        )
      }
    },
    saveForm() {
      const save = new Promise((resolve) => {
        this.$refs.docform.setFieldValue('docType', this.docType)
        resolve(true)
      })
      return save
    },
    validForm() {
      this.$refs.docform.setFieldValue(
        'title',
        this.$refs.docform.getFieldValue('creatorName') + '模型共享申请'
      )
      return Promise.resolve(true)
    },
    // 获取模型数据 (保存后)
    getmaintaindata(model, fields) {
      if (this.modelFlag) {
        // this.maintaineditable = !fields.iamLicenseUnitEntitys.readonly
        const id = model.id
        if (id) {
          const obj = {}
          this.tableFilter.map((item) => {
            obj[item] = fields[item].value
          })
          obj.id = obj.modelId
          this.modelTableList = [obj]
        }
        this.modelFlag = false
      }
      return true
    },
    // 获取模型数据 (保存前)
    initModelData() {
      if (!this.$route.query.extParams) return
      const extParams = JSON.parse(this.$route.query.extParams)
      auditModelService.getModelDetail(extParams.record).then((res) => {
        const tableFields = [...this.tableFilter]
        tableFields[0] = 'id'
        const fields = res.data.pageFormData.pageFieldInfos.filter((item) =>
          tableFields.includes(item.name)
        )
        const obj = {}
        fields.map((item) => {
          obj[item.name] = item.value
        })

        this.modelTableList = [obj]
        this.$refs.docform.setFieldValue('modelName', obj.modelName)
        this.$refs.docform.setFieldValue('modelCode', obj.modelCode)
        this.$refs.docform.setFieldValue('modelId', obj.id)
        // this.key += 1
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
@import '@/webflow/sd-flow-form.scss';

.btnselect {
  position: relative;
  top: 2px;
  right: 80px;
  float: right;
  .batchselect {
    z-index: 100;
    margin-left: 10px;
  }
}
.btnselectmaintain {
  position: relative;
  float: right;
  .batchselect {
    z-index: 100;
    margin-right: -10px;
    margin-left: 10px;
  }
}
.maintaintable {
  :global(.ant-table-thead > tr > th) {
    border-right: none;
  }
  :global(.ant-table-bordered.ant-table-empty .ant-table-placeholder) {
    border: none;
  }
  :global(.ant-table-body table) {
    //  border-left:none;
    border: none;
  }
}
</style>
