<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card>
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        inline
        class="card"
      >
          <el-form-item :label="$t('自编号')">
            <el-input v-model="queryParams.selfNo" clearable></el-input>
          </el-form-item>
          <el-form-item :label="$t('始发仓')">
            <el-select v-model="queryParams.startWarehouseIdList" :placeholder="$t('请选择')" clearable>
              <el-option
                v-for="item in exportWarehouseList"
                :key="item.id"
                :label="$l(item, 'title')"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('目的国')" :rules="{required: true}">
            <el-select v-model="queryParams.destCountryId" :placeholder="$t('请选择')" filterable default-first-option>
              <el-option
                v-for="item in countryList"
                :key="item.id"
                :label="$l(item, 'title')"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('目的仓')">
            <el-select v-model="queryParams.destWarehouseId" :placeholder="queryParams.destCountryId ? $t('请选择') : $t('请先选择目的国')" clearable>
              <el-option
                v-for="item in importWarehouseList"
                :key="item.id"
                :label="$l(item, 'title')"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('自编号状态')">
            <dict-selector :type="DICT_TYPE.BOX_ORDER_SHIPMENT_STATE" v-model="queryParams.status" clearable></dict-selector>
          </el-form-item>
          <el-form-item :label="$t('结算时间')" >
            <el-date-picker v-model="dateFilter" type="datetimerange"  range-separator="-"
              :start-placeholder="$t('开始日期')"
              :end-placeholder="$t('结束日期')"
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="handleQuery"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item :label="$t('是否结算')">
            <dict-selector :type="DICT_TYPE.SETTLEMENT_STATUS" v-model="queryParams.slStatus" clearable></dict-selector>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleQuery"
              :loading="loading"
            >{{$t('查询')}}</el-button>
            <el-button type="primary" :loading="exporting" icon="el-icon-download" @click="handleExport">{{$t('导出搜索')}}</el-button>
          </el-form-item>
      </el-form>
    </el-card>
    <el-table v-loading="loading" :data="list" border class="card" :element-loading-text="$t('汇总计算中...')">
      <el-table-column :label="$t('是否结算')" align="center">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.SETTLEMENT_STATUS" :value="scope.row.slStatus"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('No.柜号')" align="center" prop="selfNo">
        <template slot-scope="scope">
          <el-button v-if="permissions.indexOf('selfno:report:detail') > -1" type="text" @click="$router.push('./self_no_report_detail?containerNumber=' + scope.row.selfNo)">{{scope.row.selfNo}}</el-button>
          <span v-else>{{scope.row.selfNo}}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('应收款(Accounts receivable)')" align="center" prop="summaryInfo.receivableTotalFeeGroup"></el-table-column>
      <el-table-column :label="$t('received in + 目的国国家(目的地实收)')" align="center" prop="">
        <el-table-column :label="$t('运费')" prop="summaryInfo.netReceiptsCollectFreightFeeGroup"></el-table-column>
        <el-table-column :label="$t('清关费')" prop="summaryInfo.netReceiptsCollectClearanceFeeGroup"></el-table-column>
        <el-table-column :label="$t('额外费用')" prop="summaryInfo.netReceiptsCollectOtherFeeGroup"></el-table-column>
      </el-table-column>
      <el-table-column :label="$t('received in China(中国实收)')" align="center" prop="" >
        <el-table-column :label="$t('运费')" prop="summaryInfo.netReceiptsAdvanceFreightFeeGroup"></el-table-column>
        <el-table-column :label="$t('清关费')" prop="summaryInfo.netReceiptsAdvanceClearanceFeeGroup"></el-table-column>
        <el-table-column :label="$t('额外费用')" prop="summaryInfo.netReceiptsAdvanceOtherFeeGroup"></el-table-column>
      </el-table-column>
      <el-table-column :label="$t('Discounts 折扣')" align="center" prop="">
        <el-table-column :label="$t('运费')" prop="summaryInfo.discountFreightFeeGroup"></el-table-column>
        <el-table-column :label="$t('清关费')" prop="summaryInfo.discountClearanceFeeGroup"></el-table-column>
        <el-table-column :label="$t('额外费用')" prop="summaryInfo.discountOtherFeeGroup"></el-table-column>
      </el-table-column>
      <el-table-column :label="$t('操作')" v-if="permissions.indexOf('selfno:report:detail') > -1">
        <template slot-scope="scope">
          <el-button type="text" @click="$router.push('./self_no_report_detail?containerNumber=' + scope.row.selfNo)">{{$t('查看')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.rows"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getCurrencyPage } from "@/api/ecw/currency";
import {getWarehouseList} from '@/api/ecw/warehouse'
import store from "@/store";
import {boxSettlementPage, exportSettlementExcel} from "@/api/ecw/box";
import {DICT_TYPE} from "@/utils/dict";
import {getCountryListAll} from "@/api/ecw/country";
import {getListTree} from "@/api/ecw/region";

export default {
  data() {
    return {
      loading: false, // 是否加载中
      dateFilter:[], // 筛选时间
      list: [],
      total: 0,
      queryParams: {
        page: 1,
        rows: 10,
      },
      // tradeCityList: [],
      currencyList:[],
      warehouseList:[],
      countryList: [],
      // 导出中状态
      exporting: false
    };
  },
  activated(){
    this.getList();
  },
  created() {
    getCurrencyPage(this.params).then(res => this.currencyList = res.data.list)
    // getTradeCityList().then((res) => (this.tradeCityList = res.data));
    this.getList();
    getWarehouseList().then(res => this.warehouseList = res.data)
    getListTree({treeType: 1}).then(r => {
      this.countryList = r.data
    })
  },
  watch:{
    // 目的国变更后要重置目的仓
    'queryParams.destCountryId': function(){
      this.$set(this.queryParams, 'destWarehouseId', '')
    }
  },
  computed: {
    DICT_TYPE() {
      return DICT_TYPE
    },
    permissions(){
      return store.getters.permissions
    },
    /*exportCityList() {
      return this.tradeCityList.filter((item) => item.type === 2);
    },
    importCityList() {
      return this.tradeCityList.filter((item) => item.type === 1);
    },*/
    exportWarehouseList(){
      /* tradeType 1 进口，2出口，3进出口 */
      return this.warehouseList.filter(item => +item.tradeType === 2 || +item.tradeType === 3)
    },
    importWarehouseList(){
      // 目的仓根据选择的目的国过滤
      if(!this.queryParams.destCountryId) return []
      return this.warehouseList.filter(item => {
        return this.queryParams.destCountryId === item.guojia && ( +item.tradeType === 1 || +item.tradeType === 3)
      })
    },
  },
  methods: {
    getCurrencyLabel(id){
      const item = this.currencyList.find(item => item.id === id);
      if(!item)return ''
      return this.$l(item, 'title')
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      let params = {...this.queryParams}
      this.addBeginAndEndTime(params, this.dateFilter, "JsDate", false);
      // 执行查询
      boxSettlementPage(params).then((response) => {
        this.list = response.data.list;
        this.total = response.data.total;
      }).finally(() => {
        this.loading = false;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      if(!this.queryParams.destCountryId){
        return this.$message.error(this.$t('请选择目的国'))
      }
      this.getList();
    },
    handleReset(){
      this.$refs['queryForm'].resetFields()
      this.dateFilter = []
      this.handleQuery()
    },
    // 导出搜索
    handleExport(){
      this.exporting = true
      let params = {...this.queryParams}
      this.addBeginAndEndTime(params, this.dateFilter, "JsDate", false);
      exportSettlementExcel(params).then(res => {
        this.$download.excel(res, 'shipment-summary.xls');
      }).finally(() => {
        this.exporting = false
      })
    }
  },
};
</script>

<style scoped>
.card {
  margin-top: 20px;
}
.dialog-footer {
  padding: 40px;
}
.card-title {
  font-size: 18px;
  font-weight: bold;
  display: inline-block;
}
</style>
