<template>
  <div class="tableBox">
    <div
      class="btn-wrap-left"
      style="padding:10px 24px">
      <el-button
        type="primary"
        size="mini"
        @click="$refs.printEditor.show('add')"
      >
        新增模板
      </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
        fixed="right"
        label="操作"
        width="200"
      >
        <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="$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>

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

<script>
import EditModal from './EditModal'
import { getReceiptPrintModel, deleteReceiptPrintModel } from '@/api/printModal'

export default {
  components: {
    EditModal
  },
  computed: {
    printTemplateList () {
      return this.$store.state.app.smallTicketTemplateList
    }
  },
  created () {
    this.currentTemplate = this.$localStore.get('defaultSmallTicketTemplate') || ''
  },
  data () {
    return {
      loading: false,
      tableData: [],
      currentTemplate: ''
    }
  },
  methods: {
    getTicketPrintModelList () {
      this.loading = false
      getReceiptPrintModel().then(res => {
        let id = this.currentTemplate - 0
        let target = res.list.find(i => i.id === id)
        // 设置当前打印模板
        target && this.$store.commit('SET_SMALL_TICKET_TEMPLATE', JSON.parse(target.content))
        // 设置打印模板列表
        this.$store.commit('SET_SMALL_TICKET_TEMPLATE_LIST', res.list)
      }).finally(() => {
        this.loading = false
      })
    },
    delTemplate (id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteReceiptPrintModel(id).then(res => {
          this.$message.success('删除成功')
          this.getTicketPrintModelList()
        })
      })
    },
    setDefault (item) {
      this.$localStore.set('defaultSmallTicketTemplate', item.id)
      this.currentTemplate = item.id
      this.$store.commit('SET_SMALL_TICKET_TEMPLATE', JSON.parse(item.content))
      this.$message.success('设置成功')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
