
<template>
  <!-- 线下出货列表 -->
  <page-header-wrapper>
    <template v-slot:content>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="订单号码">
                <a-input v-model="queryParam.item_id" placeholder="订单号" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="手机号码">
                <a-input v-model="queryParam.order_no" placeholder="购买人手机号" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="真实姓名">
                <a-input v-model="queryParam.item_id" placeholder="购买人真实姓名" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="发货数量" has-feedback>
                <a-input v-model="queryParam.order_no" placeholder="发货数量" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="快递类型" has-feedback>
                <a-select v-model="queryParam.business_type_id" @change="onBusinessChange" placeholder="快递类型">
                  <a-select-option v-for="item in businessArray" :key="item.id" :value="item.id">{{ item.title }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="快递单号">
                <a-input v-model="queryParam.item_id" placeholder="快递单号" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-button type="primary" @click="search"><a-icon type="search" />搜索</a-button>
              <a-button class="ml8" t ype="primary" @click="onAddRole"><a-icon type="plus" />新增线下出货</a-button>
              <a-button class="ml8" @click="resetSearch"><a-icon type="undo" />重置</a-button>
              <a-button
                v-if="exports_can"
                class="ml8"
                type="dashed"
                shape="round"
                icon="download"
                @click="exports"/>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </template>

    <!-- 列表 -->
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :rowKey="(row) => row.order_item_id"
        showPagination="auto"
        bordered
        size="default"
        :scroll="{ x: 1200, y: 1000 }">
        <!-- 申请用户 实习顾问 法务老师 -->
        <div slot="user" slot-scope="text, record" style="color: #1890ff">
          <div>{{ record.user }}</div>
        </div>
        <!-- 创建时间 -->
        <span slot="created_at" slot-scope="text, record">{{ record.created_at }}</span>
        <!-- 操作 -->
        <div slot="actions" slot-scope="text, record">
          <a class="ant-dropdown-link" @click="onInfo(record)">查看详情</a>
          <!-- <a-divider type="vertical" /> -->
          <!-- <a class="ant-dropdown-link" @click="onEdit(record)">发货</a> -->
        </div>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable, UserInfo } from '@/components'
import DeriveButton from '@/components/OrderDetailInfo/DeriveButton'
import { institutionsExports } from '@/api/storage'
import moment from 'moment'
import Info from '@/views/home/components/Info'
import {
  init,
  canExport
} from '@/api/organization'
import { getMiddlesExperience } from '@/api/stock'
import storage from 'store'
import EditSend from './widgets/EditSend.vue'
import StockInfo from './widgets/StockInfo.vue'
import AddOffline from './widgets/AddOffline.vue'

// 列表规格
const columns = [
{
    title: '订单号',
    dataIndex: 'user22',
    align: 'center'
  },
  {
    title: '收货人',
    dataIndex: 'created_at',
    align: 'center'
  },
  {
    title: '发货数量',
    dataIndex: 'created_at1',
    align: 'center'
  },
  {
    title: '订单信息',
    children: [
      {
        title: '状态',
        dataIndex: 'name',
        key: 'name',
        align: 'center'
      },
      {
        title: '金额',
        dataIndex: 'name1',
        key: 'name1',
        align: 'center'
      }
    ],
    width: 300,
    align: 'center'
  },
  {
    title: '操作',
    scopedSlots: { customRender: 'actions' },
    align: 'center',
    width: 160,
    fixed: 'right'
  }
]

