<template>
  <dg-row>
    <!-- 搜索项 -->
    <search-bar ref="searchBar" :conditionForm="conditionForm" @submitSearch="receiveSearch"></search-bar>
    <!-- 表格 -->
    <new-table
      ref="table"
      :selection="true"
      :tableUrl="tableUrl"
      :tableHeader="tableHeader"
      :condition="condition"
      :tableOptList="tableOptList"
      @submitTableOpt="receviceOpt"
    >
    </new-table>
    <div class="dg-layer-demo" v-footer>
      <dg-button @click="handleClose">取消</dg-button>
      <dg-button type="primary" @click="handlSave">保存</dg-button>
    </div>
  </dg-row>
</template>

<script>
import * as Api from '@/api/admin-manage';
import newTable from '@/components/new-table';
import Editor from './noBindEditor';
import { mapGetters } from 'vuex';
import searchBar from '@/components/search-bar';
import { searchOpt } from '@/mixins/page-opt';
import { conditionForm, tableHeader } from './DataConfig';
export default {
  name: 'adminManageAdd',
  components: { newTable, searchBar },
  mixins: [searchOpt],
  computed: {
    ...mapGetters(['user'])
  },
  props: {
    orgId: String
  },
  data() {
    const vm = this;
    return {
      tableUrl: Api.tableUnAdminPolices,
      conditionForm: vm.$lodash.cloneDeep(conditionForm),
      tableHeader,
      tableOptList: ['管理范围'],
      orgSave: false,
      menuSave: false,
      orgKey: 0
    };
  },
  methods: {
    receviceOpt(type, row) {
      if (type == '管理范围') {
        this.handleDetail(row);
      }
    },
    handleClose() {
      this.$emit('close');
    },
    handlSave() {
      let ids = [];
      const selectionList = this.$refs.table.newChooseArr;
      let flag = false;
      let params = {
        userIds: ''
      };
      if (selectionList.length) {
        selectionList.some((item) => {
          if (!item.orgSave) {
            this.$message({ type: 'error', message: item.name + '未分配管理范围' });
            flag = true;
            return true;
          }
          // if (!item.menuSave) {
          //     this.$message({ type: "error", message: item.name + "未分配管理菜单" });
          //     flag = true;
          //     return true;
          // }
        });
        selectionList.forEach((item) => {
          ids.push(item.id);
        });
        params['userIds'] = ids.join(',');
        if (!flag) {
          Api.addAdmin(params)
            .then((res) => {
              this.$message.success('保存成功！');
              this.$emit('success');
            })
            .catch(() => {
              this.$message.error('保存失败！');
            });
        }
      } else {
        this.$message({ type: 'warning', message: '至少选择一条！' });
      }
    },
    handleDetail(row) {
      const that = this;
      const { id } = row;

      const layer = that.$dgLayer({
        title: '修改',
        shade: [0.4, '#FFF'],
        area: ['830px', '660px'],
        content: Editor,
        props: {
          id,
          type: 'create'
        },
        on: {
          success() {
            layer.close(layer.dialogIndex);
            that.handleSearch();
          },
          oneSaveOrg(boo) {
            row.orgSave = boo;
            that.$refs.table.toggleRowSelection(row, true);
          },
          oneSaveMenu(boo) {
            row.menuSave = boo;
            that.$refs.table.toggleRowSelection(row, true);
          }
        }
      });
    }
  },
  created() {
    this.condition = {
      orgId: {
        value: this.orgId,
        op: '='
      }
    };
  }
};
</script>
