<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog :visible.sync="showDialog" :title="title" :modal-append-to-body="false" style="text-align:left;"
    @close="closeDialog" :close-on-click-modal="false">
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'100px'">
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户" prop="customerId">
              <el-select v-model="formModel.customerId" filterable placeholder="请选择客户" style="width:300px">
                <el-option v-for="customer in customerList" :key="customer.id" :label="customer.company" :value="customer.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编号" prop="number">
              <el-input v-model="formModel.number" placeholder="请输入编号" style="width:300px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品种及规格" prop="name">
              <el-input v-model="formModel.name" placeholder="请输入品种及规格" style="width:300px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="幅宽" prop="width">
              <el-input-number v-model="formModel.width" :min="0" placeholder="请输入幅宽"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投坯日" prop="processDate">
              <el-date-picker v-model="formModel.processDate" placeholder="请选择投坯日" style="width:300px"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投坯数" prop="processVolume">
              <el-input-number v-model="formModel.processVolume" controls-position="right" :min="0" style="width:110px"></el-input-number>&nbsp;
              <el-input v-model="formModel.processRemark" placeholder="备注" style="width:182px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投坯卷数" prop="processNumber">
              <el-input-number v-model="formModel.processNumber" :min="0"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="伸长率" prop="ratio">
              <el-input-number v-model="formModel.ratio" :min="0" :precision="2"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="色泽" prop="colour">
              <el-input v-model="formModel.colour" placeholder="请输入色泽" style="width:300px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应交成品数" prop="estimateQuantity">
              <el-input-number v-model="formModel.estimateQuantity" :min="0"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="加工单价" prop="unitPrice">
              <el-input-number v-model="formModel.unitPrice" controls-position="right" :min="0" :precision="2" style="width:110px"></el-input-number>&nbsp;
              <el-input v-model="formModel.reason" placeholder="备注" style="width:182px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="坯布单价" prop="clothPrice">
              <el-input-number v-model="formModel.clothPrice" :min="0" :precision="2"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="力资单价" prop="wages">
              <el-input-number v-model="formModel.wages" :min="0" :precision="2"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formModel.remark" placeholder="请输入备注" style="width:93%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import workApi from "@/api/base/work";
import customerApi from "@/api/base/customer";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        customerId: [{ required: true, message: '客户编号不能为空', trigger: 'blur' }],
        number: [{ required: true, message: '编号不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '品种及规格不能为空', trigger: 'blur' }],
        width: [{ required: true, message: '幅宽不能为空', trigger: 'blur' }],
        processDate: [{ required: true, message: '投坯日不能为空', trigger: 'blur' }],
        processVolume: [{ required: true, message: '投坯数不能为空', trigger: 'blur' }],
        processRemark: [{ required: true, message: '投坯备注不能为空', trigger: 'blur' }],
        processNumber: [{ required: true, message: '投坯卷数不能为空', trigger: 'blur' }],
        ratio: [{ required: true, message: '伸长率不能为空', trigger: 'blur' }],
        colour: [{ required: true, message: '色泽不能为空', trigger: 'blur' }],
        estimateQuantity: [{ required: true, message: '应交成品数不能为空', trigger: 'blur' }],
        unitPrice: [{ required: true, message: '单价不能为空', trigger: 'blur' }],
        // reason: [{ required: true, message: '原因不能为空', trigger: 'blur' }],
        // clothPrice: [{ required: true, message: '坯布价不能为空', trigger: 'blur' }],
        wages: [{ required: true, message: '力资不能为空', trigger: 'blur' }],
        // remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }]
      },
      showDialog: true,
      loading: false,
      submitting: false,
      customerList: [],
    };
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate(valid => {
        if (valid) {
          (function () {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return workApi.add(self.formModel);
            }
            else {
              return workApi.update(self.formModel);
            }
          })().then(function (response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success"
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning"
              });

              self.$emit("close", false);
            }
          });
        }
      });
    },
  },
  mounted: function () {
    var self = this;

    customerApi.findList().then(response => {
      this.customerList = response.data.data;
    });
    
    (function () {
      if (self.businessKey.length == 0) {
        return workApi.create()
      }
      else {
        return workApi.edit(self.businessKey)
      }
    })().then(response => {
      var jsonData = response.data;
      self.loading = false;

      if (jsonData.result) {
        self.formModel = jsonData.data;
      } else {
        self.$message.error(jsonData.message + "");
      }
    }).catch(error => {
      self.$message.error(error + "");
    });
  }
};
</script>