<template>
  <div class="form-wrap">
    <div
      class="btn-wrap-left"
      style="padding:10px 24px">
      <el-button
        type="primary"
        size="mini"
        @click="openDialog(false)"
      >
        新建通道组
      </el-button>
    </div>
    <div class="table-box">
      <div
        class="block-title"
      >
        通道组列表
      </div>
      <el-table
        border
        stripe
        :data="tableData">
        <el-table-column
          type="index"
          width="50px"
          label="#"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称"
        >
        </el-table-column>
        <el-table-column
          label="启停标志"
        >
          <template slot-scope="props">
            <StartStop :value="props.row.is_valid"></StartStop>
          </template>
        </el-table-column>
        <el-table-column
          prop="scenic_name"
          label="入景点"
        >
          <template slot-scope="props">
            {{ (props.row.scenics.length) ? props.row.scenics.map(v=>v.scenic_name).join(','):'未配置' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="locate_scenic_name"
          label="出景点"
        >
          <template slot-scope="props">
            {{ (props.row.locate_scenics.length) ? props.row.locate_scenics.map(v=>v.scenic_name).join(','):'未配置' }}
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
        >
          <template slot-scope="props">
            <span>{{ props.row.createTime | formatTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="160px"
          label="操作"
        >
          <template slot-scope="scope">
            <el-link
              type="primary"
              size="small"
              @click="openInfoDialog(scope.row)"
            >
              通道列表
            </el-link>
            <el-link
              type="primary"
              size="small"
              @click="openDialog(scope.row)"
            >
              编辑
            </el-link>
            <el-link
              type="primary"
              size="small"
              @click="deleteItem(scope.row)"
            >
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 弹框 -->
      <ElDialog
        :title="dialog.title"
        width="680px"
        v-model="dialog.visible"
      >
        <DialogEdit
          :type="dialog.type"
          :scenic-list="scenicList"
          :current-item="currentItem"
          @updateList="getList"
        ></DialogEdit>
      </ElDialog>
      <ElDialog
        :title="infoDialog.title"
        width="760px"
        v-model="infoDialog.visible"
      >
        <InfoDialog
          :list="infoDialog.list"
        ></InfoDialog>
      </ElDialog>
    </div>
  </div>
</template>

<script>
import { apiChannelGroup, apiChannel } from '@/api/checker'
import { getScenic } from '@/api/ticketType'
import ElDialog from '@/components/Dialog'
import DialogEdit from './channelGroup/DialogEdit'
import InfoDialog from './channelGroup/InfoDialog'

export default {
  data () {
    return {
      form: {
        startDate: '',
        endDate: '',
        keyWords1: '', // 名称
        keyWords2: '', // 是否开启
        pageNum: 1,
        pageSize: 10000
      },
      scenicList: [],
      tableData: [],
      dialog: {
        visible: false,
        type: 'new',
        title: '新增通道组'
      },
      currentItem: {},
      infoDialog: {
        visible: false,
        title: '',
        list: []
      }
    }
  },
  created () {
    this.getScenicList().then(() => {
      this.getList()
    })
  },
  components: {
    ElDialog,
    DialogEdit,
    InfoDialog
  },
  filters: {
    filterValid (n) {
      return n === 1 ? '启用' : '停用'
    }
  },
  methods: {
    // 获取景点列表
    getScenicList () {
      return getScenic().then(res => {
        let arr = res?.data.children.map(item => item.data)
        this.scenicList = arr
      })
    },
    // 获取通道组列表
    getList () {
      apiChannelGroup('getCheckerChannelGroupList', this.form).then(res => {
        this.tableData = res.list
      })
    },
    // 打开弹框
    openDialog (item) {
      if (item) {
        this.dialog.title = '编辑通道组'
        this.dialog.type = 'edit'
        this.currentItem = item
      } else {
        this.dialog.title = '新建通道组'
        this.dialog.type = 'new'
      }
      this.dialog.visible = true
    },
    deleteItem (item) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        apiChannelGroup('deleteCheckerChannelGroup', item).then(() => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    openInfoDialog (item) {
      this.infoDialog.title = item.name
      apiChannel('getCheckerChannelList', {
        keyWords3: item.id,
        pageNum: 1,
        pageSize: 10000
      }).then(res => {
        this.infoDialog.list = res.list
        this.infoDialog.visible = true
      })
    }
  }
}
</script>

<style scoped>

</style>
