<template>
  <div :class="$style.container">
    <SearchBar
      :placeholder="$t('module.packingManage.labelTemplate.placeholderSearch')"
      :input-width="200"
      @on-search="handleSearch"
    >
      <el-button type="success" icon="el-icon-plus" @click="handleAdd()">
        {{ "btn.add" | i18nFilter }}
      </el-button>
    </SearchBar>
    <el-table
      :data="tableDataList"
      :class="$style.table"
      :stripe="true"
      :header-cell-style="{background:'#eef1f6',color:'#606266',height: '56px'}"
      :cell-style="{ textAlign: 'center' }"
      size="medium"
    >
      <el-table-column prop="index" :label="'tableHeader.index' | i18nFilter" width="80" fixed="left" align="center" />
      <el-table-column prop="createTime" :label="$t('module.packingManage.labelTemplate.addTime')" width="180" align="center">
        <template slot-scope="scope">
          {{ moment(scope.row.handoverTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>
      <el-table-column prop="code" :label="$t('module.packingManage.labelTemplate.labelCode')" width="180" align="center" />
      <el-table-column prop="name" :label="$t('module.packingManage.labelTemplate.labelName')" width="180" align="center" />
      <el-table-column prop="type" :label="$t('module.packingManage.labelTemplate.labelType')" width="180" align="center">
        <template slot-scope="scope">
          {{ printModelTypeList[scope.row.type] }}
        </template>
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="content"
        :label="$t('module.packingManage.labelTemplate.labelContent')"
        width="180"
        align="center"
      />
      <el-table-column prop="remark" align="center" :label="$t('module.packingManage.labelTemplate.remark')" />
      <el-table-column :label="'tableHeader.operation' | i18nFilter" align="center" width="320" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >
            {{ "btn.view" | i18nFilter }}
          </el-button>
          <span style="margin:0 3px">|</span>
          <el-button
            size="mini"
            type="info"
            icon="el-icon-edit"
            @click="handleEdit(scope.row.id)"
          >
            {{ "btn.edit" | i18nFilter }}
          </el-button>
          <span style="margin:0 3px">|</span>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.id)"
          >
            {{ "btn.delete" | i18nFilter }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :hide-on-single-page="false"
      :class="$style.table"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next"
      :total="pageTotal"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>
import moment from 'moment'
import { page, del } from '@/api/packing-manage/packing-label.js'
import SearchBar from '@/views/art/components/search-bar'
import i18n from '@/lang'
import { getLodop } from '@/assets/libs/LodopFuncs.js'

const printModelTypeList = {
  0: i18n.t('module.packingManage.labelTemplate.levelLabel'),
  1: i18n.t('module.packingManage.labelTemplate.moduleLabel'),
  2: i18n.t('module.packingManage.labelTemplate.custom')
}

export default {
  components: { SearchBar },
  props: {},
  data() {
    return {
      moment,
      tableDataList: [],
      pageTotal: 0,
      page: {
        current: 1,
        size: 10
      },
      param: {},
      printModelTypeList
    }
  },
  created() {
    this.handleSearch()
  },
  methods: {
    handleSearch(param) {
      this.param = param
      console.log(param)
      page({ ...param, key: param && param.keywords, ...this.page }).then(
        res => {
          if (!res.data) {
            return
          }
          this.pageTotal = res.data && res.data.total
          console.log(this.pageTotal)
          if (!res.data.records) {
            this.tableDataList = []
            return
          }
          this.tableDataList = res.data.records.map((m, index) => ({
            ...m,
            index: this.page.size * (this.page.current - 1) + index + 1
          }))
        }
      )
    },
    handleAdd() {
      this.$router.push({
        path: '/packing/label-design-add',
        query: {
          redirect: '/packing/label-template',
          title: '标签设计'
        }
      })
    },
    handleEdit(id) {
      this.$router.push({
        path: '/packing/label-design-add',
        query: {
          redirect: '/packing/label-template',
          title: '标签设计',
          id
        }
      })
    },
    handleView(printModel) {
      const LODOP = getLodop()
      const modelCode = printModel.content
      LODOP.ADD_PRINT_DATA('ProgramData', modelCode) // 装载模板
      // 按类名赋值
      LODOP.SET_PRINT_STYLEA('name', 'CONTENT', '张三')
      LODOP.SET_PRINT_STYLEA('code', 'CONTENT', '123455')
      LODOP.PREVIEW()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page.size = val
      this.handleSearch(this.param)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page.current = val
      this.handleSearch(this.param)
    },
    handleDelete(id) {
      this.$confirm(
        i18n.t('deleteMsgBox.content'),
        i18n.t('deleteMsgBox.hint'),
        {
          confirmButtonText: i18n.t('btn.confirm'),
          cancelButtonText: i18n.t('btn.cancel'),
          type: 'warning'
        }
      ).then(async() => {
        del({ id }).then(res => {
          this.page.current = 1
          this.handleSearch(this.param)
          this.$message({
            type: 'info',
            message: i18n.t('deleteMsgBox.doneMsg')
          })
        })
      })
    }
  }
}
</script>
<style lang="scss" module>
.container {
  .table {
    margin: 16px;
    width: 98.5%;
  }
}
</style>
