<template>
  <div class="customer-css">
    <el-form :inline="true" label-position="top" :model="searchData">
      <el-form-item label="物料编码">
        <el-input v-model="searchData.partNo" clearable style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="物料名称">
        <el-input v-model="searchData.partDesc" clearable style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-input v-model="searchData.remark1" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" @click="searchTable()">查询</el-button>
        <el-button type="primary" @click="addModal()">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      :height="searchData.height"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        v-for="(item, index) in columnList"
        :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed == '' ? false : item.fixed"
        :min-width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span v-if="!item.columnHidden">{{ scope.row[item.columnProp] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="180"
        fixed="right"
        label="Actions">
        <template slot-scope="scope">
          <a type="text" size="small" @click="editModel(scope.row)">Edit |</a>
          <a type="text" size="small" @click="deleteData(scope.row)"> Delete</a>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑弹窗 -->
    <el-dialog :title="dialogTitle" :close-on-click-modal="false" v-drag :visible.sync="dialogVisible" width="900px">
      <div class="dialog-content">
        <el-form label-position="top" :model="formData" :rules="formRules" ref="formRef">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="物料编码" prop="partNo">
                <el-input v-model="formData.partNo" placeholder="请输入物料编码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="物料名称" prop="partDesc">
                <el-input v-model="formData.partDesc" placeholder="请输入物料名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="计量单位" prop="umid">
                <el-input v-model="formData.umid" placeholder="请输入计量单位"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="询价数量" prop="qty">
                <el-input v-model="formData.qty" type="number" placeholder="请输入询价数量"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="状态" prop="remark1">
                <el-select v-model="formData.remark1" placeholder="请选择状态" clearable style="width: 100%">
                  <el-option label="询价中" value="询价中"></el-option>
                  <el-option label="报价中" value="报价中"></el-option>
                  <el-option label="已完成" value="已完成"></el-option>
                  <el-option label="已取消" value="已取消"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="材料费用" prop="materialCost">
                <el-input v-model="formData.materialCost" type="number" placeholder="请输入材料费用"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="加工费" prop="produceFee">
                <el-input v-model="formData.produceFee" type="number" placeholder="请输入加工费"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="表面处理费" prop="surfaceFee">
                <el-input v-model="formData.surfaceFee" type="number" placeholder="请输入表面处理费"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="利润" prop="profit">
                <el-input v-model="formData.profit" type="number" placeholder="请输入利润"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="未税单价" prop="price">
                <el-input v-model="formData.price" type="number" placeholder="请输入未税单价"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="税率(%)" prop="taxRate">
                <el-input v-model="formData.taxRate" type="number" placeholder="请输入税率"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="含税单价" prop="tax">
                <el-input v-model="formData.tax" type="number" placeholder="请输入含税单价"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="关税" prop="duty">
                <el-input v-model="formData.duty" type="number" placeholder="请输入关税"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="采购周期(天)" prop="purchaseCycle">
                <el-input v-model="formData.purchaseCycle" type="number" placeholder="请输入采购周期"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="模具费" prop="mouldFee">
                <el-input v-model="formData.mouldFee" type="number" placeholder="请输入模具费"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="附加费用" prop="additionalCost">
                <el-input v-model="formData.additionalCost" type="number" placeholder="请输入附加费用"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="运费" prop="freight">
                <el-input v-model="formData.freight" type="number" placeholder="请输入运费"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="报价日期" prop="createdDate">
                <el-date-picker v-model="formData.createdDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" style="width: 100%"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" :rows="3" v-model="formData.remark" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="margin-top: 20px">
        <el-button type="primary" @click="submitData()">保存</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 请根据实际API路径调整导入
// import {
//   searchQuotationList,
//   createQuotation,
//   updateQuotation,
//   deleteQuotation
// } from '@/api/quote/quotation'

export default {
  name: 'QuotationRecord',
  data() {
    return {
      dataList: [],
      dataListLoading: false,
      searchData: {
        partNo: '',
        partDesc: '',
        remark1: '',
        height: '200',
        page: 1,
        limit: 1000
      },
      dialogVisible: false,
      dialogTitle: '新增报价记录',
      formData: {
        id: '',
        partNo: '',
        partDesc: '',
        umid: '',
        qty: '',
        remark1: '',
        materialCost: '',
        produceFee: '',
        surfaceFee: '',
        profit: '',
        price: '',
        taxRate: '',
        tax: '',
        duty: '',
        purchaseCycle: '',
        mouldFee: '',
        additionalCost: '',
        freight: '',
        createdDate: '',
        remark: '',
        createdBy: ''
      },
      formRules: {
        partNo: [{ required: true, message: '请输入物料编码', trigger: 'blur' }]
      },
      columnList: [
        { columnProp: 'partNo', headerAlign: 'center', align: 'left', columnLabel: '物料编码', columnWidth: 130, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'partDesc', headerAlign: 'center', align: 'left', columnLabel: '物料名称', columnWidth: 150, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'umid', headerAlign: 'center', align: 'center', columnLabel: '计量单位', columnWidth: 90, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'qty', headerAlign: 'center', align: 'right', columnLabel: '询价数量', columnWidth: 100, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'remark1', headerAlign: 'center', align: 'center', columnLabel: '状态', columnWidth: 100, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'materialCost', headerAlign: 'center', align: 'right', columnLabel: '材料费用', columnWidth: 100, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'produceFee', headerAlign: 'center', align: 'right', columnLabel: '加工费', columnWidth: 100, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'surfaceFee', headerAlign: 'center', align: 'right', columnLabel: '表面处理费', columnWidth: 110, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'profit', headerAlign: 'center', align: 'right', columnLabel: '利润', columnWidth: 90, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'price', headerAlign: 'center', align: 'right', columnLabel: '未税单价', columnWidth: 100, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'taxRate', headerAlign: 'center', align: 'right', columnLabel: '税率', columnWidth: 80, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'tax', headerAlign: 'center', align: 'right', columnLabel: '含税单价', columnWidth: 100, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'duty', headerAlign: 'center', align: 'right', columnLabel: '关税', columnWidth: 90, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'purchaseCycle', headerAlign: 'center', align: 'center', columnLabel: '采购周期', columnWidth: 100, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'mouldFee', headerAlign: 'center', align: 'right', columnLabel: '模具费', columnWidth: 100, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'additionalCost', headerAlign: 'center', align: 'right', columnLabel: '附加费用', columnWidth: 100, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'freight', headerAlign: 'center', align: 'right', columnLabel: '运费', columnWidth: 90, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'createdDate', headerAlign: 'center', align: 'center', columnLabel: '报价日期', columnWidth: 120, fixed: '', columnHidden: false, columnSortable: true },
        { columnProp: 'remark', headerAlign: 'center', align: 'left', columnLabel: '备注', columnWidth: 180, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'createdBy', headerAlign: 'center', align: 'center', columnLabel: '最近更改人', columnWidth: 120, fixed: '', columnHidden: false, columnSortable: false }
      ]
    }
  },
  methods: {
    // 初始化方法，供父组件调用
    init(inData) {
      if (inData) {
        this.searchData = { ...this.searchData, ...inData }
      }
      this.searchTable()
    },
    // 查询列表
    searchTable() {
      this.dataListLoading = true
      // TODO: 请替换为实际接口调用
      // searchQuotationList(this.searchData).then(({ data }) => {
      //   if (data && data.code === 0) {
      //     this.dataList = data.rows
      //   } else {
      //     this.dataList = []
      //   }
      //   this.dataListLoading = false
      // })
      
      // 模拟数据示例，实际使用时请删除
      setTimeout(() => {
        this.dataList = []
        this.dataListLoading = false
      }, 500)
    },
    // 新增
    addModal() {
      this.dialogTitle = '新增报价记录'
      this.formData = {
        id: '',
        partNo: '',
        partDesc: '',
        umid: '',
        qty: '',
        remark1: '',
        materialCost: '',
        produceFee: '',
        surfaceFee: '',
        profit: '',
        price: '',
        taxRate: '',
        tax: '',
        duty: '',
        purchaseCycle: '',
        mouldFee: '',
        additionalCost: '',
        freight: '',
        createdDate: '',
        remark: '',
        createdBy: (this.$store && this.$store.state && this.$store.state.user && this.$store.state.user.name) || ''
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        if (this.$refs.formRef) {
          this.$refs.formRef.clearValidate()
        }
      })
    },
    // 编辑
    editModel(row) {
      this.dialogTitle = '编辑报价记录'
      this.formData = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
      this.$nextTick(() => {
        if (this.$refs.formRef) {
          this.$refs.formRef.clearValidate()
        }
      })
    },
    // 提交保存
    submitData() {
      this.$refs.formRef.validate((valid) => {
        if (!valid) return
        
        // TODO: 请替换为实际接口调用
        // const api = this.formData.id ? updateQuotation : createQuotation
        // api(this.formData).then(({ data }) => {
        //   if (data && data.code === 0) {
        //     this.$message.success(data.msg)
        //     this.dialogVisible = false
        //     this.searchTable()
        //   } else {
        //     this.$message.warning(data.msg)
        //   }
        // })
        
        // 模拟保存，实际使用时请删除
        this.$message.success('保存成功')
        this.dialogVisible = false
        this.searchTable()
      })
    },
    // 删除
    deleteData(row) {
      this.$confirm('确认删除该条报价记录吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // TODO: 请替换为实际接口调用
        // deleteQuotation(row.id).then(({ data }) => {
        //   if (data.code === 0) {
        //     this.$message.success('删除成功')
        //     this.searchTable()
        //   }
        // })
        
        // 模拟删除，实际使用时请删除
        this.$message.success('删除成功')
        this.searchTable()
      }).catch(() => {})
    }
  }
}
</script>

<style scoped lang="scss">
.dialog-content {
  width: 100%;
  max-height: 60vh;
  overflow-y: auto;
  padding-right: 10px;
}

.customer-css {
  padding: 10px;
}
</style>