<template>
  <auditFormTopBanner :handel-save-form="saveForm" @handelSaveForm="saveForm">
    <div class="title_sd-detail-form_common">字段绑定</div>
    <table
      ><tr
        ><td style="padding-left:5px;border:none">
          <auditAdvancedGroup
            :expand="templateExpand"
            :expand-str="'templateExpand'"
            :group-label="'组织管理数据'"
            tablestyle="''"
            @changedClick="changedClick"
          >
            <SdDataTable
              ref="SJMXDataTable"
              data-url="api/xcoa-mobile/v1/group-manger/list"
              :columns="zzColumns"
              :filter-expressions="zzExpressions"
              :show-selection="false"
              :hide-pagination="true"
              :show-advance-query="false"
            >
            </SdDataTable>
          </auditAdvancedGroup> </td></tr
    ></table>

    <table
      ><tr
        ><td style="padding-left:5px;border:none">
          <auditAdvancedGroup
            :expand="dataExpand"
            :expand-str="'dataExpand'"
            :group-label="'关联系统数据'"
            @changedClick="changedClick"
          >
            <div>
              <audit-advanced-query
                :expand="expand"
                :search-data="formData"
                :ref-name="searchform"
                :search-style="{ height: '150px', left: '20px', top: '57px' }"
                :search-fun="handleSearch"
                @searchedClick="searchedClick"
              >
                <template>
                  <a-col :span="6">
                    <a-form-model-item :label="'系统名称'" prop="sys_name">
                      <!-- <a-input v-model="formData.sys_name" allow-clear /> -->
                      <!-- 可搜索下拉框 -->
                      <a-select
                        v-model="formData.sys_name"
                        allow-clear
                        show-search
                        placeholder="请输入系统名称"
                        option-filter-prop="children"
                        filter-option
                      >
                        <a-select-option
                          v-for="(item, index) in systemNameList"
                          :key="index"
                          :value="item"
                        >
                          {{ item }}
                        </a-select-option>
                      </a-select>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6">
                    <a-form-model-item :label="'级别'" prop="level">
                      <a-input v-model="formData.level" allow-clear :disabled="true" />
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6">
                    <a-form-model-item :label="'代码'" prop="org_code">
                      <a-input v-model="formData.org_code" allow-clear />
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6">
                    <a-form-model-item :label="'名称'" prop="org_name">
                      <a-input v-model="formData.org_name" allow-clear />
                    </a-form-model-item>
                  </a-col>
                </template>
              </audit-advanced-query>
              <SdDataTableEx
                ref="GlSJMXDataTable"
                :filter-expressions="expressions"
                :columns="glColumns"
                check-type="radio"
                form-id="iamoperationrecord"
                data-url="api/xcoa-mobile/v1/iamoperationrecord/list"
                :show-advance-query="true"
                show-selection
                @searchbtnClick="searchbtnClick"
              >
                <!-- <div slot="jbTest">公司</div> -->
              </SdDataTableEx>
            </div>
          </auditAdvancedGroup>
        </td></tr
      ></table
    >
  </auditFormTopBanner>
</template>

<script>
import auditFormTopBanner from '../../components/audit-form-top-banner.vue'
import SdDataTableEx from '@/common/components/sd-data-table-ex.vue'
import SdDataTable from '@/common/components/sd-data-table.vue'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import auditAdvancedGroup from '../../components/audit-advanced-group.vue'
import MasterDataService from './master-data-service.js'
import { Message } from 'ant-design-vue'
import { getUserInfo } from '@/common/store-mixin'
import auditAdvancedQuery from '../../components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '../../components/audit-advanced-query-mixins'

export default {
  name: 'MasterdataFieldBinding',
  metaInfo: {
    title: '主数据字段绑定',
  },
  components: {
    auditFormTopBanner,
    auditAdvancedGroup,
    SdDataTableEx,
    SdDataTable,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedGroupMixins, auditAdvancedQueryMixins],
  data() {
    return {
      expand: false,
      templateExpand: true,
      dataExpand: true,
      searchform: 'GlSJMXDataTable',
      zzExpressions: [
        {
          dataType: 'long',
          name: 'id',
          op: 'eq',
          longValue: this.$route.query.id,
        },
      ],
      expressions: [],

      selectedRowKeys: [],
      zzData: [],
      zzColumns: [
        {
          dataIndex: 'id',
          sdHidden: true,
        },
        {
          title: '代码',
          dataIndex: 'code',
        },
        {
          title: '名称',
          dataIndex: 'name',
        },
        {
          title: '排序',
          dataIndex: 'weight',
        },
        {
          title: '级别',
          dataIndex: 'level',
        },
        {
          title: '备注',
          dataIndex: 'description',
        },
      ],
      glListData: [],
      glColumns: [
        {
          title: '系统名称',
          dataIndex: 'sys_name',
        },
        {
          title: '代码',
          dataIndex: 'org_code',
        },
        {
          title: '名称',
          dataIndex: 'org_name',
        },
      ],
      formData: {
        org_code: '',
        org_name: '',
        sys_name: '',
        sys_code: '',
        level: '公司',
      },
      systemNameList: [],
    }
  },
  created() {
    this.getSystemList()
  },
  methods: {
    // 获取系统名称列表
    getSystemList() {
      MasterDataService.getSystemNameList().then((res) => {
        this.systemNameList = res.data
      })
    },
    saveForm() {
      const key = this.$refs.GlSJMXDataTable.getSelectedRowKeys()
      const info = this.$refs.GlSJMXDataTable.$refs.dataTable.data[key]
      const selectOperation = []
      selectOperation.push(info)
      // 如果没有选择数据，提示用户选择数据 最大只能选择一条
      if (selectOperation.length === 0) {
        Message.error('请选择数据', 1)
        return
      }
      if (selectOperation.length > 1) {
        Message.error('最多只能选择一条数据', 1)
        return
      }
      const params = {
        eventId: 'save', // 保存标识
        inputs: [
          {
            name: 'groupId', // 参数名称
            value: this.$route.query.id, // 参数数据
          },
          {
            name: 'sysName',
            value: selectOperation[0].sys_name,
          },
          {
            name: 'code',
            value: selectOperation[0].org_code,
          },
          {
            name: 'level',
            value: '公司',
          },
          {
            name: 'name',
            value: selectOperation[0].org_name,
          },
          {
            name: 'operation',
            // 当前用户
            value: getUserInfo().name,
          },
          {
            name: 'operationTime',
            // 当前时间时间戳
            value: new Date().getTime(),
          },
        ],
        pageFlowId: '08ecc6f9-ed1d-4559-98ea-76a0e9661d6a', // 唯一标识
        pagePath: 'audit/maintain/iamOperationAssociationRecord', // 当前操作的实体类
      }

      MasterDataService.saveMasterData(params).then((res) => {
        // 保存成功后的回调
        Message.success('保存成功', 1).then((res) => {
          // this.close(true)
          window.close
        })
      })
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    handleSearch() {
      this.expressions = []
      // 系统名称
      if (this.formData.sys_name) {
        this.expressions.push({
          dataType: 'string',
          name: 'sys_name',
          op: 'eq',
          stringValue: this.formData.sys_name,
        })
      }
      // 代码
      if (this.formData.org_code) {
        this.expressions.push({
          dataType: 'string',
          name: 'org_code',
          op: 'eq',
          stringValue: this.formData.org_code,
        })
      }
      // 名称
      if (this.formData.org_name) {
        this.expressions.push({
          dataType: 'string',
          name: 'org_name',
          op: 'eq',
          stringValue: this.formData.org_name,
        })
      }
    },
  },
}
</script>
