<template>
  <el-dialog :title="comIsEntityMaterials ?  '推送到物资用量' : '推送工程量'" :visible="comPushProjectUseDialog" width="480px" class="push-project-use-dialog"
    @close="dialogClose" @open="dialogOpen" :close-on-click-modal="false" :destroy-on-close="true">
    <div class="dialog-body-content">
      <el-form :model="pushForm" ref="pushForm" :rules="rules" class="push-form">
        <el-form-item label="分包项目名称">
          <el-input :value="pushForm.projectName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="本月实际工程量">
          <el-input :value="pushForm.totalQuantities" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="需推送工程量" prop="pushQuantities" :rules="pushQuantitiesValidator(pushForm.totalQuantities)">
          <el-input v-model="pushForm.pushQuantities" @input="pushQuantitiesIpt" placeholder="请填写需推送工程量"></el-input>
        </el-form-item>
        <!-- ipm项目编码 -->
        <el-form-item label="IPM项目编码" prop="ipmProjectCode">
          <el-input v-model="pushForm.ipmProjectCode" placeholder="请输入IPM项目编码"></el-input>
        </el-form-item>
        <!-- ipm合同编码 -->
        <el-form-item label="IPM合同编码" prop="ipmContractCode">
          <el-input v-model="pushForm.ipmContractCode" placeholder="请输入IPM合同编码"></el-input>
        </el-form-item>
        <!-- ipm作业编码 -->
        <el-form-item label="IPM作业编码" prop="ipmBizCode">
          <el-input v-model="pushForm.ipmBizCode" placeholder="请输入IPM作业编码"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <!-- 底部按钮 -->
    <div class="dialog-footer-content">
      <div class="footer-btn cancel-submit" @click="cancelSubmit">取消</div>
      <div class="footer-btn ok-submit" @click="pushResult">确定推送</div>
    </div>
  </el-dialog>
