<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item :label="$t('编号')" prop="number">
        <el-input v-model="queryParams.number" :placeholder="$t('请输入报价单号、订单号')" clearable @keyup.enter.native="handleQuery"/>
        <!--// TODO -->
      </el-form-item>

      <el-form-item :label="$t('客户')" prop="orderNo">
        <el-input v-model="queryParams.orderNo" :placeholder="$t('请输入客户')" clearable @keyup.enter.native="handleQuery"/>
        <!--// TODO -->
      </el-form-item>
      
      <el-form-item :label="$t('创建时间')" prop="orderNo">
        <el-date-picker v-model="queryParams.beginCreateTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        到
        <el-date-picker v-model="queryParams.endCreateTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      
      <el-form-item :label="$t('始发地')" prop="departureId">
        <el-select v-model="queryParams.departureId" :placeholder="$t('请选择始发地')">
          <el-option v-for="item in exportCityList" :label="$l(item, 'title')" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('目的地')" prop="objectiveId">
        <el-select v-model="queryParams.objectiveId" :placeholder="$t('请选择目的地')">
          <el-option v-for="item in importCityList" :label="$l(item, 'title')" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('运输方式')" prop="orderNo">
        <dict-selector :type="DICT_TYPE.ECW_TRANSPORT_TYPE" v-model="queryParams.transportId" formatter="number" />
      </el-form-item>
      <el-form-item :label="$t('商品')" prop="orderNo">
        <el-input v-model="queryParams.product" :placeholder="$t('请输入商品类型、品名或品牌')" clearable @keyup.enter.native="handleQuery"/>
        <!--// TODO -->
      </el-form-item>
      <el-form-item :label="$t('控货')" prop="control">
        <dict-selector :type="DICT_TYPE.INFRA_BOOLEAN_STRING" v-model="queryParams.control" />
      </el-form-item>
      <el-form-item :label="$t('客户经理')" prop="orderNo">
        <user-selector v-model="queryParams.salesman" />
        <!--// TODO -->
      </el-form-item>
      <el-form-item :label="$t('销售阶段')" prop="status">
        <dict-selector :type="DICT_TYPE.ECW_OFFER_STATUS" v-model="queryParams.status" />
      </el-form-item>
      <el-form-item :label="$t('报关方式')" prop="customsType">
        <dict-selector :type="DICT_TYPE.ECW_CUSTOMS_TYPE" v-model="queryParams.customsType" />
      </el-form-item>

      <div>
        <el-form-item :label="$t('预计结束时间')" prop="orderNo">
            <el-date-picker v-model="queryParams.beginStopTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            到
            <el-date-picker v-model="queryParams.endStopTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{$t('搜索')}}</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">{{$t('重置')}}</el-button>
        </el-form-item>
      </div>
      
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['ecw:offer:create']">{{$t('新增')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['ecw:offer:export']">{{$t('导出')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list" border>
      <el-table-column :label="$t('报价单号')" align="left" prop="number" width="200px" />
      <el-table-column :label="$t('订单编号')" align="left" prop="orderNo" />
      <el-table-column :label="$t('客户名称')" align="left" prop="relationName" />
      <el-table-column :label="$t('目的地')" align="center" prop="objectiveName" />
      <el-table-column :label="$t('销售阶段')" align="left" width="180">
        <template slot-scope="{row}">
          <dict-tag :type="DICT_TYPE.ECW_OFFER_STATUS" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('预计结束时间')" align="left">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stopTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('运输方式')" align="left">
        <template slot-scope="scope" >
          <dict-tag :type="DICT_TYPE.ECW_TRANSPORT_TYPE" :value="scope.row.transportId" />
        </template>
      </el-table-column>
      

      <el-table-column :label="$t('预计销售额')" align="left" prop="estCost">
        <template slot-scope="{row}">
          <div class="" v-for="(item, feeIndex) in row.estCostVO.feeDtoList" :key="feeIndex">
            <dict-tag :type="DICT_TYPE.ECW_COST_FEE_TYPE" :value="item.feeType" />
            {{item.amount}} {{currencyMap[item.currencyId]}}
          </div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('客户经理')" align="left" prop="creatorName">
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- 
          0 取消                                        恢复
          1 特价审批    编辑、跟进、          取消    删除
          2 草稿        编辑、                       删除
          3 需求确认    编辑、跟进、结果、特价、取消   删除
          4 赢单                                    删除
          5 输单                                    删除
          6 完成                                    删除
          -->
          <el-dropdown>
            <el-button icon="el-icon-plus" circle type="primary"></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$router.push('detail?offerId=' + scope.row.offerId)"                                                                                                                  >{{$t('详情')}}</el-dropdown-item>
              <el-dropdown-item @click.native="$router.push('edit?id=' + scope.row.offerId)"                                   v-if="[1,2,3].indexOf(scope.row.status) > -1"        v-hasPermi="['ecw:offer:update']">{{$t('编辑')}}</el-dropdown-item>
              <el-dropdown-item @click.native="$router.push('logList?offerId=' + scope.row.offerId)"                           v-if="[1,3].indexOf(scope.row.status) > -1"          v-hasPermi="['ecw:offer:update']">{{$t('跟进')}}</el-dropdown-item>
              <el-dropdown-item @click.native="$router.push(`result?offerId=${scope.row.offerId}&number=${scope.row.number}`)" v-if="[3].indexOf(scope.row.status) > -1"            v-hasPermi="['ecw:offer:update']">{{$t('结果')}}</el-dropdown-item>
              <el-dropdown-item @click.native="$router.push('/offer/special?offerId=' + scope.row.offerId)"                            v-if="[3].indexOf(scope.row.status) > -1"            v-hasPermi="['ecw:offer:update']">{{$t('特价')}}</el-dropdown-item>
              <el-dropdown-item @click.native="$alert('// TODO')"                                                              v-if="[1,3].indexOf(scope.row.status) > -1"          v-hasPermi="['ecw:offer:update']">{{$t('取消')}}</el-dropdown-item>
              <el-dropdown-item @click.native="$alert('// TODO')"                                                              v-if="[0].indexOf(scope.row.status) > -1"            v-hasPermi="['ecw:offer:update']">{{$t('恢复')}}</el-dropdown-item>
              <el-dropdown-item @click.native="$alert('// TODO')"                                                              v-if="[1,2,3,4,5,6].indexOf(scope.row.status) > -1"  v-hasPermi="['ecw:offer:delete']">{{$t('删除')}}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

          <!-- <el-popover
            style="margin-left: 10px;"
            placement="bottom"
            width="100"
            trigger="hover">
              <el-button slot="reference" icon="el-icon-plus" circle type="primary"></el-button>
              <div style=" display:flex; flex-direction:column">
                <template v-if="[1,2].indexOf(scope.row.status) > -1">
                  <el-link type="primary" @click.native="$router.push('detail?offerId=' + scope.row.offerId)"
                            v-hasPermi="['ecw:offer:detail']">{{$t('详情')}}</el-link>
                  <el-link type="primary" @click="$router.push('edit?id=' + scope.row.offerId)"
                            v-hasPermi="['ecw:offer:update']">{{$t('编辑')}}</el-link>
                  <el-link type="primary" @click="$router.push('logList?offerId=' + scope.row.offerId)"
                            v-hasPermi="['ecw:offer:update']">{{$t('跟进')}}</el-link>
                </template>
                <template v-if="scope.row.status == 2">
                  <el-button size="mini" type="text" @click="$router.push('detail?offerId=' + scope.row.offerId)"
                            v-hasPermi="['ecw:offer:update']">{{$t('详情')}}</el-button>
                  <el-button size="mini" type="text" @click="$router.push(`result?offerId=${scope.row.offerId}&number=${scope.row.number}`)"
                            v-hasPermi="['ecw:offer:update']">{{$t('结果')}}</el-button>
                  <el-button size="mini" type="text" @click="$router.push('/offer/special/' + scope.row.offerId)"
                            v-hasPermi="['ecw:offer:update']">{{$t('特价')}}</el-button>

                  <el-button size="mini" type="text" 
                            v-hasPermi="['ecw:offer:update']">{{$t('取消')}}</el-button>

                  <el-button size="mini" type="text" 
                            v-hasPermi="['ecw:offer:update']">{{$t('恢复')}}</el-button>
                </template>

                <el-button size="mini" type="text" @click="$router.push('detail?offerId=' + scope.row.offerId)"
                          v-hasPermi="['ecw:offer:update']">{{$t('详情')}}</el-button>
                <el-button size="mini" type="text"
                          v-hasPermi="['ecw:offer:delete']">{{$t('删除')}}</el-button>
              </div>
          </el-popover> -->
          
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

  </div>
</template>

<script>
import { deleteOffer, getOfferPage, exportOfferExcel } from "@/api/ecw/offer";
import { getCurrencyList } from '@/api/ecw/currency';
import {getTradeCityList} from '@/api/ecw/region'
import UserSelector from '@/components/UserSelector'
export default {
  name: "Offer",
  components: {
    UserSelector
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报价单管理列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeStopTime: [],
      dateRangeStartTime: [],
      dateRangeEndTime: [],
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        number: null,
        orderIds: null,
        orderNo: null,
        consignorId: null,
        consigneeId: null,
        marks: null,
        customsType: null,
        departureId: null,
        departure: null,
        objectiveId: null,
        objective: null,
        control: null,
        competitor: null,
        importance: null,
        tradeType: null,
        tradeAdress: null,
        remarks: null,
        status: null,
        oldStatus: null,
        relation: null,
        relationId: null,
        commissionType: null,
        warehousingType: null,
        reason: null,
        estCost: null,
        sendstatus: null,
      },
      currencyList: [],
      tradeCityList:[]

    };
  },
  computed:{
    currencyMap(){
        let map = {}
        this.currencyList.forEach(item => {
            map[item.id] = this.$l(item, 'title')
        })
        return map
    },
    exportCityList() {
      return this.tradeCityList.filter(item => item.type == 2)
    },
    importCityList() {
      return this.tradeCityList.filter(item => item.type == 1)
    },
  },
  created() {
    getTradeCityList().then(res => this.tradeCityList = res.data)

    getCurrencyList().then(res => {
      this.currencyList = res.data
    })

    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeStopTime, 'stopTime');
      this.addBeginAndEndTime(params, this.dateRangeStartTime, 'startTime');
      this.addBeginAndEndTime(params, this.dateRangeEndTime, 'endTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getOfferPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeStopTime = [];
      this.dateRangeStartTime = [];
      this.dateRangeEndTime = [];
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      return this.$router.push('edit')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      return this.$router.push('edit?id=' + row.offerId)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const offerId = row.offerId;
      this.$modal.confirm(this.$t('是否确认删除报价单管理编号为{offerId}的数据项?', {offerId})).then(function() {
          return deleteOffer(offerId);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("删除成功"));
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeStopTime, 'stopTime');
      this.addBeginAndEndTime(params, this.dateRangeStartTime, 'startTime');
      this.addBeginAndEndTime(params, this.dateRangeEndTime, 'endTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm(this.$t('是否确认导出所有报价单管理数据项?')).then(() => {
          this.exportLoading = true;
          return exportOfferExcel(params);
        }).then(response => {
          this.$download.excel(response, '${table.classComment}.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
