<template>
  <div class="form-wrap">
    <el-form
      class="searchBox"
      ref="form"
      :model="form"
      :inline="true"
      label-width="140px">
      <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="tableBox">
      <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">
            {{ getAdminName(scope.row.adminIds) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="ticketTypeIds"
          label="票种">
          <template slot-scope="scope">
            {{ getTicketTypeNames(scope.row.ticketTypeIds) }}
          </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
        :page-size="10"
        layout="prev, pager, next, total"
        :current-page.sync="form.pageNum"
        @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>
      <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 } from '@/api/checker'
import { getAccountList } from '@/api/systemSetting/account'
import { getTicketTypeList } from '@/api/ticketType'
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: []
    }
  },
  computed: {
    scenicList () {
      return this.$store.state.app.scenicList
    }
  },
  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 || []
    })
  },
  components: {
    ElDialog
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    // 获取通道列表
    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.adminIds || [],
        ticketTypeIds: item.ticketTypeIds || []
      }
      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()
      })
    },
    getAdminName (ids) {
      if (!ids) return ''
      const names = this.adminList.filter(item => ids.includes(item.id)).map(item => item.loginName)
      return names.join(',')
    },
    getTicketTypeNames (ids) {
      if (!ids) return ''
      const names = this.ticketTypeList.filter(item => ids.includes(item.id)).map(item => item.name)
      return names.join(',')
    }
  }
}
</script>

<style scoped>

</style>
