<template>
  <a-card class="electronicScale-container" :bordered="false">
    <!-- 搜索框 -->
    <div class="electronicScale-container-searchForm">
      <a-form-model :model="searchForm" layout="inline" ref="searchForm" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="24">
          <a-col :xs="24" :sm="12" :md="8" :lg="6">
            <a-form-model-item label="设备MAC码" :label-col="{span:7}" :wrapper-col="{span:17}">
              <a-input v-model="searchForm.macAddress" placeholder="请输入设备MAC码" :maxLength="12" allowClear/>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12" :md="8" :lg="6">
            <a-button class="handle-btn serach-btn" type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button class="handle-btn" type="" @click="handleReset">重置</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <!-- 新增 -->
    <div class="electronicScale-container-add">
      <a-button type="primary" icon="plus" @click="openModal">新增</a-button>
    </div>
    <!-- 合计 -->
    <a-alert type="info" showIcon style="margin-bottom:15px">
      <div class="ftext" slot="message">总计<span> {{ totalNum }} 个</span></div>
    </a-alert>
    <!-- 列表 -->
    <s-table ref="table" :rowKey="(record) => record.id" :columns="columns" :data="loadData" bordered>
      <span slot="action" slot-scope="text,record">
        <a-icon type="edit" title="编辑" class="actionBtn icon-blues" @click="handleEdit(record)"/>
        <a-icon type="delete" title="删除" class="actionBtn icon-red" @click="delect(record)"/>
      </span>
    </s-table>
    <!-- 新增/编辑弹窗 -->
    <addElectronicScale :visible="isOpenModal" :id="itemId" :deviceCode="itemDeviceCode" @refresh="refreshTable" @close="isOpenModal=false"></addElectronicScale>
  </a-card>
</template>

<script>
import { STable } from '@/components'
import addElectronicScale from './addElectronicScale.vue'
import { getDeviceList, delectDevice } from '@/api/electronicScale.js'
export default {
  components: { STable, addElectronicScale },
  data () {
    return {
      searchForm: {
        macAddress: ''
      },
      isOpenModal: false, // 默认弹窗关闭
      itemDeviceCode: '', // 编辑行设备编号
      itemId: '',
	  totalNum: '',
      // 列表表头
      columns: [{ title: '序号', dataIndex: 'no', width: 60, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 100, align: 'center' },
        { title: '设备MAC码', dataIndex: 'macAddress', width: 100, align: 'center' },
        { title: '设备名称', dataIndex: 'deviceName', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '设备编号', dataIndex: 'deviceCode', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '设备型号', dataIndex: 'deviceModel', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '生产厂家', dataIndex: 'manufacturer', width: 100, align: 'center', customRender: (text) => { return text || '--' } },
        { title: '操作', dataIndex: 'action', width: 100, align: 'center', scopedSlots: { customRender: 'action' } }
      ],
	  // 加载数据方法 必须为 Promise 对象
	  loadData: parameter => {
	    return getDeviceList(Object.assign(parameter, this.searchForm, { deviceType: 'STEELYARD' })).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.searchForm = {
        macAddress: ''
      }
	  this.$refs.table.refresh(true)
    },
    // 新增
    openModal () {
      this.itemId = ''
      this.itemDeviceCode = ''
      this.isOpenModal = true
    },
    // 编辑
    handleEdit (record) {
      this.itemId = record.id
      this.itemDeviceCode = record.srcDeviceCode
      this.isOpenModal = true
    },
    // 删除
    delect (record) {
      const _this = this
      _this.$confirm({
        title: '警告',
        centered: true,
        content: '删除后无法恢复，确认删除?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          delectDevice({
            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>
	.electronicScale-container {
		.electronicScale-container-searchForm {
			.ant-form-inline .ant-form-item {
				width: 100%;
			}
		}

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

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

		.electronicScale-container-add {
			margin: 15px 0;
		}
	}
</style>
