<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-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-networkName" allowClear :maxLength="30" v-decorator="[ 'queryParam.networkName', { initialValue: queryParam.networkName, getValueFromEvent: $filterEmpty, rules: [] }]" 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-decorator="[
                  'options',
                  {initialValue: areas,
                   rules: []}
                ]"
                :options="options"
                placeholder="请选择省市区" />
            </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" :loading="loading" @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="5">
          <a-icon type="link" id="network-handleLink" title="关联箱体设备" class="actionBtn icon-orange" @click="handleBind(record)" />
        </a-badge>
      </template>
    </s-table>
    <!-- 新增编辑 -->
    <editNetworkModal :openModal="openNetworkModal" :networkId="networkId" @success="handleReset" @close="openNetworkModal=false" />
    <!-- 详情 -->
    <detailsNetworkModal :openModal="openDetailModal" :networkId="networkId" @close="openDetailModal=false" />
    <!-- 绑定箱体设备 -->
    <equipmentModal :openModal="openBindModal" :networkId="networkId" @close="openBindModal=false" />
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import addrData, { province, city, area } from '@/api/address'
import getDate from '@/libs/getDate.js'
import moment from 'moment'
import editNetworkModal from './editModal.vue'
import detailsNetworkModal from './detailsModal.vue'
import equipmentModal from './equipmentModal.vue'
export default {
  name: 'Network',
  components: { STable, VSelect, editNetworkModal, detailsNetworkModal, equipmentModal },
  data () {
    return {
      form: this.$form.createForm(this),
      formItemLayout: {
        labelCol: { span: 7 },
        wrapperCol: { span: 17 }
      },
      // 查询参数
      queryParam: {
        beginDate: null, // 开始时间
        endDate: null, // 结束时间
        networkName: '', //  网点名称
        province: null, //  省
        city: null, // 市
        area: null // 区
      },
      loading: false, // 导出loading
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '创建时间', dataIndex: 'orderDate', width: 200, align: 'center' },
        { title: '网点名称', dataIndex: 'networkName', width: 200, align: 'center' },
        { title: '省', dataIndex: 'provinceName', width: 200, align: 'center' },
        { title: '市', dataIndex: 'cityName', width: 200, align: 'center' },
        { title: '区', dataIndex: 'areaName', width: 200, align: 'center' },
        { title: '详细地址', dataIndex: 'address', width: 200, align: 'center' },
        { title: '投放时间段名称', dataIndex: 'times', width: 220, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 240, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // return getTenantsList(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
        //       _item.status = _item.status + '' === '1'
        //     }
        //     return res.data
        //   }
        // })
        return new Promise((resolve) => {
          const data = [
            { id: 1, no: 1, orderDate: '2020-10-30 09:14', networkName: '网点1', provinceName: '陕西省', cityName: '西安市', areaName: '未央区', address: '凤城十二路', times: '早高峰期' },
            { id: 2, no: 2, orderDate: '2020-10-30 09:14', networkName: '网点2', provinceName: '陕西省', cityName: '西安市', areaName: '未央区', address: '凤城十二路', times: '晚高峰期' },
            { id: 3, no: 3, orderDate: '2020-10-30 09:14', networkName: '网点3', provinceName: '陕西省', cityName: '西安市', areaName: '未央区', address: '凤城十二路', times: '早高峰期' },
            { id: 4, no: 4, orderDate: '2020-10-30 09:14', networkName: '网点4', provinceName: '陕西省', cityName: '西安市', areaName: '未央区', address: '凤城十二路', times: '早高峰期' },
            { id: 5, no: 5, orderDate: '2020-10-30 09:14', networkName: '网点5', provinceName: '陕西省', cityName: '西安市', areaName: '未央区', address: '凤城十二路', times: '早高峰期' },
            { id: 6, no: 6, orderDate: '2020-10-30 09:14', networkName: '网点6', provinceName: '陕西省', cityName: '西安市', areaName: '未央区', address: '凤城十二路', times: '早高峰期' },
            { id: 7, no: 7, orderDate: '2020-10-30 09:14', networkName: '网点7', provinceName: '陕西省', cityName: '西安市', areaName: '未央区', address: '凤城十二路', times: '早高峰期' }
          ]
          resolve(data)
        })
      },
      // 将默认当天时间日期回显在时间选择框中
      time: [
        moment(getDate.getToday().starttime, this.dateFormat),
        moment(getDate.getToday().endtime, this.dateFormat)
      ],
      dateFormat: 'YYYY-MM-DD',
      options: addrData, //  省市区下拉数据
      areas: [], //  省市区 值
      orderTotal: '', // 合计开单数量
      openNetworkModal: false, //  新增编辑网点信息
      networkId: null, //  网点id
      openDetailModal: false, //  网点信息详情
      openBindModal: false //  绑定箱体设备
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.handleReset()
    })
  },
  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]
      }
    },
    // 重置
    handleReset () {
      console.log('重置', this.queryParam)
      this.queryParam.beginDate = getDate.getToday().starttime
      this.queryParam.endDate = getDate.getToday().endtime
      this.time = [
        moment(getDate.getToday().starttime, this.dateFormat),
        moment(getDate.getToday().endtime, this.dateFormat)
      ]
      this.areas = []
      this.form.resetFields()
      this.$refs.table.refresh(true)
    },
    //  新增、编辑网点
    handleEdit (row) {
      this.networkId = row ? row.id : null
      this.openNetworkModal = true
    },
    //  查看详情
    handleView (row) {
      this.networkId = row ? row.id : null
      this.openDetailModal = true
    },
    //  关联箱体设备
    handleBind (row) {
      this.networkId = row ? row.id : 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>
