<template>
  <a-card :bordered="false" class="network-table-page-search-wrapper">
    <div class="network-searchForm">
      <a-form layout="inline" :form="form" ref="form" v-bind="formItemLayout" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="48">
          <a-col :span="6">
            <a-form-item label="兑换规则" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-input id="boxSetting-name" allowClear :maxLength="30" v-model="queryParam.name" placeholder="请输入兑换规则名称" />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-button class="handle-btn serach-btn" id="boxSetting-btn-serach" type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button class="handle-btn" type="" id="boxSetting-btn-reset" @click="handleReset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="openModal" id="boxSetting-add" v-hasPermission="'B_exchangeSetting_add'">新增</a-button>
    </div>
    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      bordered>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-icon
          id="boxSetting-edit"
          type="edit"
          title="编辑"
          class="actionBtn icon-blues"
          @click="handleEdit(record)"
          v-hasPermission="'B_exchangeSetting_edit'"/>
        <a-icon
          id="boxSetting-del"
          type="delete"
          title="删除"
          class="actionBtn icon-red"
          v-if="$hasPermissions('B_exchangeSetting_del') && !record.isEnable"
          @click="delect(record)" />
        <span v-if="!$hasPermissions('B_exchangeSetting_edit') && (!$hasPermissions('B_exchangeSetting_del') && !record.isEnable)">--</span>
      </template>
    </s-table>
    <!-- 新增/编辑 弹窗 -->
    <add-set-modal :visible="openBoxModal" :goldExRuleNo="itemGoldExRuleNo" :id="itemId" @refresh="refreshTable" @close="openBoxModal = false"></add-set-modal>
  </a-card>
</template>

<script>
import {
  STable,
  VSelect
} from '@/components'
import AddSetModal from './AddSetModal.vue'
import {
  getList,
  deleteExchangeSetting
} from '@/api/exchangeSetting.js'
export default {
  name: 'Equipment',
  components: {
    STable,
    VSelect,
    AddSetModal
  },
  data () {
    return {
      form: this.$form.createForm(this),
      formItemLayout: {
        labelCol: {
          span: 7
        },
        wrapperCol: {
          span: 17
        }
      },
      // 查询参数
      queryParam: {
        name: '' // 箱体类型
      },
      openBoxModal: false, // 新增/编辑弹窗
      itemId: '', //  新增/编辑id
	  itemGoldExRuleNo: '', // 兑换编码
      // 表头
      columns: [{
        title: '序号',
        dataIndex: 'no',
        width: 80,
        align: 'center'
      },
      {
        title: '创建时间',
        dataIndex: 'createDate',
        width: 200,
        align: 'center'
      },
      {
        title: '兑换规则名称',
        dataIndex: 'name',
        width: 200,
        align: 'center'
      },
      {
        title: '兑换规则说明',
        dataIndex: 'remarks',
        width: 200,
        align: 'center'
      },
      {
        title: '兑换规则(投放重量(g)/可兑换乐豆数(个))',
        children: [{
          title: '废旧衣物',
          dataIndex: 'clothes',
          width: 100,
          align: 'center',
          customRender: (record, text) => {
            return record ? (record.rubbishWeight ? (record.rubbishWeight + 'g/' + record.goleNum + '个') : '不兑换') : '不兑换'
          }
        },
        {
          title: '废旧塑料',
          dataIndex: 'plastic',
          width: 100,
          align: 'center',
          customRender: (record, text) => {
            return record ? (record.rubbishWeight ? (record.rubbishWeight + 'g/' + record.goleNum + '个') : '不兑换') : '不兑换'
          }
        },
        {
          title: '废旧金属',
          dataIndex: 'metal',
          width: 100,
          align: 'center',
          customRender: (record, text) => {
            return record ? (record.rubbishWeight ? (record.rubbishWeight + 'g/' + record.goleNum + '个') : '不兑换') : '不兑换'
          }
        },
        {
          title: '废旧纸张',
          dataIndex: 'paper',
          width: 100,
          align: 'center',
          customRender: (record, text) => {
            return record ? (record.rubbishWeight ? (record.rubbishWeight + 'g/' + record.goleNum + '个') : '不兑换') : '不兑换'
          }
        },
        {
          title: '有害垃圾',
          dataIndex: 'harm',
          width: 100,
          align: 'center',
          customRender: (record, text) => {
            return record ? (record.rubbishWeight ? (record.rubbishWeight + 'g/' + record.goleNum + '个') : '不兑换') : '不兑换'
          }
        },
        {
          title: '废旧金属/塑料',
          dataIndex: 'metPla',
          width: 100,
          align: 'center',
          customRender: (record, text) => {
            return record ? (record.rubbishWeight ? (record.rubbishWeight + 'g/' + record.goleNum + '个') : '不兑换') : '不兑换'
          }
        }
        ]
      },
      {
        title: '操作',
        scopedSlots: {
          customRender: 'action'
        },
        width: 240,
        align: 'center'
      }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getList(Object.assign(parameter, this.queryParam)).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
            }
            return res.data
          }
        })
      }

    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.queryParam.name = ''
    })
  },
  methods: {
    // 重置
    handleReset () {
      this.queryParam.name = ''
      this.$refs.table.refresh(true)
    },
    // 新增/编辑 后刷新列表
    refreshTable () {
      if (this.itemId) {
        this.$refs.table.refresh()
      } else {
        this.$refs.table.refresh(true)
      }
    },
    // 新建
    openModal () {
      this.openBoxModal = true
      this.itemId = ''
	    this.itemGoldExRuleNo = ''
    },
    // 编辑
    handleEdit (item) {
      this.openBoxModal = true
      this.itemId = item.id
	    this.itemGoldExRuleNo = item.goldExRuleNo
    },
    // 删除
    delect (row) {
      const _this = this
      this.$confirm({
        title: '警告',
        centered: true,
        content: '删除后无法恢复，确认删除?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          deleteExchangeSetting({
            id: row.id
          }).then(res => {
            console.log(res, 'res1111')
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
	.network-table-page-search-wrapper {
		.network-searchForm {
			.ant-form-inline .ant-form-item {
				width: 100%;
			}

			// 搜索框的下间距
			.ant-form-item {
				margin-bottom: 10px;
			}

			.handle-btn {
				margin-top: 4px;
			}

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

		.actionBtn {
			font-size: 20px;
			padding: 0 10px;
		}

		// 合计
		.total {
			margin: 15px 0 25px;
			width: 100%;
			background-color: #e6f7ff;
			border: 1px solid #91d5ff;
			padding: 8px 15px 8px 27px;
			border-radius: 4px;

			.iconImg {
				color: #1890FF;
				margin-right: 10px;
			}
		}
	}
</style>
