<template>
  <div class="tableBox">
    <div
      class="btn-wrap-left"
      style="padding:10px 24px">
      <el-button
        type="primary"
        size="mini"
        @click="$refs.printEditor.show('new')"
      >
        新增模板
      </el-button>
    </div>

    <div class="block-title">
      模板列表
    </div>
    <el-table
      border
      stripe
      v-loading="loading"
      :data="printTemplateList"
    >
      <el-table-column
        prop="id"
        label="ID"
        width="60"
      >
      </el-table-column>

      <el-table-column
        prop="name"
        label="模板名"
      >
        <template slot-scope="scope">
          {{ scope.row.name }} {{ currentTemplate===scope.row.id?'【当前模板】':'' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="ticket_type_list"
        label="绑定票种"
      >
        <template slot-scope="scope">
          <el-tag
            v-for="(item,index) in scope.row.ticket_type_list"
            :key="index"
          >
            {{ item.name }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        fixed="right"
        label="操作"
        width="240"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            v-if="currentTemplate!==scope.row.id"
            @click="setDefault(scope.row)"
          >
            设为默认
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="handleCopy(scope.row)"
          >
            创建副本
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="$refs.printEditor.show('edit',scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            size="small"
            v-if="currentTemplate!==scope.row.id"
            @click="delTemplate(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <PrintEditor
      ref="printEditor"
      @update="getTicketPrintModelList"></PrintEditor>
  </div>
</template>

<script>
import PrintEditor from './printManage/index.vue'
import { addTicketPrintModel, getTicketPrintModelList, deleteTicketPrintModel } from '@/api/printModal'

export default {
  components: {
    PrintEditor
  },
  computed: {
    printTemplateList () {
      return this.$store.state.app.printTemplateList
    }
  },
  created () {
    this.currentTemplate = this.$localStore.get('defaultTemplate') || ''
  },
  data () {
    return {
      loading: false,
      tableData: [],
      currentTemplate: ''
    }
  },
  methods: {
    getTicketPrintModelList () {
      this.loading = false
      getTicketPrintModelList().then(res => {
        let id = this.currentTemplate - 0
        let target = res.list.find(i => i.id === id)
        // 设置当前打印模板
        target && this.$store.commit('SET_PRINT_TEMPLATE', JSON.parse(target.content))
        // 设置打印模板列表
        this.$store.commit('SET_PRINT_TEMPLATE_LIST', res.list)
      }).finally(() => {
        this.loading = false
      })
    },
    delTemplate (id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTicketPrintModel(id).then(res => {
          this.$message.success('删除成功')
          this.getTicketPrintModelList()
        })
      })
    },
    setDefault (item) {
      this.$localStore.set('defaultTemplate', item.id)
      this.currentTemplate = item.id
      this.$store.commit('SET_PRINT_TEMPLATE', JSON.parse(item.content))
      this.$message.success('设置成功')
    },
    handleCopy (item) {
      this.$confirm('确定创建模板副本?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const copy = JSON.parse(JSON.stringify(item))
        copy.name = copy.name + ' - 副本'

        addTicketPrintModel(copy).then(res => {
          this.$message.success('创建成功')

          this.getTicketPrintModelList()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
