<template>
  <main class="add-dialog">
    <dg-row class="property-wapper">
      <!-- 搜索栏 -->
      <search-bar :conditionForm="conditionForm" @submitSearch="receiveSearch"></search-bar>
      <!-- 表格 -->
      <new-table
        ref="table"
        :selection="true"
        :tableHeader="tableHeader"
        :tableUrl="tableUrl[attrBelongType]"
        :condition="condition"
        @selection-change="handleSelectChange"

      >
      </new-table>
    </dg-row>
    <div v-footer>
      <dg-button @click="handleClose">取消</dg-button>
      <dg-button type="primary" @click="handleSave">保存</dg-button>
    </div>
  </main>
</template>

<script>
import { relateSourceTableHeader, relateSourceCondition } from '../../auth-subject-manage/DataConfig';
import { addAttrRelateSource, ObjNotRelAppTableUrl, ObjNotRelServiceTableUrl } from '@/api/data-auth';
import searchBar from '@/components/search-bar';
import { searchOpt } from '@/mixins/page-opt';
import newTable from '@/components/new-table';
export default {
  name: 'add-dialog',
  props: {
    attrBelongType: String,
    parentNode: Object
  },
  components: { searchBar, newTable },
  mixins: [searchOpt],
  data() {
    const vm = this;
    return {
      tableHeader: relateSourceTableHeader[vm.attrBelongType],
      tableUrl: {
        APP: ObjNotRelAppTableUrl,
        SER: ObjNotRelServiceTableUrl
      },
      conditionForm: [
        ...relateSourceCondition[vm.attrBelongType],
        {
          name: 'attrId',
          value: this.parentNode.id
        }
      ],
      condition: {
        attrId: {
          value: this.parentNode.id,
          op: '='
        }
      },
      selectList: []
    };
  },
  computed: {}, // 计算属性
  watch: {}, // 侦听器（扩展的计算属性）
  methods: {
    /**
     * 关闭
     */
    handleClose() {
      this.$emit('close');
    },
    handleSelectChange(selection) {
      this.selectList = this.$lodash.cloneDeep(selection);
    },
    /**
     * 添加
     */
    handleSave() {
      const params = {
        attrId: this.parentNode.id,
        relList: this.selectList.map((item) => {
          return {
            id: item.id,
            code: this.attrBelongType == 'APP' ? item.applyCode : item.serviceCode
          };
        })
      };
      addAttrRelateSource(params).then((res) => {
        this.$message.success('保存成功');
        this.$emit('success');
      });
    }
  },
  created() {},
  mounted() {
  }
};
</script>
<style lang="scss" scoped></style>
