<template>
  <el-drawer
    title="创建订单"
    direction="ltr"
    size="40%"
    :visible="controlWindows.create"
    @opened="openDrawer"
    :before-close="closeWindow"
  >
    <div class="create">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客户名称" prop="productName">
          <el-input
            maxlength="50"
            v-model="form.productName"
            placeholder="请输入客户名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="炼厂名称" prop="refineryId">
          <el-select v-model="form.refineryId" placeholder="请选择炼厂">
            <el-option
              v-for="(item, index) in refineryList"
              :key="index"
              :label="item.refineryName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品类型" prop="productType">
          <el-select v-model="form.productType" placeholder="请选择产品类型">
            <el-option
              v-for="(item, index) in productTypeList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input
            maxlength="50"
            v-model="form.productName"
            placeholder="请输入产品名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="销售价" prop="productName">
          <el-input
            maxlength="50"
            v-model="form.productName"
            placeholder="请输入销售价"
          ></el-input>
          元/吨
        </el-form-item>
        <el-form-item label="预约提货量" prop="productName">
          <el-input
            maxlength="50"
            v-model="form.productName"
            placeholder="请输入预约提货量"
          ></el-input>
          吨
        </el-form-item>
      </el-form>
    </div>
    <div class="buttons">
      <el-button @click="controlWindows.create = false">取消 </el-button>
      <el-button @click="submit">确定</el-button>
    </div>
  </el-drawer>
</template>

<script>
import serve from "api/product.js";
import refineryServe from "api/refineryInfo.js";
export default {
  props: {
    controlWindows: Object,
    // refineryTypeEnum: Array,
  },
  data() {
    return {
      form: {},
      refineryList: [],
      productTypeList: [
        {
          label: "0#柴",
          value: "0#",
        },
        {
          label: "-10#柴",
          value: "-10#",
        },
        {
          label: "92#汽",
          value: "92#",
        },
        {
          label: "95#汽",
          value: "95#",
        },
      ],
      rules: {
        productName: [
          { required: true, message: "请输入产品名称", trigger: "blur" },
        ],
        refineryId: [
          { required: true, message: "请选择炼厂", trigger: "change" },
        ],
        enableMark: [
          { required: true, message: "请选择启用状态", trigger: "change" },
        ],
        productType: [
          { required: true, message: "请选择产品类型", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.findByEntity();
  },
  methods: {
    openDrawer() {},
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.judgeInterface(this.form).then((res) => {
            if (res.code === 20000) {
              this.$message.success(res.msg);
              this.closeWindow();
            }
          });
        }
      });
    },
    judgeInterface(form) {
      let { id } = this.controlWindows.addInfo;
      if (id) return serve.update(form);
      else return serve.save(form);
    },
    //炼厂list
    findByEntity() {
      refineryServe.findByEntity().then((res) => {
        this.refineryList = res.data;
      });
    },
    closeWindow() {
      this.$emit("closeWindow");
      this.form = {};
      this.$refs.form.clearValidate();
      this.controlWindows.create = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.create {
  padding: 20px;
  .el-input,
  .el-select,
  .el-textarea {
    width: 300px;
  }
}
.buttons {
  position: absolute;
  left: 0;
  bottom: 0;
  padding-right: 40px;
  width: 100%;
  height: 80px;
  line-height: 80px;
  text-align: right;
  border-top: 1px solid #f2f3f5;
  .el-button {
    text-align: right;
    font-size: 14px;
    border-radius: 5px;
  }
}
</style>
