<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="20">
          <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="网点标签">
              <a-select
                style="max-height: 50px;overflow: auto;margin-top: 3px;"
                id="network-labelNoList"
                placeholder="请选择网点标签"
                mode="multiple"
                allowClear
                v-model="queryParam.labelNoList"
                :showSearch="true"
                option-filter-prop="children"
                :filter-option="filterOption"
              >
                <a-select-option v-for="item in lableData" :key="item.labelNo" :value="item.labelNo">{{ item.labelName }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item prop="provinceCode" label="省:">
              <a-select v-model="queryParam.provinceCode" @change="getCityList" placeholder="请选择省">
                <a-select-option v-for="item in addrProvinceList" :value="item.id" :key="item.id + 'a'">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item prop="cityCode" label="市:">
              <a-select v-model="queryParam.cityCode" @change="getAreaList" placeholder="请选择市">
                <a-select-option v-for="item in addrCityList" :value="item.id" :key="item.id + 'b'">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item prop="districtCode" label="区:">
              <a-select v-model="queryParam.districtCode" placeholder="请选择区/县">
                <a-select-option v-for="item in addrDistrictList" :value="item.id" :key="item.id + 'c'">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="20">
          <a-col :span="6">
            <a-form-item label="网点名称" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-input id="network-queryWord" allowClear :maxLength="30" v-model="queryParam.queryWord" 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>
    <div class="table-operator">
      <a-button v-if="$hasPermissions('B_network_add')" 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>
      </a-icon></div>
    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ x: 1680}"
      bordered>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-icon
          v-if="$hasPermissions('B_network_look')"
          type="eye"
          id="network-handleView"
          title="详情"
          class="actionBtn icon-green"
          @click="handleView(record)" />
        <a-icon
          v-if="$hasPermissions('B_network_edit')"
          type="edit"
          id="network-handleEdit"
          title="编辑"
          class="actionBtn icon-blues"
          @click="handleEdit(record)" />
        <a-badge v-if="$hasPermissions('B_network_bind')" :count="record.deviceNum">
          <a-icon type="link" id="network-handleLink" title="绑定箱体设备" class="actionBtn icon-orange" @click="handleBind(record)" />
        </a-badge>
        <span v-if="!$hasPermissions('B_network_look') && !$hasPermissions('B_network_edit') && !$hasPermissions('B_network_bind')">--</span>
      </template>
    </s-table>
    <!-- 新增编辑 -->
    <edit-network-modal :openModal="openNetworkModal" :networkId="networkId" @success="handleReset" @close="closeModal" />
    <!-- 详情 -->
    <details-network-modal :openModal="openDetailModal" :networkId="networkId" :networkNo="networkNo" @close="closeModal" />
    <!-- 绑定箱体设备 -->
    <equipment-modal :openModal="openBindModal" :networkId="networkId" :networkNo="networkNo" @close="closeModal" />
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import moment from 'moment'
import editNetworkModal from './editModal.vue'
import detailsNetworkModal from './detailsModal.vue'
import equipmentModal from './equipmentModal.vue'
import { stationList, getProvince, getCityByPro, getDistrictByCity } from '@/api/station'
import { lableSeleteList } from '@/api/labelSetting.js'
export default {
  name: 'Network',
  components: { STable, VSelect, editNetworkModal, detailsNetworkModal, equipmentModal },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 7 },
        wrapperCol: { span: 17 }
      },
      // 查询参数
      queryParam: {
        beginDate: null, // 开始时间
        endDate: null, // 结束时间
        queryWord: '', //  网点名称
        provinceCode: undefined, //  省
        cityCode: undefined, // 市
        districtCode: undefined, // 区
        labelNoList: []
      },
      lableData: [],
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '创建时间', dataIndex: 'createDate', width: 200, align: 'center' },
        { title: '网点名称', dataIndex: 'name', width: 200, align: 'center', customRender: text => { return text || '--' } },
        { title: '网点标签', dataIndex: 'labelName', width: 200, align: 'center', ellipsis: true, customRender: text => { return text || '--' } },
        { title: '省', dataIndex: 'provinceName', width: 100, align: 'center' },
        { title: '市', dataIndex: 'cityName', width: 100, align: 'center' },
        { title: '区', dataIndex: 'districtName', width: 100, align: 'center' },
        { title: '详细地址', dataIndex: 'address', width: 200, align: 'center', ellipsis: true },
        { title: '兑换规则', dataIndex: 'exRuleName', width: 150, align: 'center' },
        { title: '投放时间段名称', dataIndex: 'timeRuleName', width: 150, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 200, 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',
      orderTotal: '', // 合计开单数量
      openNetworkModal: false, //  新增编辑网点信息
      networkId: null, //  网点id
      networkNo: null, //  网点编号
      openDetailModal: false, //  网点信息详情
      openBindModal: false, //  绑定箱体设备
      addrProvinceList: [], //  省下拉
      addrCityList: [], //  市下拉
      addrDistrictList: [] //  区下拉
    }
  },
  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]
      }
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    // 获取标签数据
    getlableSeleteList () {
      lableSeleteList().then(res => {
        if (res.status == 200) {
          this.lableData = res.data || []
        }
      })
    },
    // 重置
    handleReset () {
      this.queryParam.beginDate = undefined
      this.queryParam.endDate = undefined
      this.queryParam.queryWord = ''
      this.queryParam.provinceCode = undefined
      this.queryParam.cityCode = undefined
      this.queryParam.districtCode = undefined
      this.time = []
      this.$refs.table.refresh(true)
      this.networkId = undefined
      this.networkNo = undefined
      this.queryParam.labelNoList = []
    },
    //  新增、编辑网点
    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
    },
    //  关闭弹框
    closeModal () {
      this.networkId = null
      this.openNetworkModal = false
      this.openDetailModal = false
      this.openBindModal = false
      this.$refs.table.refresh()
    },
    // 获取省列表'
    getProvinceList () {
      getProvince().then(res => {
        if (res.status == 200) {
          this.addrProvinceList = res.data || []
        } else {
          this.addrProvinceList = []
        }
      })
    },
    // 获取城市列表
    getCityList (val) {
      this.addrCityList = []
      this.addrDistrictList = []
      this.queryParam.cityCode = undefined
      this.queryParam.districtCode = undefined
      this.getCityListRequest(val)
    },
    getCityListRequest (val) {
      getCityByPro({
        id: val
      }).then(res => {
        if (res.status == 200) {
          this.addrCityList = res.data || []
        } else {
          this.addrCityList = []
        }
      })
    },
    // 获取区县列表
    getAreaList (val) {
      this.addrDistrictList = []
      this.queryParam.districtCode = undefined
      this.getAreaListRequest(val)
    },
    getAreaListRequest (val) {
      getDistrictByCity({
        id: val
      }).then(res => {
        if (res.status == 200) {
          this.addrDistrictList = res.data || []
        } else {
          this.addrDistrictList = []
        }
      })
    }
  },
  mounted () {
    this.getProvinceList()
    this.getlableSeleteList()
  }
}
</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>
