<template>
  <div class="form-wrap ">
    <div class="search-box">
      <div class="block-title">
        查询条件
      </div>
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        label-width="70px"
      >
        <el-form-item
          label="名称"
          prop="keyWords1">
          <el-input v-model="form.keyWords1"></el-input>
        </el-form-item>
        <el-form-item
          label="通道组"
          prop="keyWords3">
          <el-select v-model="form.keyWords3">
            <el-option
              label="全部"
              value="">
            </el-option>
            <el-option
              v-for="(item, index) in channelGroupList"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="启停"
          prop="keyWords2">
          <el-select v-model="form.keyWords2">
            <el-option
              label="全部"
              value=""
            ></el-option>
            <el-option
              label="启用"
              :value="1"
            ></el-option>
            <el-option
              label="停用"
              :value="0"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="进出"
          prop="keyWords4">
          <el-select v-model="form.keyWords4">
            <el-option
              label="全部"
              value=""
            ></el-option>
            <el-option
              label="进口"
              :value="1"
            ></el-option>
            <el-option
              label="出口"
              :value="2"
            ></el-option>
            <el-option
              label="双向"
              :value="3"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="btn-wrap">
        <el-button
          type="default"
          size="mini"
          @click="reset">
          重置
        </el-button>

        <el-button
          type="primary"
          size="mini"
          @click="getList">
          查询
        </el-button>

        <el-button
          type="primary"
          size="mini"
          @click="openDialog(false)">
          新建通道
        </el-button>
      </div>
    </div>

    <div class="table-box">
      <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
          width="80"
          prop="type"
          label="类型">
        </el-table-column>
        <el-table-column
          prop="group_name"
          label="通道组">
        </el-table-column>
        <el-table-column
          prop="scenic_name"
          label="景点">
        </el-table-column>
        <el-table-column
          width="50"
          label="进出">
          <template slot-scope="props">
            <span>{{ props.row.is_in | filterInOut }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="80"
          label="当日进"
          prop="dayEnter"></el-table-column>
        <el-table-column
          width="80"
          label="当日出"
          prop="dayOut"></el-table-column>
        <el-table-column
          width="140"
          label="创建时间">
          <template slot-scope="props">
            <span>{{ props.row.createTime | formatTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="120"
          label="状态">
          <template slot-scope="scope">
            <span>{{ scope.row.is_valid ? '已启用' : '已停用' }}</span>
            <el-button
              type="text"
              size="small"
              @click="offSale(scope.row)"
              class="offSale-btn"
              v-if="scope.row.is_valid"
            >
              <i class="iconfont icon-offSale"></i>停用
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="onSale(scope.row)"
              class="onSale-btn"
              v-else
            >
              <i class="iconfont icon-onSale"></i>启用
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          width="200px"
          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>
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page.sync="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        @size-change="handleSizeChange"
        @current-change="getList()"
        :total="total"
      >
      </el-pagination>
      <!-- 弹框 -->
      <ElDialog
        :title="dialog.title"
        width="760px"
        v-model="dialog.visible">
        <DialogEdit
          :type="dialog.type"
          :group-list="channelGroupList"
          :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 { apiChannel, apiChannelGroup, getDeviceList } from '@/api/checker'
import ElDialog from '@/components/Dialog'
import DialogEdit from './channelManage/DialogEdit'
import InfoDialog from './channelManage/InfoDialog'
export default {
  data () {
    return {
      form: {
        'startDate': '',
        'endDate': '',
        'keyWords1': '', // 名称
        'keyWords2': '', // 是否开启
        keyWords3: '',
        keyWords4: '',
        keyWords5: '',
        pageNum: 1,
        pageSize: 10
      },
      channelGroupList: [],
      tableData: [],
      total: 0,
      dialog: {
        visible: false,
        type: 'new',
        title: '新增通道'
      },
      currentItem: {},
      infoDialog: {
        visible: false,
        title: '',
        list: []
      }
    }
  },
  created () {
    this.getList()
    this.getChannelGroup()
  },
  components: {
    ElDialog,
    DialogEdit,
    InfoDialog
  },
  filters: {
    filterInOut (n) {
      switch (n) {
        case 1:
          return '进口'
        case 2:
          return '出口'
        case 3:
          return '双向'
      }
    }
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    // 获取通道组列表
    getChannelGroup () {
      apiChannelGroup('getCheckerChannelGroupList', { pageNum: 1, pageSize: 10000 }).then(res => {
        this.channelGroupList = res.list
      })
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },

    // 获取通道列表
    getList () {
      apiChannel('getCheckerChannelList', this.form).then(res => {
        this.total = res.total
        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(() => {
        apiChannel('deleteCheckerChannel', item).then(() => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    openInfoDialog (item) {
      this.infoDialog.title = item.name
      getDeviceList({
        keyWords6: item.id,
        pageNum: 1,
        pageSize: 10000
      }).then(res => {
        this.infoDialog.list = res.list
        this.infoDialog.visible = true
      })
    },
    offSale (data) {
      let form = Object.assign({}, data)
      form.is_valid = 0
      apiChannel('updateCheckerChannel', form).then(res => {
        this.$message.success('操作成功')
        this.getList()
      })
    },
    onSale (data) {
      let form = Object.assign({}, data)
      form.is_valid = 1
      apiChannel('updateCheckerChannel', form).then(res => {
        this.$message.success('操作成功')
        this.getList()
      })
    }
  }
}
</script>

<style scoped>

</style>