</template>
<script>
import { subtract, targetIsNegative, add } from "@/utils/decimal";
import { cloneDeep } from 'lodash-es';
export default {
  name: "pushProjectUseDialog",
  model: {
    prop: "pushProjectUseDialog",
    event: "close"
  },
  props: {
    pushProjectUseDialog: {
      type: Boolean,
      default: false
    },
    isEntityMaterials: {
      type: Boolean,
      default: false
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  watch: {
    pushProjectUseDialog: {
      handler(newValue, oldValue) {
        this.comPushProjectUseDialog = newValue;
      }
    },
    comIsEntityMaterials: {
      handler(newValue, oldValue) {
        this.comIsEntityMaterials = newValue;
      }
    },
    rowData: {
      handler(newValue, oldValue) {
        const _temp = newValue ? newValue : {};
        this.pushForm = cloneDeep({ ...this.pushForm, ..._temp });
      }
    }
  },
  data() {
    return {
      comIsEntityMaterials: this.isEntityMaterials,
      comPushProjectUseDialog: this.pushProjectUseDialog,
      pushForm: {
        id: "",
        projectName: "",
        totalQuantities: "",
        pushQuantities: "",
        ipmProjectCode: "",
        ipmContractCode: "",
        ipmBizCode: "",
      },
      rules: {
        ipmProjectCode: [
          { required: true, trigger: ["blur", "change"], message: "请输入IPM项目编码", whitespace: true }
        ],
        ipmContractCode: [
          { required: true, trigger: ["blur", "change"], message: "请输入IPM合同编码", whitespace: true }
        ],
        ipmBizCode: [
          { required: true, trigger: ["blur", "change"], message: "请输入IPM作业编码", whitespace: true }
        ]
      }
    };
  },
  //可访问data属性
  created() {

  },
  //计算集
  computed: {

  },
  //方法集
  methods: {
    // 验证需推送工程量
    pushQuantitiesValidator(maxValue) {
      return [{
        trigger: ['blur', 'change'], validator: (rule, value, callback) => {
          const reg = /^(?!0\d)(?!0+$)(?!0*\.0*$)\d+(\.\d+)?$/;
          if (!reg.test(value)) return callback(new Error("请输入正确的工程量"));
          const _maxValue = maxValue ? maxValue : 0;
          if (targetIsNegative(subtract(_maxValue, value))) return callback(new Error("注：推送工程量不得大于实际产生的总工程量"));
          callback();
        }
      }];
    },
    pushQuantitiesIpt(value) {
      const reg = /^(?!0\d)(?!0+$)(?!0*\.0*$)\d+(\.\d+)?$/;
      if (reg.test(value)) {
        this.pushForm.pushQuantities = add(0, value);
      }
    },
    clearValidate() {
      const form = this.$refs["pushForm"];
      if (form) form.clearValidate();
    },
    dialogClose() {
      this.$emit("dialogClose");
      this.pushForm = this.$options.data.call(this).pushForm;
      this.clearValidate();
      this.$emit("close", false);
    },
    dialogOpen() {

    },
    cancelSubmit() {
      this.comPushProjectUseDialog = false;
    },
    //推送工程量
    pushResult() {
      this.$refs["pushForm"].validate(flag => {
        if (flag) {
          const { id, pushQuantities, ipmProjectCode, ipmContractCode, ipmBizCode } = cloneDeep(this.pushForm);
          this.$emit("submitPushData", {
            id,
            pushQuantities,
            ipmProjectCode,
            ipmContractCode,
            ipmBizCode
          });
        }
      });
    }
  },
}
</script>
<style lang="scss" scoped>
.push-project-use-dialog {
  ::v-deep .el-dialog {
    margin-top: 0px !important;
    margin: 0px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .el-dialog__header {
      height: 56px;
      padding: 0px 20px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #eeeeee;
      border-top-left-radius: 4px;
      border-top-right-radius: 4px;

      .el-dialog__title {
        color: #232323;
        font-size: 16px;
        font-weight: bold;
      }

      .el-dialog__headerbtn {
        position: static;
        width: 16px;
        height: 16px;
      }
    }

    .el-dialog__body {
      padding: 0px;

      .dialog-body-content {
        padding: 24px 20px;
        box-sizing: border-box;

        .push-form {
          .el-form-item {
            &.is-error {
              margin-bottom: 33px;
            }

            margin-bottom: 16px;
            display: flex;
            align-items: center;

            .el-form-item__label {
              width: 98px;
              min-width: 98px;
              line-height: 20px;
              color: rgba(35, 35, 35, 0.8);
              font-weight: 350;
              font-size: 14px;
              text-align: right;
              padding-right: 0px;
              margin-right: 16px;
              white-space: nowrap;
            }
            .el-form-item__content {
              width: 100%;
              line-height: 32px;

              .el-input {
                &.is-disabled {
                  .el-input__inner {
                    background: #fff;
                    color: rgba(35, 35, 35, 0.8);
                  }
                }
              }

              .el-input__inner {
                line-height: 32px;
                height: 32px;
                border-radius: 2px;
                padding: 0px 7px;

                ::placeholder {
                  color: #c0c4cc !important;
                }
              }
            }
          }
        }
      }

      .dialog-footer-content {
        padding: 16px 20px;
        box-sizing: border-box;
        border-top: 1px solid #eeeeee;
        display: flex;
        align-items: center;
        justify-content: flex-end;

        .footer-btn {
          height: 32px;
          padding: 0px 16px;
          font-size: 14px;
          font-weight: 350;
          box-sizing: border-box;
          display: flex;
          align-items: center;
          justify-content: center;
          border-radius: 4px;
          cursor: pointer;

          &.cancel-submit {
            color: rgba(35, 35, 35, 0.8);
            background: #fff;
            border: 1px solid #dcdfe6;
            margin-right: 12px;
          }

          &.ok-submit {
            color: #fff;
            background: #0081ff;
          }
        }
      }
    }
  }
}
</style>
