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

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="报价单号" prop="number">
        <el-input v-model="queryParams.number" placeholder="请输入报价单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </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']">新增</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']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <!-- <el-table-column label="主键ID" align="center" prop="offerId" /> -->
      <el-table-column label="报价单号" align="center" prop="number" />
      <!-- <el-table-column label="订单ids" align="center" prop="orderIds" /> -->
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="客户名称" align="center" prop="consignorId" />
      <!-- <el-table-column label="收货人id" align="center" prop="consigneeId" />
      <el-table-column label="唛头" align="center" prop="marks" />
      <el-table-column label="报关类别：我司全代：1，自单代报：2，混合报关：3" align="center" prop="customsType" />
      <el-table-column label="始发地" align="center" prop="departureId" />
      <el-table-column label="始发地信息" align="center" prop="departure" /> -->
      <el-table-column label="目的地" align="center" prop="objectiveId" />
      <!-- <el-table-column label="目的地信息" align="center" prop="objective" /> -->
      <el-table-column label="预计结束时间" align="center" prop="stopTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stopTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运输方式" align="center" prop="stopTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stopTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务员" align="center" prop="stopTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stopTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="控货" align="center" prop="control" />
      <el-table-column label="竞争对手" align="center" prop="competitor" />
      <el-table-column label="重要程度" align="center" prop="importance" />
      <el-table-column label="价格有效期开始" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格有效期结束" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交货方式：FOB(离岸价)，CIF(到岸价)，CNF(成本加运费)，EXW(出厂价)" align="center" prop="tradeType" />
      <el-table-column label="交货港口、地点" align="center" prop="tradeAdress" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="状态：取消报价：0，特价审批中：1，需求确认：2跟进中：3，赢单：4，输单：5，报价完成：6" align="center" prop="status" />
      <el-table-column label="取消报价前状态" align="center" prop="oldStatus" />
      <el-table-column label="报价关联客户，发货人：1，收货人：2" align="center" prop="relation" />
      <el-table-column label="报价关联客户id" align="center" prop="relationId" />
      <el-table-column label="佣金类型：0 无 1 明佣 2 暗佣 3 明+暗 " align="center" prop="commissionType" />
      <el-table-column label="入仓类型类型" align="center" prop="warehousingType" />
      <el-table-column label="原因" align="center" prop="reason" /> -->
      <el-table-column label="预计销售额" align="center" prop="estCost" />
      <!-- <el-table-column label="站内信状态，0未发送，1已发送" align="center" prop="sendstatus" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['ecw:offer:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['ecw:offer:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报价单号" prop="number">
          <el-input v-model="form.number" placeholder="请输入报价单号" />
        </el-form-item>
        <el-form-item label="订单ids" prop="orderIds">
          <el-input v-model="form.orderIds" placeholder="请输入订单ids" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="客户id，发货人" prop="consignorId">
          <el-input v-model="form.consignorId" placeholder="请输入客户id，发货人" />
        </el-form-item>
        <el-form-item label="收货人id" prop="consigneeId">
          <el-input v-model="form.consigneeId" placeholder="请输入收货人id" />
        </el-form-item>
        <el-form-item label="唛头" prop="marks">
          <el-input v-model="form.marks" placeholder="请输入唛头" />
        </el-form-item>
        <el-form-item label="报关类别：我司全代：1，自单代报：2，混合报关：3" prop="customsType">
          <el-select v-model="form.customsType" placeholder="请选择报关类别：我司全代：1，自单代报：2，混合报关：3">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="始发地" prop="departureId">
          <el-input v-model="form.departureId" placeholder="请输入始发地" />
        </el-form-item>
        <el-form-item label="始发地信息" prop="departure">
          <el-input v-model="form.departure" placeholder="请输入始发地信息" />
        </el-form-item>
        <el-form-item label="目的地" prop="objectiveId">
          <el-input v-model="form.objectiveId" placeholder="请输入目的地" />
        </el-form-item>
        <el-form-item label="目的地信息" prop="objective">
          <el-input v-model="form.objective" placeholder="请输入目的地信息" />
        </el-form-item>
        <el-form-item label="预计结束时间" prop="stopTime">
          <el-date-picker clearable v-model="form.stopTime" type="date" value-format="yyyy-MM-dd" placeholder="选择预计结束时间" />
        </el-form-item>
        <el-form-item label="控货" prop="control">
          <el-radio-group v-model="form.control">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="竞争对手" prop="competitor">
          <el-input v-model="form.competitor" placeholder="请输入竞争对手" />
        </el-form-item>
        <el-form-item label="重要程度" prop="importance">
          <el-input v-model="form.importance" placeholder="请输入重要程度" />
        </el-form-item>
        <el-form-item label="价格有效期开始" prop="startTime">
          <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd" placeholder="选择价格有效期开始" />
        </el-form-item>
        <el-form-item label="价格有效期结束" prop="endTime">
          <el-date-picker clearable v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="选择价格有效期结束" />
        </el-form-item>
        <el-form-item label="交货方式：FOB(离岸价)，CIF(到岸价)，CNF(成本加运费)，EXW(出厂价)" prop="tradeType">
          <el-select v-model="form.tradeType" placeholder="请选择交货方式：FOB(离岸价)，CIF(到岸价)，CNF(成本加运费)，EXW(出厂价)">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="交货港口、地点" prop="tradeAdress">
          <el-input v-model="form.tradeAdress" placeholder="请输入交货港口、地点" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="状态：取消报价：0，特价审批中：1，需求确认：2跟进中：3，赢单：4，输单：5，报价完成：6" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="取消报价前状态" prop="oldStatus">
          <el-radio-group v-model="form.oldStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报价关联客户，发货人：1，收货人：2" prop="relation">
          <el-input v-model="form.relation" placeholder="请输入报价关联客户，发货人：1，收货人：2" />
        </el-form-item>
        <el-form-item label="报价关联客户id" prop="relationId">
          <el-input v-model="form.relationId" placeholder="请输入报价关联客户id" />
        </el-form-item>
        <el-form-item label="佣金类型：0 无 1 明佣 2 暗佣 3 明+暗 " prop="commissionType">
          <el-select v-model="form.commissionType" placeholder="请选择佣金类型：0 无 1 明佣 2 暗佣 3 明+暗 ">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="入仓类型类型" prop="warehousingType">
          <el-select v-model="form.warehousingType" placeholder="请选择入仓类型类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入原因" />
        </el-form-item>
        <el-form-item label="预计费用清单" prop="estCost">
          <el-input v-model="form.estCost" placeholder="请输入预计费用清单" />
        </el-form-item>
        <el-form-item label="站内信状态，0未发送，1已发送" prop="sendstatus">
          <el-radio-group v-model="form.sendstatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createOffer, updateOffer, deleteOffer, getOffer, getOfferPage, exportOfferExcel } from "@/api/ecw/offer";

export default {
  name: "Offer",
  components: {
  },
  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,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        number: [{ required: true, message: "报价单号不能为空", trigger: "blur" }],
        consignorId: [{ required: true, message: "客户id，发货人不能为空", trigger: "blur" }],
        consigneeId: [{ required: true, message: "收货人id不能为空", trigger: "blur" }],
        sendstatus: [{ required: true, message: "站内信状态，0未发送，1已发送不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    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;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        offerId: undefined,
        number: undefined,
        orderIds: undefined,
        orderNo: undefined,
        consignorId: undefined,
        consigneeId: undefined,
        marks: undefined,
        customsType: undefined,
        departureId: undefined,
        departure: undefined,
        objectiveId: undefined,
        objective: undefined,
        stopTime: undefined,
        control: undefined,
        competitor: undefined,
        importance: undefined,
        startTime: undefined,
        endTime: undefined,
        tradeType: undefined,
        tradeAdress: undefined,
        remarks: undefined,
        status: undefined,
        oldStatus: undefined,
        relation: undefined,
        relationId: undefined,
        commissionType: undefined,
        warehousingType: undefined,
        reason: undefined,
        estCost: undefined,
        sendstatus: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    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')
      this.reset();
      this.open = true;
      this.title = "添加报价单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const offerId = row.offerId;
      getOffer(offerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报价单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.offerId != null) {
          updateOffer(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createOffer(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const offerId = row.offerId;
      this.$modal.confirm('是否确认删除报价单管理编号为"' + offerId + '"的数据项?').then(function() {
          return deleteOffer(offerId);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).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('是否确认导出所有报价单管理数据项?').then(() => {
          this.exportLoading = true;
          return exportOfferExcel(params);
        }).then(response => {
          this.$download.excel(response, '${table.classComment}.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>