<template>
  <div class="app-container">
    <el-tabs v-model="activeTab">
      <!-- ═══════════ Tab 1: 客户列表 ═══════════ -->
      <el-tab-pane label="客户列表" name="list">
        <div class="toolbar">
          <el-input
            v-model="queryParams.clientName"
            placeholder="搜索名称/编号/联系人"
            size="small"
            clearable
            style="width:320px"
            prefix-icon="el-icon-search"
            @keyup.enter.native="handleSearch"
          />
          <el-button type="primary" size="small" icon="el-icon-search" @click="handleSearch">搜索</el-button>
          <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">新增客户</el-button>
        </div>

        <el-table v-loading="loading" :data="clientList" border size="small" style="width:100%">
          <el-table-column label="编号" prop="clientNo" width="100" />
          <el-table-column label="名称" prop="clientName" min-width="160" />
          <el-table-column label="联系人" prop="contact" width="100" />
          <el-table-column label="电话" prop="phone" width="130" />
          <el-table-column label="城市" prop="city" width="110" />
          <el-table-column label="订单数" prop="orderCount" width="80" align="center" />
          <el-table-column label="备注" prop="remark" min-width="120" :show-overflow-tooltip="true" />
          <el-table-column label="操作" width="140" align="center" fixed="right">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" style="color:#f56c6c" @click="handleDelete(scope.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" />
      </el-tab-pane>

      <!-- ═══════════ Tab 2: 历史发货单 ═══════════ -->
      <el-tab-pane label="历史发货单" name="orders">
        <div class="toolbar">
          <el-select v-model="orderClientId" filterable placeholder="选择甲方客户" style="width:400px" @change="loadClientOrders" clearable>
            <el-option v-for="c in clientOptions" :key="c.clientId" :label="c.clientNo + ' | ' + c.clientName" :value="c.clientId" />
          </el-select>
          <span v-if="orderClientName" style="margin-left:12px;color:#909399;font-size:12px">共 {{ orderList.length }} 条记录</span>
        </div>

        <el-table v-loading="orderLoading" :data="orderList" border size="small" style="width:100%">
          <el-table-column label="发货单号" prop="doNo" width="150" />
          <el-table-column label="供应商" prop="supplierName" min-width="140" />
          <el-table-column label="金额" width="130" align="right">
            <template slot-scope="scope">¥{{ scope.row.totalAmount }}</template>
          </el-table-column>
          <el-table-column label="交货期" prop="deliveryDate" width="100" />
          <el-table-column label="结单日期" prop="actualCloseDate" width="100" />
          <el-table-column label="物料数" prop="itemCount" width="70" align="center" />
          <el-table-column label="状态" width="100">
            <template slot-scope="scope">
              <el-tag :type="statusType(scope.row.deliveryStatus)" size="small">{{ statusLabel(scope.row.deliveryStatus) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="80" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-view" @click="showOrderDetail(scope.row)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-empty v-if="!orderClientId && !orderLoading" description="请先选择甲方客户" />
        <el-empty v-if="orderClientId && !orderList.length && !orderLoading" description="该客户暂无发货记录" />
      </el-tab-pane>
    </el-tabs>

    <!-- ═══════════ 新增/编辑客户弹窗 ═══════════ -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogOpen" width="600px" append-to-body @close="cancelDialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="90px" size="small">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="客户编号" prop="clientNo">
              <el-input v-model="form.clientNo" placeholder="如 CU-001" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="clientName">
              <el-input v-model="form.clientName" placeholder="客户企业名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="联系人" prop="contact">
              <el-input v-model="form.contact" placeholder="联系人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="手机号/固话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="电子邮箱" prop="email">
              <el-input v-model="form.email" placeholder="电子邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在城市" prop="city">
              <el-input v-model="form.city" placeholder="如 广东深圳" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="详细地址" />
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="客户等级" prop="grade">
              <el-select v-model="form.grade" style="width:100%">
                <el-option label="A级" value="A" />
                <el-option label="B级" value="B" />
                <el-option label="C级" value="C" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" style="width:100%">
                <el-option label="正常" value="0" />
                <el-option label="停用" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancelDialog">取消</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-dialog>

    <!-- ═══════════ 发货单详情弹窗 ═══════════ -->
    <el-dialog title="发货单详情" :visible.sync="detailOpen" width="780px" append-to-body>
      <div v-if="detailData" class="detail-card">
        <div class="detail-section">
          <table class="detail-table">
            <tr>
              <td class="dt-label">发货单号</td><td class="dt-value"><b>{{ detailData.doNo }}</b></td>
              <td class="dt-label">供应商</td><td class="dt-value">{{ detailData.supplierName }}</td>
            </tr>
            <tr>
              <td class="dt-label">总金额</td><td class="dt-value" style="color:#409EFF;font-weight:700">¥{{ detailData.totalAmount }}</td>
              <td class="dt-label">状态</td>
              <td class="dt-value">
                <el-tag :type="statusType(detailData.deliveryStatus)" size="small">{{ statusLabel(detailData.deliveryStatus) }}</el-tag>
              </td>
            </tr>
            <tr>
              <td class="dt-label">交货期</td><td class="dt-value">{{ detailData.deliveryDate || '-' }}</td>
              <td class="dt-label">结单日期</td><td class="dt-value">{{ detailData.actualCloseDate || '-' }}</td>
            </tr>
            <tr v-if="detailData.remark">
              <td class="dt-label">备注</td><td class="dt-value" colspan="3">{{ detailData.remark }}</td>
            </tr>
          </table>
        </div>
        <div class="detail-section">
          <div class="section-title">物料明细</div>
          <el-table :data="detailData.items || []" border size="small" style="width:100%">
            <el-table-column label="物料名称" prop="materialName" min-width="140" />
            <el-table-column label="规格" prop="spec" width="120" />
            <el-table-column label="单位" prop="unit" width="60" />
            <el-table-column label="数量" prop="quantity" width="80" align="right" />
            <el-table-column label="单价" width="100" align="right">
              <template slot-scope="s">¥{{ s.row.unitPrice }}</template>
            </el-table-column>
            <el-table-column label="小计" width="100" align="right">
              <template slot-scope="s">¥{{ s.row.totalPrice }}</template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div slot="footer">
        <el-button @click="detailOpen = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClient, getClient, addClient, updateClient, delClient, getClientOrders } from "@/api/bid/client"
import { getDelivery } from "@/api/bid/delivery"

export default {
  name: "Client",
  data() {
    return {
      // ── Tab ──
      activeTab: "list",

      // ── 客户列表 ──
      loading: false,
      clientList: [],
      total: 0,
      queryParams: { pageNum: 1, pageSize: 20, clientName: "" },

      // ── 新增/编辑 ──
      dialogOpen: false,
      dialogTitle: "",
      form: { grade: "B", status: "0" },
      rules: {
        clientNo: [{ required: true, message: "客户编号不能为空", trigger: "blur" }],
        clientName: [{ required: true, message: "客户名称不能为空", trigger: "blur" }]
      },
      editId: null,

      // ── 历史发货单 ──
      orderClientId: null,
      orderClientName: "",
      orderLoading: false,
      orderList: [],
      clientOptions: [],

      // ── 发货单详情 ──
      detailOpen: false,
      detailData: null
    }
  },
  created() {
    this.getList()
    this.loadClientOptions()
  },
  methods: {
    // ═══════════ 客户列表 ═══════════
    getList() {
      this.loading = true
      listClient(this.queryParams).then(r => {
        this.clientList = r.rows || []
        this.total = r.total || 0
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleSearch() {
      this.queryParams.pageNum = 1
      this.getList()
      this.loadClientOptions()
    },

    // ═══════════ 新增/编辑 ═══════════
    handleAdd() {
      this.editId = null
      this.form = { grade: "B", status: "0", clientNo: "", clientName: "", contact: "", phone: "", email: "", city: "", address: "", remark: "" }
      this.dialogTitle = "新增客户"
      this.dialogOpen = true
    },
    handleEdit(row) {
      this.editId = row.clientId
      this.form = { ...row }
      this.dialogTitle = "编辑客户"
      this.dialogOpen = true
    },
    cancelDialog() {
      this.dialogOpen = false
      this.$refs.form && this.$refs.form.clearValidate()
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        const action = this.editId ? updateClient(this.form) : addClient(this.form)
        action.then(() => {
          this.$modal.msgSuccess(this.editId ? "修改成功" : "新增成功")
          this.dialogOpen = false
          this.getList()
        }).catch(() => {})
      })
    },

    // ═══════════ 删除 ═══════════
    handleDelete(row) {
      this.$modal.confirm('确认删除客户 "' + row.clientName + '"？').then(() => {
        delClient(row.clientId).then(() => {
          this.$modal.msgSuccess("删除成功")
          this.getList()
        })
      }).catch(() => {})
    },

    // ═══════════ 历史发货单 ═══════════
    loadClientOptions() {
      listClient({ pageNum: 1, pageSize: 999 }).then(r => {
        this.clientOptions = r.rows || []
      }).catch(() => {})
    },
    loadClientOrders(clientId) {
      if (!clientId) {
        this.orderList = []
        this.orderClientName = ""
        return
      }
      this.orderLoading = true
      this.orderList = []
      // 获取客户名用于显示
      const c = this.clientOptions.find(o => o.clientId === clientId)
      this.orderClientName = c ? c.clientName : ""
      getClientOrders(clientId).then(r => {
        this.orderList = (r.data || []).map(o => ({
          ...o,
          totalAmount: o.totalAmount || o.total_amount,
          deliveryDate: o.deliveryDate || o.delivery_date,
          actualCloseDate: o.actualCloseDate || o.actual_close_date,
          deliveryStatus: o.deliveryStatus || o.delivery_status,
          itemCount: o.itemCount || o.item_count
        }))
        this.orderLoading = false
      }).catch(() => { this.orderLoading = false })
    },

    // ═══════════ 发货单详情 ═══════════
    showOrderDetail(row) {
      getDelivery(row.doId || row.do_id).then(r => {
        this.detailData = r.data
        this.detailOpen = true
      }).catch(() => {})
    },

    // ═══════════ 工具方法 ═══════════
    statusType(s) {
      return { pending: "warning", transit: "primary", history: "success" }[s] || ""
    },
    statusLabel(s) {
      return { pending: "待发", transit: "在途", history: "已收货" }[s] || s || "-"
    }
  }
}
</script>

<style scoped>
.app-container { background: #fff; padding: 20px; border-radius: 4px; min-height: calc(100vh - 104px); }
.toolbar { margin-bottom: 16px; display: flex; align-items: center; gap: 8px; }
.detail-card { padding: 0; }
.detail-section { margin-bottom: 20px; }
.section-title { font-size: 14px; font-weight: 700; color: #1a2c4e; margin-bottom: 10px; padding-left: 8px; border-left: 4px solid #1171c4; }
.detail-table { width: 100%; border-collapse: collapse; }
.detail-table td { padding: 8px 12px; border: 1px solid #e4e7ed; }
.dt-label { background: #f5f7fa; color: #606266; font-weight: 600; width: 90px; font-size: 12px; }
.dt-value { color: #303133; font-size: 13px; }
</style>
