<template>
  <div>
    <en-table-layout :tableData="tableData.data" :loading="loading">
      <div slot="toolbar" class="inner-toolbar">
        <div class="toolbar-btns">
          <el-button size="mini" type="primary" icon="el-icon-circle-plus-outline"
            v-if="checkPermission(['goodsLabel:add'])" @click="handleAdd">添加</el-button>
        </div>
        <div class="toolbar-search">
          <en-table-search @search="searchEvent" placeholder="请输入标签名称" />
        </div>
      </div>

      <template slot="table-columns">
        <el-table-column prop="name" label="标签名称" />
        <el-table-column label="创建时间">
          <template slot-scope="scope">{{ scope.row.create_time | unixToDate }}</template>
        </el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" v-if="checkPermission(['goodsLabel:bindGoods'])"
              @click="handleBind(scope.row)">绑定商品</el-button>
            <el-button size="mini" v-if="checkPermission(['goodsLabel:edit'])"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger"
              v-if="checkPermission(['goodsLabel:delete'])"
              @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </template>

      <el-pagination v-if="tableData" slot="pagination" @size-change="handlePageSizeChange"
        @current-change="handlePageCurrentChange" :current-page="tableData.page_no" :page-sizes="[10, 20, 50, 100]"
        :page-size="tableData.page_size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.data_total">
      </el-pagination>
    </en-table-layout>
    <el-dialog :title="collectingAccountForm.id ? '编辑标签' : '添加标签'" :visible.sync="dialogVisible" width="500px"
      @open="handleDialogOpen" :modal-append-to-body="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="collectingAccountForm" :rules="formRules" ref="collectingAccountForm" label-width="80px">
        <el-form-item label="标签名称" prop="name">
          <el-input v-model="collectingAccountForm.name" :maxlength="20" clearable placeholder="请输入标签名称"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitCollectingAccountForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as API_GoodsTag from '@/api/goodsTag'
export default {
  name: 'tagManage',
  data() {
    return {
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 列表数据
      tableData: '',
      // 添加/编辑表单
      collectingAccountForm: {},
      advancedForm: {

      },
      // 添加|编辑 表单规则
      formRules: {
        name: [
          this.MixinRequired('支付方式名称必填！'),
          { min: 2, max: 30, message: '长度在 2 到 30 个字符' }
        ]
      },
      // 是否显示 添加|编辑 表单
      dialogVisible: false
    }
  },
  watch: {
  },
  mounted() {
    this.GET_CollectingAccountList()
  },
  methods: {
    /** 高级搜索事件触发 */
    advancedSearchEvent() {
      this.params = {
        ...this.params,
        ...this.advancedForm
      }
      this.params.page_no = 1
      this.GET_CollectingAccountList()
    },
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_CollectingAccountList()
    },

    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.GET_CollectingAccountList()
    },

    /** 添加弹窗 */
    handleAdd() {
      this.collectingAccountForm = {
        default_flag: true,
        enable_flag: true
      }
      this.dialogVisible = true
    },

    /** 编辑弹窗 */
    handleEdit(row) {
      this.collectingAccountForm = this.MixinClone(row)
      this.dialogVisible = true
    },

    /** 绑定商品 */
    handleBind(row) {
      this.$router.push({ path: `/goods/tag-add/${row.id}`, query: { tag_name: row.name }})
    },
    /** 删除 */
    handleDelete(id) {
      this.$confirm('确定要删除吗？', '提示', { type: 'warning' }).then(() => {
        API_GoodsTag.del(id).then(() => {
          this.$message.success('删除成功！')
          this.GET_CollectingAccountList()
        })
      }).catch(() => { })
    },
    /** dialog打开后重置form表单校验结果 */
    handleDialogOpen() {
      setTimeout(() => { this.$refs['collectingAccountForm'].clearValidate() })
    },

    /** 提交 添加|编辑 表单 */
    submitCollectingAccountForm() {
      this.$refs['collectingAccountForm'].validate((valid) => {
        if (valid) {
          const { id } = this.collectingAccountForm
          const data = this.MixinClone(this.collectingAccountForm)
          if (id) {
            API_GoodsTag.edit(id, data).then(response => {
              this.dialogVisible = false
              this.$message.success('修改成功！')
              this.GET_CollectingAccountList()
            })
          } else {
            API_GoodsTag.add(data).then(() => {
              this.dialogVisible = false
              this.$message.success('添加成功！')
              this.GET_CollectingAccountList()
            })
          }
        } else {
          return false
        }
      })
    },

    /** 搜索事件触发 */
    searchEvent(data) {
      this.params = {
        ...this.params,
        name: data
      }
      this.params.page_no = 1
      this.GET_CollectingAccountList()
    },

    /** 查询分页列表 */
    GET_CollectingAccountList() {
      this.loading = true
      API_GoodsTag.getTagsList(this.params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
.face-image {
  display: block;
  width: 50px;
  height: 50px;
  margin: 0 auto;
}

.birthday{
  width: 94%;
}

/deep/ .el-dialog__body {
  padding: 10px 20px;
}
</style>
