<template>
  <a-card :bordered="false" class="shopOrder-table-page-search-wrapper">
    <div class="shopOrder-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-input id="shopOrder-contactPhone" allowClear :maxLength="11" v-model="queryParam.contactPhone" placeholder="请输入用户手机" />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="乐豆余额" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <div class="input-number">
                <a-input-number
                  id="inputNumber"
                  v-model="queryParam.value1"
                  :min="1"
                  :max="10"
                  @change="onChange" />-<a-input-number
                    id="inputNumber"
                    v-model="queryParam.value2"
                    :min="1"
                    :max="10"
                    @change="onChange"
                  />
              </div>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="账户状态" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <v-select
                v-model="queryParam.orderStatus"
                ref="orderStatus"
                id="shopOrder-status"
                code="SETTLE_STATUS_ALL"
                placeholder="请选择订单状态"
                allowClear></v-select>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-button class="handle-btn serach-btn" id="shopOrder-btn-serach" type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button class="handle-btn" type="" id="shopOrder-btn-reset" @click="handleReset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-divider />
    <div class="table-operator">
      <a-button class="export-btn" id="shopOrder-btn-export" type="" :loading="loading" @click="handleExport">导出</a-button>
    </div>
    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      bordered>
      <!-- 订单状态 -->
      <template slot="orderStatus" slot-scope="text, record">
        <span>{{ record.orderStatus==1 ? '已启用' : '已禁用' }}</span>
      </template>
      <!-- 订单总额 -->
      <template slot="orderPrice" slot-scope="text, record">
        <span>{{ record.orderPrice || 0 }}乐豆</span>
      </template>
      <!--启用禁用 -->
      <span slot="state" slot-scope="text, record">
        <a-switch
          v-hasPermission="'B_goodsClass_enable'"
          checkedChildren="启用"
          unCheckedChildren="禁用"
          id="bannerSetting-changeFlagHandle"
          :checked="record.state == 1 ? true : false"
          @change="changeFlagHandle(text, record)" />
      </span>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button id="shopOrder-handleView" type="link" @click="handleUser(record)">用户详情</a-button>
        <a-button id="shopOrder-handleView" type="link" @click="handleLd(record)">乐豆明细</a-button>
      </template>
    </s-table>
  </a-card>

</template>

