<template>
  <el-dialog
    v-loading="loading"
    :title="'设置部分字段'"
    :center="true"
    align="left"
    top="15vh"
    width="700px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin: -20px 0 0 0">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :size="'mini'"
            :rules="rulesThis"
            label-width="110px"
            class="demo-ruleForm"
          >
            <el-form-item label="承诺回款时间" prop="paytime">
              <el-date-picker
                v-model="ruleForm.paytime"
                type="date"
                style="width: 100%"
                :disabled="false"
                value-format="yyyy-MM-dd"
                :picker-options="pickerOptions"
                placeholder="承诺回款时间"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="平台订单号" prop="platform_order">
              <el-input
                v-model="ruleForm.platform_order"
                placeholder="如：PO号"
                type="textarea"
                :rows="4"
                maxlength="2000"
              />
            </el-form-item>
            <el-form-item label="其他单号" prop="workNo">
              <el-input
                v-model="ruleForm.workNo"
                placeholder="如：业管单号"
                type="textarea"
                :rows="4"
                maxlength="2000"
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button type="primary" :size="'mini'" @click="submitForm"
            >保 存
          </el-button>
          <el-button :size="'mini'" @click="showModelThis = false"
            >取 消</el-button
          >
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/salesOrder";
import resToken from "@/mixins/resToken";
export default {
  name: "brand",
  props: ["showModel", "sitem"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "添加单位",
      showModelThis: this.showModel,
      options: [],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 1000 * 60 * 60 * 24;
        },
      },
      ruleForm: {
        orderCode: "",
        paytime: "",
        workNo: "",
        platform_order: "",
      },
      rulesThis: this.rules,
      rules: {
        paytime: [
          { required: true, message: "请选择承诺回款时间", trigger: "change" },
        ],
        platform_order: [
          { required: true, message: "请输入平台订单号", trigger: "blur" },
        ],
        workNo: [
          { required: true, message: "请输入其他单号", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    async initForm() {
      this.loading = true;
      this.rulesThis = this.rules;
      this.options = [];
      await this.resetForm();
      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { orderCode, paytime, workNo, platform_order } = this.sitem;
          this.ruleForm = {
            orderCode: orderCode || "",
            paytime: paytime || "",
            workNo: workNo || "",
            platform_order: platform_order || "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          let res = (res = await asyncRequest.saleaddother(model));

          this.loading = false;
          if (res && res.code === 0) {
            this.$notify.success({
              title: "设置成功！",
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

   <style lang="scss" scoped>
.brand {
}
</style>
