<template>
  <div>
    <el-form ref="form" :model="formData" :rules="rules" label-width="60px" :disabled="readonly">
      <el-collapse v-model="activeNames" :accordion="false">
        <!-- 订单信息组 -->
        <el-collapse-item title="订单信息" name="1">
          <template slot="title">
            <!-- 在这里选择订单后快速填写相关信息 -->
            <span>订单信息</span>
             <el-button @click.stop="openOrderDialog" style="margin-left: 10px;" plain :type="formData.orderId ? 'success' : 'default'">
               {{ formData.orderId ? formData.orderCode : '选择订单' }}
             </el-button>
          </template>
          <el-row>
            <el-col :span="12">
              <el-form-item label="订单号" prop="orderCode">
                <el-input v-model="formData.orderCode" placeholder="请输入订单号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="合同号" prop="contractCode">
                <el-input v-model="formData.contractCode" placeholder="请输入合同号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户" prop="customerName">
                <el-input v-model="formData.customerName" placeholder="请输入客户名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="业务员" prop="salesman">
                <el-input v-model="formData.salesman" placeholder="请输入业务员姓名" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>

        <!-- 原料信息组 -->
        <el-collapse-item title="原料信息" name="2">
          <el-row>
            <el-col :span="12">
              <el-form-item label="厂家" prop="rawManufacturer">
                <el-input v-model="formData.rawManufacturer" placeholder="请输入原料厂家" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="材质" prop="rawMaterial">
                <el-input v-model="formData.rawMaterial" placeholder="请输入原料材质" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="厚度" prop="rawThick">
                <el-input v-model="formData.rawThick" placeholder="请输入原料厚度(mm)" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="宽度" prop="rawWidth">
                <el-input v-model="formData.rawWidth" placeholder="请输入原料宽度(mm)" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>

        <!-- 成品信息组 -->
        <el-collapse-item title="成品信息" name="3">
          <el-row>
            <el-col :span="12">
              <el-form-item label="成品名称" prop="productName">
                <el-input v-model="formData.productName" placeholder="请输入成品名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="材质" prop="productMaterial">
                <el-input v-model="formData.productMaterial" placeholder="请输入成品材质" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="镀层(g)" prop="coatingG">
                <el-input v-model="formData.coatingG" placeholder="请输入镀层重量" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="成品宽度" prop="productWidth">
                <el-input v-model="formData.productWidth" placeholder="请输入成品宽度" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="轧制厚度" prop="rollingThick">
                <el-input v-model="formData.rollingThick" placeholder="请输入轧制厚度" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="标签厚度" prop="markCoatThick">
                <el-input v-model="formData.markCoatThick" placeholder="请输入标签厚度" />
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="吨钢长度区间" prop="tonSteelLengthRange">
                <el-input v-model="formData.tonSteelLengthRange" placeholder="请输入吨钢长度区间(m)" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="数量" prop="planQty">
                <el-input v-model="formData.planQty" placeholder="请输入成品数量" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="重量" prop="planWeight">
                <el-input v-model="formData.planWeight" placeholder="请输入成品重量" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="表面处理" prop="surfaceTreatment">
                <MemoInput storageKey="surfaceTreatmentDesc" v-model="formData.surfaceTreatment"
                  placeholder="请输入表面处理" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="切边要求" prop="widthReq">
                <el-select v-model="formData.widthReq" placeholder="请输入切边要求" style="width: 100%">
                  <el-option label="净边料" value="净边料" />
                  <el-option label="毛边料" value="毛边料" />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="包装要求" prop="productPackaging">
                <el-select v-model="formData.productPackaging" placeholder="请选择包装要求" style="width: 100%">
                  <el-option label="裸包" value="裸包" />
                  <el-option label="普包" value="普包" />
                  <el-option label="简包" value="简包" />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="宽度要求" prop="productEdgeReq">
                <el-input v-model="formData.productEdgeReq" placeholder="请输入宽度要求" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="用途" prop="usageReq">
                <el-input v-model="formData.usageReq" placeholder="请输入成品用途" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>

        <!-- 其他信息组 -->
        <el-collapse-item title="生产信息" name="4">
          <el-row>
            <el-col :span="12">
              <el-form-item label="开始时间" prop="startTime">
                <el-date-picker clearable v-model="formData.startTime" style="width: 100%" type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择生产开始时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结束时间" prop="endTime">
                <el-date-picker clearable v-model="formData.endTime" style="width: 100%" type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择生产结束时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="后处理" prop="postProcess">
                <el-input v-model="formData.postProcess" placeholder="请输入后处理要求" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="下工序" prop="nextProcess">
                <el-input v-model="formData.nextProcess" placeholder="请输入下工序" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="取样" prop="sampleReq">
                <el-input v-model="formData.sampleReq" placeholder="请输入取样要求" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="序号" prop="bizSeqNo">
                <el-input v-model="formData.bizSeqNo" placeholder="请输入序号" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注信息" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </el-form>

    <el-dialog title="选择订单" :visible.sync="dialogVisible" width="80%" append-to-body>
      <div class="order-dialog-content">
        <!-- 筛选条件 -->
        <el-form :model="orderQueryParams" ref="orderQueryForm" size="small" :inline="true" label-width="80px">
          <el-form-item label="合同号">
            <el-select v-model="orderQueryParams.contractId" placeholder="请选择合同">
              <el-option
                v-for="contract in contractList"
                :key="contract.contractId"
                :label="contract.contractNo"
                :value="contract.contractId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="订单号">
            <el-input v-model="orderQueryParams.orderCode" placeholder="请输入订单号" style="width: 180px" />
          </el-form-item>
          <el-form-item label="客户">
            <el-input v-model="orderQueryParams.customerName" placeholder="请输入客户名称" style="width: 180px" />
          </el-form-item>
          <el-form-item label="业务员">
            <el-input v-model="orderQueryParams.salesman" placeholder="请输入业务员" style="width: 120px" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="getOrderList">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetOrderQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 订单列表 -->
        <el-table v-loading="orderLoading" :data="orderList" style="width: 100%" @row-click="handleOrderSelect">
          <el-table-column type="selection" width="55" />
          <el-table-column prop="orderCode" label="订单号" width="150" />
          <el-table-column prop="orderType" label="订单类型" width="100" />
          <el-table-column prop="contractCode" label="合同号" width="150" />
          <el-table-column prop="companyName" label="客户" width="180" />
          <el-table-column prop="salesman" label="业务员" width="100" />
          <el-table-column prop="orderAmount" label="订单金额" width="120" align="right" />
          <el-table-column prop="deliveryDate" label="交货日期" width="150" />
          <el-table-column prop="createTime" label="创建时间" width="180" />
          <el-table-column label="操作" width="80" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click.stop="selectOrder(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
          <el-pagination
            background
            layout="prev, pager, next, jumper"
            :total="orderTotal"
            :page-size="orderQueryParams.pageSize"
            :current-page.sync="orderQueryParams.pageNum"
            @current-change="getOrderList"
          />
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder } from '@/api/crm/order';
import { listContract } from '@/api/crm/contract';

