<template>
  <div class="order-base-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4" />
      <vab-query-form-right-panel :span="20">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select
              v-if="false"
              v-model="queryForm.order_state_id"
              clearable
              filterable
              :placeholder="__('全部状态')"
              style="width: 140px"
            >
              <el-option
                v-for="item in optionsId"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <el-radio-group v-model="queryForm.order_state_id" @change="queryData">
              <el-radio-button v-for="item in optionsId" :key="item.value" :label="item.value" >{{item.label}}</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="false">
            <el-select
              v-model="queryForm.kind_id"
              clearable
              :placeholder="__('订单类型')"
              style="width: 100px"
            >
              <el-option
                v-for="item in optionsKindId"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="false">
            <el-select
              v-model="queryForm.order_buyer_evaluation_status"
              clearable
              :placeholder="__('是否评论')"
              style="width: 100px"
            >
              <el-option
                v-for="item in optionsStatus"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.order_id"
              clearable
              :placeholder="__('订单编号')"
              style="width: 100px"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.src_order_id"
              clearable
              :placeholder="__('源订单编号')"
              style="width: 100px"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.order_title"
              clearable
              :placeholder="__('订单标题')"
              style="width: 100px"
            />
          </el-form-item>
          <el-form-item>
            <div class="block">
              <ms-date-range-picker
                v-model="order_time_range"
                @update:modelValue="order_time_range = $event"
              />
              <el-date-picker
                v-if="false"
                v-model="queryForm.order_date"
                :placeholder="__('选择日期')"
                style="width: 150px"
                type="date"
                value-format="yyyy-MM-dd"
              />
            </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-right-panel>
    </vab-query-form>

    <el-table
      ref="table"
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        fixed="left"
        :label="__('源订单编号')"
        prop="src_order_id"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column
        align="center"
        fixed="left"
        :label="__('订单编号')"
        prop="order_id"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column
        align="center"
        :label="__('退货退款')"
        show-overflow-tooltip
        width="80px"
      >
        <template #default="{ row }">
          <span v-if="row.return_flag" style="color: orangered">
            <el-button v-if="row.return_flag" type="text">
              {{ __('退货信息') }}
            </el-button>
          </span>
          <span v-if="!row.return_flag">
            {{ __('否') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="left"
        :formatter="formatStateId"
        :label="__('订单状态')"
        prop="order_state_id"
        show-overflow-tooltip
        width="100"
      >
        <template #default="{ row }">
          <el-button v-if="row.order_state_id === 2010" plain size="mini">{{ formatStateId(row) }}</el-button>
          <el-button v-else-if="row.order_state_id === 2011" plain size="mini">{{  formatStateId(row) }}</el-button>
          <el-button v-else-if="row.order_state_id === 2013" plain size="mini">{{  formatStateId(row) }}</el-button>
          <el-button v-else-if="row.order_state_id === 2020" plain size="mini" type="warning">{{  formatStateId(row) }}</el-button>
          <el-button v-else-if="row.order_state_id === 2030" plain size="mini" type="danger">{{  formatStateId(row) }}</el-button>
          <el-button v-else-if="row.order_state_id === 2040" plain size="mini" type="info">{{  formatStateId(row) }}</el-button>
          <el-button v-else-if="row.order_state_id === 2060" plain size="mini" type="success">{{  formatStateId(row) }}</el-button>
          <el-button v-else-if="row.order_state_id === 2070" plain size="mini" type="plain">{{  formatStateId(row) }}</el-button>
          <el-button v-else>
            {{ __('未知') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="left"
        :label="__('应付金额')"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          {{ row.currency_symbol_left }}{{ row.order_payment_amount }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('付款方式')"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <span v-if="row.payment_type_id === 1301" style="color: red">{{ __('货到付款') }}</span>
          <span v-if="row.payment_type_id === 1302">{{ __('在线支付') }}</span>
          <span v-if="row.payment_type_id === 1303">{{ __('白条支付') }}</span>
          <span v-if="row.payment_type_id === 1304">{{ __('现金支付') }}</span>
          <span v-if="row.payment_type_id === 1305" style="color: red">
            {{ __('线下支付') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('下单时间')"
        prop="order_time"
        show-overflow-tooltip
        width="150"
      />
      <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="__('配送时间')"
        show-overflow-tooltip
        width="170"
      >
        <template #default="{ row }">
          <span v-if="row.delivery_time_name != '正常配送'" style="color: red">
            {{ row.delivery_time_name }}
          </span>
          <span v-if="row.delivery_time_name == '正常配送'">
            {{ row.delivery_time_name }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('配送地址')"
        prop="mo_receive_area"
        show-overflow-tooltip
        width="250"
      />
      <el-table-column
        align="center"
        :label="__('活动类型')"
        prop="activity_type_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('客户名称')"
        prop="buyer_user_name"
        show-overflow-tooltip
        width="100px"
      />
      <el-table-column
        align="center"
        :formatter="formatReview"
        :label="__('订单审核')"
        prop="order_is_review"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('积分')"
        prop="order_resource_ext1"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('报关状态')"
        prop="order_cb_sync"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatReview"
        :label="__('财务状态')"
        prop="order_finance_review"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatOut"
        :label="__('出库状态')"
        prop="order_is_out"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="foematShipped"
        :label="__('发货状态')"
        prop="order_is_shipped"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatDealTime"
        :label="__('状态时间')"
        prop="order_deal_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :formatter="formatPaid"
        :label="__('付款状态')"
        prop="order_is_paid"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatReceived"
        :label="__('收货状态')"
        prop="order_is_received"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('买家留言')"
        prop="order_message"
        show-overflow-tooltip
      />
    </el-table>

    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { renderTime, timestampToTime } from '@/utils/date'
  import {
    getBaseList,
  } from '@/api/order/base'
  import { mapGetters } from 'vuex'
  import {stateCode} from '@/config'
  import MsDateRangePicker from '@/components/MsDateRangePicker'

  export default {
    name: 'PurchaseOrder',
    components: {MsDateRangePicker},
    data() {
      return {
        stateCode:stateCode,
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        dataList: [],
        optionsStatus: [
          {
            value: 0,
            label: this.__('未评价'),
          },
          {
            value: 1,
            label: this.__('已评价'),
          },
        ],
        optionsKindId: [
          {
            value: 1201,
            label: this.__('实物订单'),
          },
          {
            value: 1202,
            label: this.__('服务订单'),
          },
          {
            value: 1203,
            label: this.__('电子卡券'),
          },
          {
            value: 1204,
            label: this.__('外卖订单'),
          },
          {
            value: 1205,
            label: this.__('课程订单'),
          },
        ],
        optionsId: [
          {
            value: null,
            label: this.__('全部'),
          },
          {
            value: 2010,
            label: this.__('待付款'),
          },
          {
            value: 2030,
            label: this.__('待发货'),
          },
          {
            value: 2040,
            label: this.__('待签收'),
          },
          {
            value: 2060,
            label: this.__('完成'),
          },
          {
            value: 2070,
            label: this.__('关闭'),
          },
        ],
        value: '',
        listLoading: true,
        loading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          stime: null,
          etime: null,
          kind_id:null,
          order_state_id:null,
          order_type: "buyer",
          pageNum: 1,
          pageSize: 20,
        },
        order_time_range:["", ""],
      }
    },
    computed: {
      ...mapGetters({
        configs: 'routes/configs',
      }),
      ...mapGetters({
        rid: 'user/rid',
      }),
    },
    created() {
      this.fetchData()
    },
    methods: {
      formatDealTime(row) {
        return timestampToTime(row.order_deal_time)
      },
      formatTime(row) {
        return renderTime(row.order_time)
      },
      formatPaid(row) {
        return row.order_is_paid === 3010
          ? this.__('未付款')
          : row.order_is_paid === 3011
            ? this.__('付款待审核')
            : row.order_is_paid === 3012
              ? this.__('部分付款')
              : this.__('已付款')
      },
      formatReceived(row) {
        return row.order_is_received === 0
          ? this.__('未收货')
          : this.__('已收货')
      },
      formatPrint(row) {
        return row.order_express_print === 0
          ? this.__('未打印')
          : this.__('已打印')
      },
      formatFlag(row) {
        return row.return_flag ? this.__('是') : this.__('否')
      },
      formatReview(row) {
        return row.order_is_review === 0 ? this.__('未审核') : this.__('已审核')
      },
      foematShipped(row) {
        return row.order_is_shipped === 3030
          ? this.__('未发货')
          : row.order_is_shipped === 3032
            ? this.__('已发货')
            : this.__('部分发货')
      },
      formatOut(row) {
        return row.order_is_out === 3020
          ? this.__('未出库')
          : row.order_is_out === 3021
            ? this.__('部分出库通过')
            : this.__('已出库')
      },
      formatStateId(row) {
        return row.order_state_id === 2011
          ? this.__('待审核')
          : row.order_state_id === 2013
            ? this.__('待财务审核')
            : row.order_state_id === 2020
              ? this.__('待配货')
              : row.order_state_id === 2030
                ? this.__('待发货')
                : row.order_state_id === 2040
                  ? this.__('待签收')
                  : row.order_state_id === 2060
                    ? this.__('已完成')
                    : row.order_state_id === 2010
                      ? this.__('待付款')
                      : this.__('已关闭')
      },
      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

        if (this.order_time_range.length > 0) {
          if (this.order_time_range[0]) {
            this.queryForm.stime = this.order_time_range[0].getTime();
          }
          if (this.order_time_range[1]) {
            this.queryForm.etime = this.order_time_range[1].getTime();
          }
        } else {
          this.queryForm.stime = null
          this.queryForm.etime = null
        }


        const { data } = await getBaseList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
<style lang="scss" scoped>

  .el-popper {
    width: 100px;

    .oper-btn{
      display: inline-grid;
      width: 100%;

      .el-button{
        margin: 5px auto;
      }
    }
  }

</style>
