<template>
  <el-container>
    <el-main>
      <div class="batch-dispatch sa-m-b-20">
        <div class="title"> 方法一：如使用导入订单，需完善发货表单物流信息后再上传 </div>
        <div class="order-content sa-flex sa-flex-col">
          <div class="sa-flex sa-flex-col">
            <div class="order-file sa-table-line-1 sa-m-b-8">
              <span v-if="state.file">{{ state.file.name }}</span>
            </div>
            <el-upload
              action
              :http-request="onImportFile"
              :show-file-list="false"
              accept=".xlsx,.xls"
            >
              <el-button v-if="!state.file">导入发货单</el-button>
              <el-button class="is-link" type="primary" v-if="state.file">重新选择文件</el-button>
            </el-upload>
          </div>
          <el-form class="sa-m-t-22" ref="expressRef" :inline="true" :model="express">
            <el-form-item
              label="快递公司"
              prop="code"
              :rules="[{ required: true, message: '请选择快递公司', trigger: 'none' }]"
            >
              <el-select
                v-model="express.code"
                placeholder="请选择快递公司"
                @change="onChangeExpressCode"
                filterable
                remote
                reserve-keyword
                :remote-method="remoteMethod"
                :loading="deliverCompany.loading"
              >
                <el-option
                  v-for="dc in deliverCompany.data"
                  :key="dc"
                  :label="dc.name"
                  :value="dc.code"
                  :ref="`dc-${dc.code}`"
                  :data-name="dc.name"
                  >{{ dc.name }}&nbsp;({{ dc.code }})</el-option
                >
                <div class="sa-p-l-15 sa-p-r-27">
                  <sa-pagination
                    layout="total, prev, pager, next"
                    :pageData="deliverCompany.pageData"
                    @updateFn="getDeliverCompany"
                  />
                </div>
              </el-select>
              <div class="select-append" @click="onDispatch">发货</div>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="batch-dispatch">
        <div class="title">
          方法二：如使用批量发货，需确认
          <el-button class="is-link" type="primary"> &nbsp;商城配置-第三方服务&nbsp; </el-button>
          中快递鸟配置完成
        </div>
        <div class="sa-flex sa-row-center">
          <el-button
            type="primary"
            :disabled="modal.params.order_ids.length == 0"
            @click="onBatchDispatch"
            >批量发货</el-button
          >
        </div>
      </div>
    </el-main>
  </el-container>
</template>
<script setup>
  import { getCurrentInstance, reactive } from 'vue';
  import { useModal } from '@/sheep/hooks';
  import { api } from '../order.service';
  import useExpress from '@/app/shop/admin/data/express/express.js';
  import OrderLoopDispatch from './loopDispatch.vue';

  const props = defineProps(['modal']);

  const { proxy } = getCurrentInstance();

  const state = reactive({
    file: '',
  });

  const { express, deliverCompany, getDeliverCompany, onChangeExpressCode, remoteMethod } =
    useExpress();

  // 导入发货单
  function onImportFile(file) {
    state.file = file.file;
  }

  // 发货单发货 TODO
  async function onDispatch() {
    proxy.$refs.expressRef &&
      proxy.$refs.expressRef.validate(async (valid) => {
        if (valid) {
          const dispatchData = {
            action: 'multiple',
            'express[name]': express.name,
            'express[code]': express.code,
            file: state.file,
          };

          let dispatchForm = new FormData();
          for (let name in dispatchData) {
            dispatchForm.append(name, dispatchData[name]);
          }

          let { error, data } = await api.order.dispatchByUpload(dispatchForm);
          if (error == 0)
            useModal(
              OrderLoopDispatch,
              {
                title: '批量操作',
                method: 'upload',
                data: data,
              },
              {
                confirm: () => {},
              },
            );
        }
      });
  }

  // 批量发货
  async function onBatchDispatch() {
    const { error, data } = await api.order.dispatch({
      order_ids: props.modal.params.order_ids,
      action: 'multiple',
    });
    if (error == 0)
      useModal(
        OrderLoopDispatch,
        {
          title: '批量操作',
          method: 'api',
          data: data,
        },
        {
          confirm: () => {},
        },
      );
  }
</script>
<style lang="scss" scoped>
  .batch-dispatch {
    background: var(--sa-table-header-bg);
    border-radius: 4px;
    padding: 0 16px 24px;
    .title {
      line-height: 18px;
      margin-bottom: 20px;
      padding: 15px 0;
      display: flex;
      align-items: center;
      .el-button {
        height: 18px;
      }
    }
    .order-content {
      .order-file {
        height: 16px;
        line-height: 16px;
        font-size: 12px;
        font-weight: 400;
        color: var(--sa-font);
      }
      .el-form--inline .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
      }

      :deep() {
        .el-select {
          flex: 1;
        }
        .el-input__inner {
          border-radius: 4px 0 0 4px;
        }
      }
      .select-append {
        padding: 0 16px;
        line-height: 30px;
        background: var(--sa-table-header-bg);
        border: 1px solid var(--sa-border);
        box-sizing: border-box;
        border-left: none;
        border-radius: 0 4px 4px 0;
        font-size: 12px;
        font-weight: 400;
        color: var(--sa-font);
        cursor: pointer;
      }
    }
  }
</style>
