<!--version: 2, 设备选择弹窗-->

<template>
  <el-dialog :title="title" :visible="show" width="1000px" top="2vh" @open="open" @close="close"
             :append-to-body="true">
    <el-form size="small" :inline="true" label-width="5em" @submit.prevent.native="onSearch">
      <el-form-item label="渠道名称" prop="name">
        <el-input
          v-model="searchForm.name"
          placeholder="请输入渠道名称"
          clearable
          @keyup.enter.native="onSearch"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch()" icon="el-icon-search">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row v-if="multiple" style="margin-bottom: 1em;">
      <span style="line-height: 28px">已选：</span>
      <span style="line-height: 28px" v-if="selected == null || selected.length === 0">暂无</span>
      <el-tag
        v-for="(item,index) of selected"
        type="primary"
        closable
        disable-transitions
        style="margin-right: 4px"
        @close="onCloseSelected(index)"
      >{{item.name}}</el-tag>
    </el-row>

    <el-table
      ref="multipleTable"
      :data="tableData"
      v-loading="loadTable"
      @row-click="changeSelection"
      @row-dblclick="select"
      @select-all="selectionAll"
      @select="changeSelection"
      highlight-current-row
    >
      <el-table-column type="selection" v-if="multiple"/>
      <el-table-column label="ID" align="center" prop="channelId" width="80"/>
      <el-table-column label="图片" align="center" prop="picture" width="80">
        <image-preview slot-scope="d" :src="d.row.picture" :width="32" :height="32"/>
      </el-table-column>
      <el-table-column label="渠道名称" align="center" prop="name"/>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="d">
          <dict-tag :value="d.row.type" :options="dict.type.recharge_channel_type"/>
        </template>
      </el-table-column>
      <el-table-column label="适用平台" align="center" prop="platform">
        <template slot-scope="d">
          <dict-tag :value="d.row.platform" :options="dict.type.channel_platform"/>
        </template>
      </el-table-column>
      <el-table-column label="API类型" align="center" prop="apiType">
        <template slot-scope="d">
          <dict-tag :value="d.row.apiType" :options="dict.type.channel_api_type"/>
        </template>
      </el-table-column>
      <el-table-column label="渠道成本" align="center" prop="costRate">
        <template slot-scope="d">{{d.row.costRate | money}} %</template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
    </el-table>
    <pagination
      :limit.sync="searchForm.pageSize"
      :page.sync="searchForm.pageNum"
      :total="total"
      @pagination="searchList">
    </pagination>
    <template #footer>
      <el-button type="primary" @click="submit()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {clone} from "@/utils";
import { listChannel } from '@/api/system/channel';

export default {
  name: "ChannelDialog",
  dicts: ['recharge_channel_type', 'channel_platform', 'channel_api_type'],
  props: {
    // 标题
    title: {
      type: String,
      default: '选择渠道'
    },
    value: {
      type: [String, Number, Array],
      default: null,
    },
    multiple: {
      type: Boolean,
      default: false,
    },
    show: {
      type: Boolean,
      default: false
    },
    query: {
      type: Object,
      default: () => ({})
    },
    initSelect: {
      type: Array,
      default: null,
    }
  },
  data() {
    return {
      loadTable: false,
      tableData: [],
      searchForm: {},
      total: 0,
      row: null,
      selected: [],
    }
  },
  methods: {
    onCloseSelected(index) {
      this.selected.splice(index, 1);
      this.$nextTick(() => {
        this.refreshTableSelection();
      })
    },
    // 获取数据列表
    searchList() {
      this.loadTable = true;
      listChannel(this.searchForm).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
        // 刷新表格状态
        this.$nextTick(()=>{
          this.refreshTableSelection();
        })
      }).finally(() =>{
        this.loadTable = false;
      })
    },
    // 打开时
    open() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        ...this.query,
      }

      if (this.initSelect) {
        this.selected = clone(this.initSelect);
      } else {
        this.selected = [];
      }

      this.searchList();
    },
    // 刷新表格的选中状态
    refreshTableSelection() {
      if(this.multiple){
        this.tableData.forEach(item => {
          if (this.selected.map(j => j.channelId).includes(item.channelId)) {
            this.$refs.multipleTable.toggleRowSelection(item, true);
          } else {
            this.$refs.multipleTable.toggleRowSelection(item, false);
          }
        });
      }
    },
    // 全选
    selectionAll(val){
      let flag = val.length > 0;
      this.tableData.forEach(item => {
        if (flag && !this.selected.map(i => i.channelId).includes(item.channelId)){
          this.selected.push(item);
        } else if (!flag && this.selected.map(i => i.channelId).includes(item.channelId)){
          this.selected = this.selected.filter(i => i.channelId !== item.channelId);
        }
      })
    },
    // 确认选中
    submit() {
      if (this.multiple) {
        this.$emit('select', this.selected);
      } else {
        this.select(this.row);
      }
    },
    // 更换某一行的选中状态
    changeSelection(row){
      if(this.multiple){
        if (this.selected.map(i => i.channelId).includes(row.channelId)){
          this.$refs.multipleTable.toggleRowSelection(row, false);
          this.selected = this.selected.filter(i => i.channelId !== row.channelId);
        }else {
          this.$refs.multipleTable.toggleRowSelection(row, true);
          this.selected.push(row);
        }
      } else {
        this.row = row;
      }
    },
    // 点击搜索
    onSearch() {
      this.searchForm.pageNum = 1;
      this.searchList();
    },
    // 关闭弹窗
    close() {
      this.$emit('update:show', false);
      this.searchForm.pageNum = 1;
      this.searchForm.pageSize = 10;
    },
    // 选中一行
    select(row) {
      if (!this.multiple) {
        if (!row) return this.$message.error('请选择一行');
        this.selected = [row];
        this.$emit('select', row);
      }
    },
  },
}
</script>
