<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="报价单号" prop="quoteNo">
        <el-input v-model="queryParams.quoteNo" placeholder="请输入报价单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input v-model="queryParams.clientName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="全部" clearable style="width:110px">
          <el-option label="草稿" value="draft" />
          <el-option label="已发送" value="sent" />
          <el-option label="已确认" value="confirmed" />
          <el-option label="已拒绝" value="rejected" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @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">新建报价单</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="quoteList" border>
      <el-table-column label="报价单号" prop="quoteNo" width="160" />
      <el-table-column label="客户名称" prop="clientName" min-width="160" show-overflow-tooltip />
      <el-table-column label="关联询价单" prop="rfqNo" width="140" />
      <el-table-column label="总金额" prop="totalAmount" width="120" align="right">
        <template slot-scope="s">
          <strong style="color:#409EFF">¥{{ s.row.totalAmount | money }}</strong>
        </template>
      </el-table-column>
      <el-table-column label="币种" prop="currency" width="70" align="center" />
      <el-table-column label="有效期" prop="validityDate" width="110" align="center">
        <template slot-scope="s">{{ s.row.validityDate | date }}</template>
      </el-table-column>
      <el-table-column label="状态" width="90" align="center">
        <template slot-scope="s">
          <el-tag :type="statusType(s.row.status)" size="mini">{{ statusLabel(s.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" width="160" align="center" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="s">
          <el-button size="mini" type="text" icon="el-icon-view" @click="goDetail(s.row)">详情/编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" style="color:#f56c6c" @click="handleDelete(s.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { listClientQuote, delClientQuote, addClientQuote } from "@/api/bid/clientquote";
export default {
  name: "ClientQuote",
  filters: {
    money(v) { return v ? Number(v).toFixed(2) : "0.00"; },
    date(v) { return v ? v.substring(0, 10) : "-"; }
  },
  data() {
    return {
      loading: false,
      quoteList: [],
      total: 0,
      queryParams: { pageNum: 1, pageSize: 10, quoteNo: null, clientName: null, status: null }
    };
  },
  created() { this.getList(); },
  methods: {
    getList() {
      this.loading = true;
      listClientQuote(this.queryParams).then(res => {
        this.quoteList = res.rows || [];
        this.total = res.total || 0;
        this.loading = false;
      }).catch(() => { this.loading = false; });
    },
    handleQuery() { this.queryParams.pageNum = 1; this.getList(); },
    resetQuery() { this.resetForm("queryForm"); this.handleQuery(); },
    handleAdd() {
      // Create draft then navigate
      addClientQuote({ status: "draft", currency: "CNY", items: [] }).then(res => {
        const id = res.data && res.data.quoteId;
        if (id) this.$router.push({ path: "/quotemgr/clientquote/detail", query: { quoteId: id } });
        else this.$router.push({ path: "/quotemgr/clientquote/detail", query: { quoteId: "__new__" } });
      }).catch(() => {
        this.$router.push({ path: "/quotemgr/clientquote/detail", query: { quoteId: "__new__" } });
      });
    },
    goDetail(row) {
      this.$router.push({ path: "/quotemgr/clientquote/detail", query: { quoteId: row.quoteId } });
    },
    handleDelete(row) {
      this.$modal.confirm("确认删除报价单【" + row.quoteNo + "】？").then(() => {
        return delClientQuote(row.quoteId);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      });
    },
    statusType(s) { return { draft: "info", sent: "primary", confirmed: "success", rejected: "danger" }[s] || "info"; },
    statusLabel(s) { return { draft: "草稿", sent: "已发送", confirmed: "已确认", rejected: "已拒绝" }[s] || s; }
  }
};
</script>