export default {
  name: 'Online',
  components: {
    STable,
    UserInfo,
    Info,
    DeriveButton
  },
  data () {
    return {
      exports_can: false,
      columns,
      searchType: false, // 展示全部的搜索
      businessArray: [], // 业务类型
      statusArray: [], // 机构列表

      queryParam: {
        order_no: '', // 订单编号
        institution: '', // 机构名称
        item_id: '', // 机构编号
        nickname: '', // 下单人
        business_type_id: '', // 业务类型
        status: '', // 机构状态
        paid_at: undefined, // 下单时间
        audit_at: undefined, // 执行时间
        settle_at: undefined // 结案时间
      },
      listCount: 0,
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getMiddlesExperience(requestParameters)
      }
    }
  },
  watch: {
    $route (to, from) {
      if (
        (from.name === 'Detail' && to.name === 'FristCheckrderIndex') ||
        (from.name === 'Home' && to.name === 'FristCheckrderIndex') ||
        (from.name === 'AssignrderIndex' && to.name === 'FristCheckrderIndex') ||
        (from.name === 'SignInrderIndex' && to.name === 'FristCheckrderIndex') ||
        (from.name === 'ReCheckrderIndex' && to.name === 'FristCheckrderIndex') ||
        (from.name === 'HandleService' && to.name === 'FristCheckrderIndex') ||
        (from.name === 'CompleteOrder' && to.name === 'FristCheckrderIndex')
      ) {
        this.$refs.table.loadData()
      }
    }
  },
  created () {
    this.init()
  },
  methods: {
    moment,
    // 获取检索的基本样式
    init () {
      init().then(res => {
        this.statusArray = res.status // 机构状态
        this.businessArray = res.type // 业务类型
        // this.canExports()
      })
    },
    // 更改创建时间
    onCreateChange (dates, dateStrings) {
      this.queryParam.paid_at = dateStrings
    },
    // 更改执行时间
    onCarryChange (dates, dateStrings) {
      this.queryParam.audit_at = dateStrings
    },
    // 更改结案时间
    onSettleChange (dates, dateStrings) {
      this.queryParam.settle_at = dateStrings
    },
    // 处理状态
    onDisposeChange (e) {
      this.queryParam.dispose = e
    },
    // 业务类型
    onBusinessChange (e) {
      this.queryParam.business_type_id = e
    },
    // 机构状态
    onOrganizationChange (e) {
      this.queryParam.status = e
    },
    // 搜索列表
    search () {
      this.$refs.table.refresh(true) // 重置接口且刷先到第一页
    },
    // 重置搜索列表
    resetSearch () {
      this.queryParam = {
        order_no: '', // 订单编号
        institution: '', // 机构名称
        item_id: '', // 机构编号
        nickname: '', // 下单人
        business_type_id: '', // 业务类型
        status: '', // 机构状态
        paid_at: undefined, // 下单时间
        audit_at: undefined, // 执行时间
        settle_at: undefined// 结案时间
      }
      this.$refs.table.refresh(true) // 重置接口且刷先到第一页
    },
    // 机构是否能导出
    canExports () {
      canExport(storage.get('userid')).then(res => {
        this.exports_can = res.exports
      })
    },
    // 导出列表
    exports () {
      const params = this.queryParam
      params.audit_at = this.queryParam.audit_at || ''
      params.paid_at = this.queryParam.paid_at || ''
      params.settle_at = this.queryParam.settle_at || ''
      if (this.listCount > 0) {
        let downUrl = institutionsExports() + '?user_id=' + storage.get('userid')
        for (const key in params) {
          downUrl = downUrl + '&' + key + '=' + params[key]
        }
        window.open(downUrl)
      } else {
        this.$notification.error({
          message: '温馨提醒',
          description: '数据为空不能下载！'
        })
      }
    },
    // 新增用户
    onAddRole () {
      const success = {
        title: '新增线下出货',
        width: 600,
        maskClosable: true,
        closable: true,
        okText: '确认新增',
        cancelText: '取消'
      }
      this.$dialog(
        AddOffline,
        {
          on: {
            ok: () => {
              this.$refs.table.loadData()
            }
          }
        },
        success
      )
    },
    // 查看详情
    onInfo (item) {
      const success = {
        title: '立即发货',
        width: 1000,
        maskClosable: true,
        closable: true,
        footer: '',
        cancelText: '取消'
      }
      this.$dialog(
        StockInfo,
        {
          item,
          on: {
            ok: () => {
              this.$refs.table.loadData()
            }
          }
        },
        success
      )
    },
    // 发货
    onEdit (item) {
      const success = {
        title: '立即发货',
        width: 600,
        maskClosable: true,
        closable: true,
        okText: '确认发货',
        cancelText: '取消'
      }
      this.$dialog(
        EditSend,
        {
          item,
          on: {
            ok: () => {
              this.$refs.table.loadData()
            }
          }
        },
        success
      )
    }
  }
}
</script>

<style lang="less" scoped>
.ml8 {
  margin-left: 8px;
}
</style>
