<template>
  <div class="market-order-base-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="20">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-select
            v-model="queryForm.mo_status"
            :placeholder="__('选择状态')"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>

          <el-form-item>
            <el-input
              v-model.trim="queryForm.mo_id"
              clearable
              :placeholder="__('请输入订单')"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.mo_item_desc"
              clearable
              :placeholder="__('订单标题')"
            />
          </el-form-item>
          <el-form-item>
            <div class="block">
              <el-date-picker
                v-model="queryForm.mo_receive_date"
                align="right"
                :picker-options="pickerOptions"
                :placeholder="__('选择日期')"
                type="date"
              />
            </div>
          </el-form-item>

          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="4">
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('查看详细') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('商品图片')"
        prop="mo_item_images"
        show-overflow-tooltip
        width="150px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.mo_item_images"
            :preview-src-list="[scope.row.mo_item_images]"
            :src="scope.row.mo_item_images"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('订单编号')"
        prop="mo_id"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('配送方式')"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <span v-if="row.delivery_type_id === 5">{{ __('自提') }}</span>
          <span v-else-if="row.delivery_type_id === 10">{{ __('配送') }}</span>
          <span v-else>
            {{ __('配送') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('配送时间')"
        prop="mo_receive_date"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('配送地址')"
        prop="mo_receive_area"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatStatus"
        :label="__('是否确认接单')"
        prop="mo_confirm_status"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('发布时间')"
        prop="mo_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('接单费用')"
        prop="mo_amount"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('离接单结束时间')"
        prop="mo_receive_etime"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('是否平台转单')"
        prop="old_store_id"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        :label="__('是否店铺转单')"
        prop="store_id"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        :label="__('备注')"
        prop="mo_receive_remark"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('来源订单号')"
        prop="order_id"
        show-overflow-tooltip
        width="120"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList } from '@/api/market/order/base'
  import Edit from './components/MarketOrderBaseEdit'

  export default {
    name: 'MarketOrderBase',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now()
          },
          shortcuts: [
            {
              text: this.__('今天'),
              onClick(picker) {
                picker.$emit('pick', new Date())
              },
            },
            {
              text: this.__('昨天'),
              onClick(picker) {
                const date = new Date()
                date.setTime(date.getTime() - 3600 * 1000 * 24)
                picker.$emit('pick', date)
              },
            },
            {
              text: this.__('一周前'),
              onClick(picker) {
                const date = new Date()
                date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
                picker.$emit('pick', date)
              },
            },
          ],
        },
        value1: '',
        value2: '',
        options: [
          {
            value: 2,
            label: this.__('店铺已接单'),
          },
          {
            value: 3,
            label: this.__('骑手已结单'),
          },
          {
            value: 4,
            label: this.__('配送完成'),
          },
        ],
        value: '',
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatStatus(row) {
        return row.mo_confirm_status === 0
          ? this.__('未确认')
          : row.mo_confirm_status === 1
          ? this.__('店铺确认接单')
          : this.__('用户确认接单')
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true

        console.log(this.queryForm)
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.tota = data.records
        this.listLoading = false
      },
    },
  }
</script>
