<template>
  <section class="AppPetitionManage">
    <ai-list>
      <ai-title slot="title" title="广播播发" isShowBottomBorder/>
      <template #content>
        <ai-search-bar bottomBorder>
          <template slot="left">
            <ai-select v-model="search.messageType" placeholder="媒资类型" clearable
                       :selectList="$dict.getDict('dlbResourceType')"
                       @change=";(page.current = 1), getList()"></ai-select>
            <ai-select v-model="search.messageUrgency" placeholder="级别" clearable
                       :selectList="$dict.getDict('dlbMessageUrgency')"
                       @change=";(page.current = 1), getList()"></ai-select>
          </template>
          <template slot="right">
            <el-input v-model="search.messageName" size="small" placeholder="媒资名称" clearable
                      @keyup.enter.native=";(page.current = 1), getList()"
                      @clear=";(page.current = 1), (search.messageName = ''), getList()"
                      suffix-icon="iconfont iconSearch"/>
          </template>
        </ai-search-bar>
        <ai-search-bar class="ai-search-ba mar-t10">
          <template slot="left">
            <el-button icon="iconfont iconAdd" type="primary" size="small" @click="onAdd('')">添加</el-button>
            <!-- <el-button icon="iconfont iconDelete" size="small" @click="removeAll" :disabled="ids.length == 0">删除 </el-button> -->
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :col-configs="colConfigs" :total="total" :dict="dict"
                  :current.sync="page.current" :size.sync="page.size" @getList="getList"
                  @selection-change="(v) => (ids = v.map((e) => e.id))">
          <el-table-column slot="options" label="操作" align="center" width="180" fixed="right">
            <template slot-scope="{ row }">
              <el-button type="text" @click="onAdd(row.broadcastId)">复制</el-button>
              <el-button type="text" @click="cancel(row.broadcastId)"
                         v-if="row.broadcastStatus == 0 || row.broadcastStatus == 1 || row.broadcastStatus == 2">撤回
              </el-button>
            </template>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
  </section>
</template>

<script>
import {mapState} from 'vuex'

export default {
  name: 'List',
  props: {
    dict: Object,
    instance: Function,
    params: Object,
  },

  data() {
    return {
      isAdd: false,
      page: {
        current: 1,
        size: 10,
      },
      total: 0,
      search: {
        messageName: '',
        messageType: '',
        messageUrgency: '',
      },
      id: '',
      ids: [],
      colConfigs: [
        {prop: 'messageName', label: '媒资名称', width: 400},
        {prop: 'messageType', label: '媒资类型', align: 'center', dict: "dlbResourceType"},
        {prop: 'messageUrgency', label: '级别', align: 'center', dict: "dlbMessageUrgency"},
        {prop: 'taskType', label: '播发方式', align: 'center', dict: "dlbBroadTaskType"},
        {prop: 'startDate', label: '开始时间', align: 'center'},
        {prop: 'broadcastStatus', label: '状态', align: 'center', dict: "dlbBroadcastStatus"},
        {prop: 'areaName', label: '地区', align: 'center'},
        {prop: 'createUserName', label: '创建人', align: 'center'},
        {slot: 'options'},
      ],
      tableData: [],
      areaId: '',
    }
  },

  computed: {
    ...mapState(['user']),

    param() {
      return {
        ...this.search,
        areaId: this.user.info?.areaId,
        ids: this.ids,
      }
    },
  },

  created() {
    this.areaId = this.user.info.areaId
    this.dict.load('dlbResourceType', 'dlbMessageUrgency', 'dlbBroadTaskType', 'dlbBroadcastStatus', 'dlbMessageUrgency').then(() => {
      this.getList()
    })
  },

  methods: {
    getList() {
      this.instance.post(`/app/appzyvideobroadcast/getBroadcastRecords`, null, {
        params: {
          ...this.page,
          ...this.search,
        },
      })
      .then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.records
          this.total = parseInt(res.data.total)
        }
      })
    },
    onAdd(id) {
      this.$emit('change', {
        type: 'add',
        params: {
          id: id || ''
        }
      })
    },
    cancel(id) {
      this.$confirm('确定撤回该广播?').then(() => {
        this.instance.post(`/app/appzyvideobroadcast/getBroadcastRecall?broadcastId=${id}`).then((res) => {
          if (res.code == 0) {
            this.$message.success('撤回成功！')
            this.getList()
          }
        })
      })
    },

    removeAll() {
      var id = this.ids.join(',')
      this.remove(id)
    },
  },
}
</script>

<style lang="scss" scoped>
.AppPetitionManage {
  height: 100%;

  .mar-t10 {
    margin-top: 10px;
  }
}
</style>
