<template>
  <div class="print-manage">
    <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"></el-input>
      </el-form-item>

      <el-form-item
        label="内容"
        prop="content">
        <el-input v-model="form.content"></el-input>
      </el-form-item>

      <div class="btn-wrap">
        <el-button
          @click="reset"
        >
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getTicketPrintModelList"
        >
          搜索
        </el-button>
        <el-button
          type="primary"
          size="mini"
          @click="$refs.printEditor.show('new')"
        >
          新增模板
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <div class="block-title">
        打印模板列表
      </div>
      <el-table
        border
        stripe
        v-loading="loading"
        :data="printTemplateList"
      >
        <el-table-column
          prop="name"
          label="模板名"
        >
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="是否默认"
        >
          <template slot-scope="scope">
            {{ scope.row.isDefault === 1 ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="240"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              v-if="scope.row.isDefault !== 1"
              @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"
              @click="delTemplate(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

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

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

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

        addTicketPrintModel(copy).then(res => {
          if (res.code === '200') {
            this.$message.success('创建成功')
          } else {
            this.$message.error(res.msg || '创建失败')
          }

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

<style lang="scss" scoped>

</style>
