<template>
  <a-card class="labelSetting-container" :bordered="false">
    <!-- 新增 -->
    <div class="labelSetting-container-add">
      <a-button type="primary" icon="plus" @click="openModal" id="labelSetting-openModal" v-hasPermission="'B_labelSetting_add'">新增</a-button>
    </div>
    <!-- 列表 -->
    <s-table ref="table" :rowKey="record => record.id" :columns="columns" :data="loadData" bordered>
      <template slot="action" slot-scope="text, record">
        <a-icon type="edit" title="编辑" class="actionBtn icon-blues" @click="handleEdit(record)" v-hasPermission="'B_labelSetting_edit'"/>
        <a-icon type="delete" title="删除" class="actionBtn icon-red" @click="delect(record)" v-hasPermission="'B_labelSetting_delete'"/>
        <span v-if="!$hasPermissions('B_labelSetting_edit') && !$hasPermissions('B_labelSetting_delete')">--</span>
      </template>
    </s-table>
    <!-- 新增/编辑弹窗 -->
    <add-label-Setting :visible="isOpenModal" :id="itemId" @refresh="refreshTable" @close="isOpenModal = false" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import addLabelSetting from './addLabelSetting.vue'
import { getLableListData, delectLable } from '@/api/labelSetting.js'
export default {
  name: 'LabelSetting',
  components: { STable, addLabelSetting },
  data () {
    return {
      isOpenModal: false, // 默认弹窗关闭
      itemId: '', // 编辑行id
      // 列表表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 60, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 100, align: 'center' },
        { title: '网点标签名称', dataIndex: 'labelName', width: 100, align: 'center' },
        { title: '备注', dataIndex: 'remarks', width: 100, align: 'center', ellipsis: true, customRender: text => { return text || '--' } },
        { title: '操作', dataIndex: 'action', width: 100, align: 'center', scopedSlots: { customRender: 'action' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getLableListData(Object.assign(parameter)).then(res => {
          if (res.status == 200) {
            const no = (res.data.pageNo - 1) * res.data.pageSize
            for (let i = 0; i < res.data.list.length; i++) {
              const _item = res.data.list[i]
              _item.no = no + i + 1
            }
            this.totalNum = res.data.count
            return res.data
          } else {
            this.totalNum = 0
          }
        })
      }
    }
  },
  methods: {
    // 重置
    handleReset () {
      this.$refs.table.refresh(true)
    },
    // 新增
    openModal () {
      console.log('--------新增')
      this.itemId = ''
      this.isOpenModal = true
    },
    // 编辑
    handleEdit (record) {
      this.itemId = record.id
      this.isOpenModal = true
    },
    // 删除
    delect (record) {
      const _this = this
      _this.$confirm({
        title: '警告',
        centered: true,
        content: '删除后无法恢复，确认删除?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          delectLable({
            id: record.id
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
          })
        }
      })
    },
    // 保存或编辑后刷新列表
    refreshTable () {
      // 编辑
      if (this.itemId) {
        this.$refs.table.refresh()
      } else {
        // 保存
        this.$refs.table.refresh(true)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.labelSetting-container {
  .labelSetting-container-searchForm {
    .ant-form-inline .ant-form-item {
      width: 100%;
    }
  }

  // 查询按钮
  .handle-btn {
    margin-top: 4px;
  }

  .serach-btn {
    margin-right: 10px;
  }

  .labelSetting-container-add {
    margin-bottom: 15px;
  }
}
 // 查看小程序码  弹框
  .qrCodeModal {
    .qrCode {
      display: block;
      max-width: 100%;
      margin: 0 auto;
    }
    .downQrCode {
      display: block;
      margin: 30px auto;
    }
  }
</style>
