<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="数据权限名称"
        prop="name">
        <el-input v-model="form.name" clearable></el-input>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getList(true)">
          搜索
        </el-button>
        <el-button
          type="primary"
          @click="openAddDialog(true)">
          新增子项
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <!-- <div class="block-title">
        数据权限列表
      </div> -->
      <el-table
        border
        v-loading="loading"
        :data="tableData">
        <el-table-column
          prop="name"
          label="数据权限名称">
        </el-table-column>
        <el-table-column
          prop="adminIds"
          label="管理员">
          <template slot-scope="scope">
            {{ getNameList(scope.row.admins, 'loginName') }}
          </template>
        </el-table-column>
        <el-table-column
          prop="ticketTypeIds"
          label="票种">
          <template slot-scope="scope">
            {{ getNameList(scope.row.ticketTypes) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="scenicIds"
          label="景点">
          <template slot-scope="scope">
            {{ getNameList(scope.row.scenicList) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="payChannelIds"
          label="支付渠道">
          <template slot-scope="scope">
            {{ getNameList(scope.row.payChannelList) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="checkerIds"
          label="检票设备">
          <template slot-scope="scope">
            {{ getNameList(scope.row.checkerList) }}
          </template>
        </el-table-column>
        <el-table-column
          width="150px"
          label="操作">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="openEditDialog(scope.row)"
            >
              编辑
            </el-link>

            <el-link
              type="primary"
              @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>
    </div>
    <!-- 弹框 -->

    <ElDialog
      :title="`${currentItem.id ? '编辑' : '新增'}子项`"
      width="500px"
      v-model="dialogVisible"
    >
      <el-form
        inline
        :model="currentItem"
        ref="form"
        class="form-wrap"
        label-width="150px"
        style="margin-top: 20px">
        <el-form-item
          label="数据权限名称"
          verify
          prop="name">
          <el-input v-model="currentItem.name"></el-input>
        </el-form-item>
        <el-form-item
          label="管理员"
          v-if="currentItem.id"
          prop="adminIds">
          <el-select
            multiple
            clearable
            v-model="currentItem.adminIds">
            <el-option
              v-for="item in adminList"
              :key="item.id"
              :value="item.id"
              :label="item.loginName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="票种"
          v-if="currentItem.id"
          prop="ticketTypeIds">
          <el-select
            multiple
            clearable
            v-model="currentItem.ticketTypeIds">
            <el-option
              v-for="item in ticketTypeList"
              :key="item.id"
              :value="item.id"
              :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="景点"
          v-if="currentItem.id"
          prop="scenicIds">
          <el-select
            multiple
            clearable
            v-model="currentItem.scenicIds">
            <el-option
              v-for="item in scenicList"
              :key="item.id"
              :value="item.id"
              :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="支付渠道"
          v-if="currentItem.id"
          prop="payChannelIds">
          <el-select
            multiple
            clearable
            v-model="currentItem.payChannelIds">
            <el-option
              v-for="item in payChannelList"
              :key="item.id"
              :value="item.id"
              :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="检票设备"
          v-if="currentItem.id"
          prop="checkerIds">
          <el-select
            v-model="currentItem.checkerIds"
            multiple
            clearable>
            <el-option
              v-for="item in deviceList"
              :key="item.id"
              :value="item.id"
              :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div style="margin: 20px; float: right;">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmOptItem">确定</el-button>
      </div>
    </ElDialog>
  </div>
</template>

<script>
import { getSubProjectList, addSubProject, updateSubProject, deleteSubProject, getDeviceList } from '@/api/checker'
import { getAccountList } from '@/api/systemSetting/account'
import { getTicketTypeList, getScenic } from '@/api/ticketType'
import { getPayChannel } from '@/api/payChannel'
import ElDialog from '@/components/Dialog'

export default {
  data () {
    return {
      form: {
        name: '', // 名称
        pageNum: 1,
        pageSize: 10
      },
      loading: false,
      tableData: [],
      total: 0,
      dialogVisible: false,
      configDialogVisible: false,
      currentItem: {},
      adminList: [],
      ticketTypeList: [],
      scenicList: [],
      payChannelList: [],
      deviceList: []
    }
  },
  created () {
    this.getList()

    getAccountList({ pageSize: -1, pageNum: 1 }).then(res => {
      this.adminList = res.data.records || []
    })

    getTicketTypeList({ pageSize: -1, pageNum: 1 }).then(res => {
      this.ticketTypeList = res.data.records || []
    })

    getScenic().then(res => {
      this.scenicList = res.data.records || []
    })

    getPayChannel({ pageSize: -1, pageNum: 1 }).then(res => {
      this.payChannelList = res.data.records || []
    })

    getDeviceList({ pageSize: -1, pageNum: 1 }).then(res => {
      this.deviceList = res.data.records || []
    })
  },
  components: {
    ElDialog
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    // 获取通道列表
    getList (goFirst) {
      let params = { ...this.form }

      goFirst && (params.pageNum = 1)
      this.loading = true
      getSubProjectList(params).then(res => {
        const list = res.data?.records || []
        this.total = res.data?.total || 0
        this.tableData = list
        this.loading = false
      })
    },
    // 打开弹框
    openAddDialog () {
      this.currentItem = {
        id: '',
        name: ''
      }
      this.dialogVisible = true
    },
    openEditDialog (item) {
      this.currentItem = {
        id: item.id,
        name: item.name,
        adminIds: item.admins ? item.admins.map(adm => adm.id) || [] : [],
        ticketTypeIds: item.ticketTypes ? item.ticketTypes.map(tt => tt.id) || [] : [],
        scenicIds: item.scenicList ? item.scenicList.map(sc => sc.id) || [] : [],
        payChannelIds: item.payChannelList ? item.payChannelList.map(pc => pc.id) || [] : [],
        checkerIds: item.checkerList ? item.checkerList.map(ch => ch.id) || [] : []
      }
      this.dialogVisible = true
    },
    deleteItem (data) {
      this.$confirm('确定删除子项吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSubProject(data.id).then(res => {
          this.$message.success('操作成功')
          this.getList()
        })
      }).catch(() => {})
    },
    confirmOptItem () {
      const opt = this.currentItem.id ? updateSubProject : addSubProject
      opt(this.currentItem).then(res => {
        this.$message.success('操作成功')
        this.dialogVisible = false
        this.getList()
      })
    },
    getNameList (list, key = 'name') {
      if (!list) return ''
      return list.map(item => item[key]).join(',')
    }
  }
}
</script>

<style scoped>

</style>
