<template>
  <div>
    <el-descriptions title="订单明细">
    </el-descriptions>
    <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-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="!currentRow.detailId"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="!currentRow.detailId"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border :data="deliveryPlanDetailList" highlight-current-row @row-click="handleRowClick">
      <!-- <el-table-column label="订单ID" align="center" prop="orderId" /> -->
      <el-table-column label="订单详情" align="center" prop="orderDetail" width="150px" show-overflow-tooltip/>
      <!-- <el-table-column label="客户ID" align="center" prop="customerId" /> -->
      <el-table-column label="客户名称" align="center" prop="customerDetail" width="150px" show-overflow-tooltip/>
      <el-table-column label="地址" align="center" prop="address" width="150px" show-overflow-tooltip/>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @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-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="orderId">
          <!-- 支出远程搜索的下拉选 -->
          <!-- <el-input v-model="form.orderId" placeholder="请输入订单ID" /> -->
          <el-select v-model="form.orderId" placeholder="选择订单后自动填写其他信息" filterable @change="handleOrderChange">
            <el-option
              v-for="item in orderList"
              :key="item.orderId"
              :label="item.orderCode"
              :value="item.orderId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="订单详情" prop="orderDetail">
          <el-input v-model="form.orderDetail" placeholder="请输入内容" />
        </el-form-item>
        <!-- 这个不显示 -->
        <!-- <el-form-item label="客户ID" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入客户ID" />
        </el-form-item> -->
        <el-form-item label="客户详情" prop="customerDetail">
          <el-input v-model="form.customerDetail" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeliveryPlanDetail, getDeliveryPlanDetail, delDeliveryPlanDetail, addDeliveryPlanDetail, updateDeliveryPlanDetail } from "@/api/wms/deliveryPlanDetail";
import { listOrder } from '@/api/crm/order'

export default {
  name: "DeliveryPlanDetail",
  props: {
    planId: {
      type: String,
      default: undefined
    }
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发货计划明细表格数据
      deliveryPlanDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planId: this.planId,
        orderId: undefined,
        orderDetail: undefined,
        customerId: undefined,
        customerDetail: undefined,
        address: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      orderList: [],
      currentRow: {}
    };
  },
  created() {
    // this.getList();
    this.getOrder();
  },
  watch: {
    planId: {
      handler(newVal, oldVal) {
        if (newVal !== oldVal) {
          this.queryParams.planId = newVal;
          this.getList();
          this.currentRow = {};
        }
      },
      immediate: true
    }
  },
  methods: {
    /** 查询发货计划明细列表 */
    getList() {
      this.loading = true;
      listDeliveryPlanDetail(this.queryParams).then(response => {
        this.deliveryPlanDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getOrder() {
      listOrder({
        orderId: this.form.orderId,
        pageNum: 1,
        pageSize: 999,
        orderType: 1
      }).then(response => {
        this.orderList = response.rows;
      })
    },
    handleRowClick(row) {
      this.currentRow = row;
      // this.form = {
      //   detailId: row.detailId,
      //   planId: this.planId,
      //   orderId: row.orderId,
      //   orderDetail: row.orderDetail,
      //   customerId: row.customerId,
      //   customerDetail: row.customerDetail,
      //   address: row.address,
      //   remark: row.remark,
      //   delFlag: row.delFlag,
      //   createTime: row.createTime,
      //   createBy: row.createBy,
      //   updateTime: row.updateTime,
      //   updateBy: row.updateBy
      // };
    },
    handleOrderChange(orderId) {
      console.log(orderId);
      // 查找改订单
      const order = this.orderList.find(item => item.orderId === orderId);
      console.log(order);
      if (order) {
        this.form.orderDetail = order.orderCode;
        this.form.customerId = order.customerId;
        this.form.customerDetail = order.customerCode;
        this.form.address = order.address;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        detailId: undefined,
        planId: this.planId,
        orderId: undefined,
        orderDetail: undefined,
        customerId: undefined,
        customerDetail: undefined,
        address: undefined,
        remark: undefined,
        delFlag: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.detailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加发货计划明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const detailId = row.detailId || this.currentRow.detailId;
      getDeliveryPlanDetail(detailId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改发货计划明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.detailId != null) {
            updateDeliveryPlanDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addDeliveryPlanDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const detailIds = row.detailId || this.currentRow.detailId;
      this.$modal.confirm('是否确认删除发货计划明细编号为"' + detailIds + '"的数据项？').then(() => {
        this.loading = true;
        return delDeliveryPlanDetail(detailIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
  }
};
</script>
