<template>
  <a-card :bordered="false" class="network-table-page-search-wrapper">
    <div class="network-searchForm">
      <a-form layout="inline" 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-range-picker
                id="network-time"
                v-model="time"
                :format="dateFormat"
                :placeholder="['开始时间','结束时间']"
                :disabledDate="disabledDate"
                @change="onChange" />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="网点名称" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-input id="network-name" allowClear :maxLength="30" v-model="queryParam.name" placeholder="请输入网点名称" />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="区域" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-cascader
                id="network-areas"
                v-model="areas"
                :options="options"
                placeholder="请选择省市区"
                @change="addrChange"/>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-button class="handle-btn serach-btn" id="network-btn-serach" type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button class="handle-btn" type="" id="network-btn-reset" @click="handleReset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-divider />
    <div class="table-operator">
      <a-button class="add-btn" id="network-btn-add" type="primary" @click="handleEdit">新增网点</a-button>
    </div>
    <!-- 合计 -->
    <div class="total">
      <a-icon type="info-circle" class="iconImg" />
      <strong>总计：</strong><span v-model="orderTotal">{{ orderTotal || 0 }} 条</span>
    </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 type="eye" id="network-handleView" title="详情" class="actionBtn icon-green" @click="handleView(record)" />
        <a-icon type="edit" id="network-handleEdit" title="编辑" class="actionBtn icon-blues" @click="handleEdit(record)" />
        <a-badge :count="record.devCount">
          <a-icon type="link" id="network-handleLink" title="绑定箱体设备" class="actionBtn icon-orange" @click="handleBind(record)" />
        </a-badge>
      </template>
    </s-table>
    <!-- 新增编辑 -->
    <edit-network-modal :openModal="openNetworkModal" :networkId="networkId" @success="handleReset" @close="openNetworkModal=false" />
    <!-- 详情 -->
    <details-network-modal :openModal="openDetailModal" :networkId="networkId" :networkNo="networkNo" @close="openDetailModal=false" />
    <!-- 绑定箱体设备 -->
    <equipment-modal :openModal="openBindModal" :networkId="networkId" :networkNo="networkNo" @close="openBindModal=false" />
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import addrData from '@/api/address'
import moment from 'moment'
import editNetworkModal from './editModal.vue'
import detailsNetworkModal from './detailsModal.vue'
import equipmentModal from './equipmentModal.vue'
import { stationList } from '@/api/station'
export default {
  name: 'Network',
  components: { STable, VSelect, editNetworkModal, detailsNetworkModal, equipmentModal },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 7 },
        wrapperCol: { span: 17 }
      },
      // 查询参数
      queryParam: {
        beginDate: null, // 开始时间
        endDate: null, // 结束时间
        name: '', //  网点名称
        provinceCode: null, //  省
        cityCode: null, // 市
        districtCode: null // 区
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 200, align: 'center' },
        { title: '网点名称', dataIndex: 'name', width: 200, align: 'center' },
        { title: '省', dataIndex: 'provinceName', width: 200, align: 'center' },
        { title: '市', dataIndex: 'cityName', width: 200, align: 'center' },
        { title: '区', dataIndex: 'districtName', width: 200, align: 'center' },
        { title: '详细地址', dataIndex: 'address', width: 200, align: 'center' },
        { title: '兑换规则', dataIndex: 'exRuleName', width: 200, align: 'center' },
        { title: '投放时间段名称', dataIndex: 'timeRuleName', width: 220, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 240, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return stationList(Object.assign(parameter, this.queryParam)).then(res => {
          if (res.status == 200) {
            const no = (res.data.pageNo - 1) * res.data.pageSize
            this.orderTotal = res.data.count
            for (let i = 0; i < res.data.list.length; i++) {
              const _item = res.data.list[i]
              _item.no = no + i + 1
            }
            return res.data
          }
        })
      },
      // 将默认当天时间日期回显在时间选择框中
      time: [],
      dateFormat: 'YYYY-MM-DD',
      options: addrData, //  省市区下拉数据
      areas: [], //  省市区 值
      orderTotal: '', // 合计开单数量
      openNetworkModal: false, //  新增编辑网点信息
      networkId: null, //  网点id
      networkNo: null, //  网点编号
      openDetailModal: false, //  网点信息详情
      openBindModal: false //  绑定箱体设备
    }
  },
  methods: {
    moment,
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    // 创建时间change
    onChange (dates, dateStrings) {
      if ((dates, dateStrings)) {
        this.queryParam.beginDate = dateStrings[0]
        this.queryParam.endDate = dateStrings[1]
      }
    },
    //  省市区  change
    addrChange (val) {
      this.queryParam.provinceCode = val[0]
      this.queryParam.cityCode = val[1]
      this.queryParam.districtCode = val[2]
    },
    // 重置
    handleReset () {
      this.queryParam.beginDate = null
      this.queryParam.endDate = null
      this.queryParam.name = ''
      this.queryParam.provinceCode = null
      this.queryParam.cityCode = null
      this.queryParam.districtCode = null
      this.time = []
      this.areas = []
      this.$refs.table.refresh(true)
      this.networkId = null
      this.networkNo = null
    },
    //  新增、编辑网点
    handleEdit (row) {
      this.networkId = row ? row.id : null
      this.networkNo = row ? row.stationNo : null
      this.openNetworkModal = true
    },
    //  查看详情
    handleView (row) {
      this.networkId = row ? row.id : null
      this.networkNo = row ? row.stationNo : null
      this.openDetailModal = true
    },
    //  关联箱体设备
    handleBind (row) {
      this.networkId = row ? row.id : null
      this.networkNo = row ? row.stationNo : null
      this.openBindModal = true
    }
  }
}
</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>
