<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="order_form">
      <el-form-item label="订单编号" prop="F_OrderList" >
        <el-input v-model="ruleForm.F_OrderList" placeholder="请输入订单编号" disabled="false" />
      </el-form-item>
      <el-form-item label="合并类型" prop="F_Type" >
        <el-select v-model="ruleForm.F_Type" class="form_select" filterable clearable placeholder="请选择合并类型">
          <el-option v-for="item in mergeType" :key="item.F_Value" :label="item.F_Value" :value="item.F_Value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="赠送商品" prop="F_Gift">
        <el-input v-model="ruleForm.F_Gift" placeholder="请输入赠送商品"/>
      </el-form-item>
      <el-form-item label="减免金额" prop="F_Money">
        <el-input v-model="ruleForm.F_Money" placeholder="请输入减免金额"/>
      </el-form-item>
      <el-form-item label="客服备注" prop="F_Remark">
        <el-input :autosize="{ minRows: 4}" v-model="ruleForm.F_Remark" type="textarea" placeholder="请输入客服备注"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(0)">保存</el-button>
        <el-button type="primary" @click="submitForm(1)">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getDictionaryValueList } from '@/api/commonAPI'
import { addConsolidateOrderList } from "@/api/orderManagement/consolidateOrderList";

export default {
  name: "Merge",
  props: {
    rowid: {
      type: Array,
      default: []
    },
    layerid: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      mergeType: [],//订单类型
      ruleForm: {
        F_Id: '',
        F_OrderList: '', //合并订单
        F_Type: '', //合并类型
        F_Gift: '', //赠送商品
        F_Money: '', //减免金额
        F_Remark: '', //销售备注
        F_state: '', //订单状态
      },
      rules: {
        order1: [{
          required: true,
          trigger: 'blur',
          message: '请输入订单编号1',
        }],
        order2: [{
          required: true,
          trigger: 'blur',
          message: '请输入订单编号2',
        }],
        F_Type: [{
          required: true,
          trigger: 'change',
          message: '请选择合并类型',
        }],
        F_Gift: [{
          required: true,
          trigger: 'blur',
          message: '请输入赠送商品',
        }],
        F_Money: [{
          required: true,
          trigger: 'blur',
          message: '请输入减免金额',
        }],
      },
      loading: false
    };
  },
  created() {
    this.getMergeType()
    Promise.all([
    ]).then(() => {
      if (this.rowid) {
        this.ruleForm.F_OrderList = this.rowid.join(',').replace(/\s+/g, '')
      }
    });
  },
  methods: {
    submitForm(state) {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.ruleForm.F_state = state
          //添加
          addConsolidateOrderList(this.ruleForm).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getList() // 重新加载父级数据
              this.$message.success('恭喜你，添加成功!')
            }
          }).catch(() => {
            this.loading = false
          })
          return
        } else {
          this.$message.error("请输入有效的必填项信息!");
          return false;
        }
      });
    },
    // 获取合并类型
    getMergeType() {
      return new Promise(resolve=>{
        const params={
          isleaf:true,
          code:'MERGETYPE'
        }
        getDictionaryValueList(params).then(response=>{
          if(response.state.toLowerCase() === 'success'){
            this.mergeType = response.data.Rows
          }
        })
      })
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.order_form .form_select {
  width: 100%;
}
</style>
