<!--
批量设置用户名单级别
@Author: linqian
@Date: 2021-07-08 11:31
-->
<template>
  <div>
    <el-form inline>
      <el-form-item label="白名单级别：">
        <dg-select v-model="level" enum="ListLevelEnum"></dg-select>
      </el-form-item>
    </el-form>
    <p style="margin-bottom: 5px">选中的人员列表：</p>
    <dg-table :data="selectedData" border row-key="id" paging-type="client" :pagination-total="selectedData.length">
      <dg-table-column type="index" label="序号" width="75" align="center" />
      <dg-table-column label="姓名" prop="user" align="center">
        <template slot-scope="{ row }">
          {{ row.name || row.userName }}
        </template>
      </dg-table-column>
      <template v-for="item in tableHeader">
        <dg-table-column :key="item.id" v-bind="item" align="center"> </dg-table-column>
      </template>
    </dg-table>
    <div v-footer>
      <dg-button @click="handleCancel">取消</dg-button>
      <dg-button type="primary" @click="handleSubmit">确定</dg-button>
    </div>
  </div>
</template>

<script>
import { saveWhiteList, updateWhiteListLevel } from '@/api/list-manage';

export default {
  components: {},
  props: {
    selectedData: Array,
    operate: {
      type: String
    }
  },
  data() {
    return {
      tableHeader: [
        {
          label: '身份证号码',
          prop: 'idcard'
        },
        {
          label: '单位名称',
          prop: 'orgName'
        }
      ],
      level: '1' // 默认为低级别
    };
  },
  computed: {},
  methods: {
    handleCancel() {
      this.$emit('close');
    },
    // 保存
    save() {
      const params = {
        level: this.level,
        subIds: this.selectedData.map((item) => item.id)
      };   
      return new Promise((resolve) => {
        saveWhiteList(params).then((res) => {
          resolve(res.data);
          this.$message.success('保存成功！');
        });
      });
    },
    // 设置级别
    setLevel() {
      const params = {
        level: this.level,
        listIds: this.selectedData.map((item) => item.listId)
      };
      return new Promise((resolve) => {
        updateWhiteListLevel(params).then((res) => {
          resolve(res.data);
          this.$message.success('设置成功！');
        });
      });
    },

    async handleSubmit() {
      // 添加白名单
      if (this.operate == 'add') {
        await this.save();
      } else {
        // 设置白名单
        await this.setLevel();
      }
      this.$emit('success');
    }
  },
  created() {},
  mounted() {}
};
</script>

<style lang='scss'>
</style>