export default {
  name: "PlanDetailForm",
  props: {
    value: {
      type: Object,
      default: () => ({})
    },
    planSheetId: {
      type: String,
      required: true
    },
    readonly: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    formData: {
      get() {
        return this.value || {};
      },
      set(newVal) {
        const data = { ...newVal, planSheetId: this.planSheetId };
        this.$emit('input', data);
        this.$emit('change', data);
      }
    }
  },
  data() {
    return {
      // 折叠面板默认展开项（全部打开）
      activeNames: ['1', '2', '3', '4'],
      // 表单校验
      rules: {
        planSheetId: [
          { required: true, message: "关联排产单ID不能为空", trigger: "blur" }
        ]
      },
      // 订单列表
      orderList: [],
      // 订单总数
      orderTotal: 0,
      // 订单加载状态
      orderLoading: false,
      // 合同列表
      contractList: [],
      // 弹窗是否可见
      dialogVisible: false,
      // 订单查询参数
      orderQueryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: undefined,
        orderCode: undefined,
        orderType: undefined,
        customerId: undefined,
        orderAmount: undefined,
        salesman: undefined,
        deliveryDate: undefined,
        contractId: undefined,
        contractCode: undefined,
        customerName: undefined
      }
    };
  },
  methods: {
    /** 表单验证 */
    validate(callback) {
      return this.$refs.form.validate(callback);
    },
    reset() {
      this.$refs["form"].resetFields();
    },
    /** 打开订单选择弹窗 */
    openOrderDialog() {
      this.dialogVisible = true;
      // 加载合同列表
      this.getContractList();
      // 加载订单列表
      this.getOrderList();
    },
    /** 获取合同列表 */
    getContractList() {
      listContract().then(response => {
        this.contractList = response.rows;
      });
    },
    /** 获取订单列表 */
    getOrderList() {
      this.orderLoading = true;
      listOrder(this.orderQueryParams).then(response => {
        this.orderList = response.rows;
        this.orderTotal = response.total;
        this.orderLoading = false;
      });
    },
    /** 重置订单查询参数 */
    resetOrderQuery() {
      this.orderQueryParams = {
        pageNum: 1,
        pageSize: 10,
        orderId: undefined,
        orderCode: undefined,
        orderType: undefined,
        customerId: undefined,
        orderAmount: undefined,
        salesman: undefined,
        deliveryDate: undefined,
        contractId: undefined,
        contractCode: undefined,
        customerName: undefined
      };
      this.getOrderList();
    },
    /** 选择订单 */
    selectOrder(row) {
      this.formData.orderCode = row.orderCode;
      this.formData.contractCode = row.contractCode;
      this.formData.customerName = row.companyName;
      this.formData.salesman = row.salesman;
      this.dialogVisible = false;
    },
    /** 处理订单行点击 */
    handleOrderSelect(row, event, column) {
      // 点击操作列时不触发选择
      if (column && column.fixed === 'right') {
        return;
      }
      this.selectOrder(row);
    }
  }
};
</script>

<style scoped>
.order-dialog-content {
  padding: 10px 0;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>