<template>
  <div>
    <el-empty v-if="!orderId || orderId == ''" description="未选中订单" />

    <!-- <el-empty v-else-if="!orderItemList.length" description="暂无订单明细" /> -->

    <div v-else>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-if="editable">新增</el-button>
          <el-button type="primary" plain icon="el-icon-refresh" size="mini" @click="getList">刷新</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="orderItemList">
        <el-table-column label="产品类型" align="center" prop="productType" />
        <el-table-column label="规格要求" align="center" prop="specRequire" />
        <el-table-column label="产品数量" align="center" prop="productNum" />
        <el-table-column label="特殊要求" align="center" prop="specialRequire" />
        <el-table-column label="明细金额" align="center" prop="itemAmount" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="editable">
          <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" />
    </div>

    <!-- 添加或修改正式订单明细对话框 -->
    <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="productType">
          <el-input v-model="form.productType" placeholder="请输入产品类型" />
        </el-form-item>
        <el-form-item label="产品数量" prop="productNum">
          <el-input v-model="form.productNum" placeholder="请输入产品数量" />
        </el-form-item>
         <el-form-item label="明细金额" prop="itemAmount">
          <el-input size="mini" v-model="form.itemAmount" placeholder="请输入明细金额" />
        </el-form-item>
         <el-form-item label="规格要求" prop="specRequire">
          <el-input v-model="form.specRequire" placeholder="请输入规格要求" />
        </el-form-item>
        <el-form-item label="特殊要求" prop="specialRequire">
          <el-input v-model="form.specialRequire" type="textarea" 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 { listOrderItem, getOrderItem, delOrderItem, addOrderItem, updateOrderItem } from "@/api/crm/orderItem";
import { actions, ORDER_ACTIONS } from "../js/actions";

export default {
  name: "OrderItem",
  props: {
    orderId: {
      required: true
    },
    editable: {
      type: Boolean,
      default: true
    },
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 正式订单明细表格数据
      orderItemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: undefined,
        productType: undefined,
        specRequire: undefined,
        productNum: undefined,
        specialRequire: undefined,
        itemAmount: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productType: [
          { required: true, message: "请输入产品类型", trigger: "blur" }
        ],
        productNum: [
          { required: true, message: "请输入产品数量", trigger: "blur" }
        ],
        itemAmount: [
          { required: true, message: "请输入明细金额", trigger: "blur" },
          // 必须是数字，最多两位小数，使用自定义的校验规则
          { validator: (rule, value, callback) => {
            if (!/^\d+(\.\d{1,2})?$/.test(value)) {
              callback(new Error("请输入最多两位小数的数字"));
            } else {
              callback();
            }
          }, trigger: "input" }
        ],
      }
    };
  },
  watch: {
    orderId: {
      handler(newVal, oldVal) {
        // if (newVal !== oldVal) {
          this.queryParams.orderId = newVal;
          this.getList();
        // }
      },
      immediate: true
    }
  },
  methods: {
    /** 查询正式订单明细列表 */
    getList() {  
      if (!this.queryParams.orderId) {
        this.orderItemList = [];
        this.total = 0;
        return;
      }
      this.loading = true;
      listOrderItem(this.queryParams).then(response => {
        this.orderItemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itemId: undefined,
        orderId: this.orderId,
        productType: undefined,
        specRequire: undefined,
        productNum: undefined,
        specialRequire: undefined,
        itemAmount: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: 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.itemId)
      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 itemId = row.itemId || this.ids
      getOrderItem(itemId).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.itemId != null) {
            actions[ORDER_ACTIONS.updateOrderdetail].handler(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            actions[ORDER_ACTIONS.createOrderdetail].handler(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const itemIds = row.itemId || this.ids;
      this.$modal.confirm('是否确认删除正式订单明细编号为"' + itemIds + '"的数据项？').then(() => {
        this.loading = true;
        return delOrderItem(itemIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/orderItem/export', {
        ...this.queryParams
      }, `orderItem_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