<script>
import { STable, VSelect } from '@/components'
import getDate from '@/libs/getDate.js'
import moment from 'moment'
export default {
  name: 'ShopOrder',
  components: { STable, VSelect },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 7 },
        wrapperCol: { span: 17 }
      },
      // 查询参数
      queryParam: {
        beginDate: null, // 开始时间
        endDate: null, // 结束时间
        contactPhone: '', //  用户手机
        orderNo: '', //  订单编号
        orderStatus: null, // 订单状态
        value1: '',
        value2: ''
      },
      loading: false, // 导出loading
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '用户手机', dataIndex: 'userMoblie', width: 200, align: 'center' },
        { title: '乐豆余额', dataIndex: 'orderDate', width: 200, align: 'center' },
        { title: '累计乐豆', dataIndex: 'orderNo', width: 200, align: 'center' },
        { title: '注册时间', scopedSlots: { customRender: 'orderStatus' }, width: 200, align: 'center' },
        { title: '最后登录时间', scopedSlots: { customRender: 'orderPrice' }, width: 200, align: 'center' },
        { title: '最后投递时间', scopedSlots: { customRender: 'orderPrice' }, width: 200, align: 'center' },
        { title: '状态', width: 100, dataIndex: 'state', align: 'center', scopedSlots: { customRender: 'state' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 150, 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 = [
            { no: 1, orderDate: '2020-10-30 09:14', userMoblie: '15709252525', orderNo: 'NO54456798', orderStatus: '1', orderPrice: '155' },
            { no: 2, orderDate: '2020-10-30 09:14', userMoblie: '15709252525', orderNo: 'NO54456798', orderStatus: '0', orderPrice: '155' },
            { no: 3, orderDate: '2020-10-30 09:14', userMoblie: '15709252525', orderNo: 'NO54456798', orderStatus: '1', orderPrice: '155' },
            { no: 4, orderDate: '2020-10-30 09:14', userMoblie: '15709252525', orderNo: 'NO54456798', orderStatus: '1', orderPrice: '155' },
            { no: 5, orderDate: '2020-10-30 09:14', userMoblie: '15709252525', orderNo: 'NO54456798', orderStatus: '0', orderPrice: '155' },
            { no: 6, orderDate: '2020-10-30 09:14', userMoblie: '15709252525', orderNo: 'NO54456798', orderStatus: '0', orderPrice: '155' }
          ]
          resolve(data)
        })
      },
      // 将默认当天时间日期回显在时间选择框中
      time: [
        moment(getDate.getToday().starttime, this.dateFormat),
        moment(getDate.getToday().endtime, this.dateFormat)
      ],
      dateFormat: 'YYYY-MM-DD'
    }
  },
  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]
      }
    },
    // 查看用户详情
    handleUser (row) {
      this.$router.push({ path: `/userInfo/userManageList_user/detail/${row.id}` })
    },
    // 查看乐豆详情
    handleLd (row) {
      this.$router.push({ path: `/userInfo/userManageList_Ld/detail/${row.id}` })
    },
    // 重置
    handleReset () {
      // 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.queryParam.contactPhone = ''
      this.queryParam.orderNo = ''
      this.queryParam.orderStatus = null
      this.$refs.table.refresh(true)
    },
    // 导出
    handleExport () {
      const params = {
        beginDate: this.queryParam.beginDate == null ? getDate.getToday().starttime : this.queryParam.beginDate,
        endDate: this.queryParam.endDate == null ? getDate.getToday().endtime : this.queryParam.endDate,
        contactPhone: this.queryParam.contactPhone,
        orderNo: this.queryParam.orderNo,
        orderStatus: this.queryParam.orderStatus
      }
      if (!params.beginDate && !params.endDate) {
        this.$message.error('请先选择需要导出的下单时间区间再进行导出！')
        return
      }
      // 判断两个时间段是否相差m个月  第二个参数指相差单位，第三个参数指是否返回浮点形式（小数）
      if (moment(params.endDate).diff(moment(params.beginDate), 'months', true) > 3) {
        this.$message.error('单次最多只能导出3个月的数据，请缩小查询区间后再进行导出！')
      }
      // this.loading = true
      // exportBundle(params).then(res => {
      //   this.loading = false
      //   this.download(res)
      // })
    },
    download (data) {
      if (!data) { return }
      const url = window.URL.createObjectURL(new Blob([data]))
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      const a = moment().format('YYYYMMDDhhmmss')
      const fname = '订单记录-' + a
      link.setAttribute('download', fname + '.xlsx')
      document.body.appendChild(link)
      link.click()
    }
  }
}
</script>
<style lang="less" scoped>
  .shopOrder-table-page-search-wrapper{
    .shopOrder-searchForm{
      .ant-form-inline .ant-form-item{
        width: 100%;
      }
      // 搜索框的下间距
      .ant-form-item {
      	margin-bottom: 10px;
      }
	  .input-number{
		  display: flex;
		  margin-top:3px;
	  }
      .handle-btn{
        margin-top: 4px;
      }
      .serach-btn{
        margin-right: 10px;
      }
    }
    .export-btn{
      background-color: #ff9900;
      border-color: #ff9900;
      color: #fff;
    }
    .export-btn.ant-btn:focus:not(.ant-btn-primary):not(.ant-btn-danger), .export-btn.ant-btn:hover:not(.ant-btn-primary):not(.ant-btn-danger){
      color: #fff;
      border-color: #ff9900;
    }
    .blue {
    	color: #1890FF;
    }
    .green {
    	color: #16b50e;
    }
    .red {
    	color: red;
    }
  }
</style>
