<template>
  <el-dialog
    v-loading="loading"
    :title="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-top: -20px" class="account">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :size="'mini'"
            :rules="rulesThis"
            label-width="85px"
            class="demo-ruleForm"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item label="无地址数" prop="wsend_num" required>
                  <el-input
                    disabled
                    v-model="ruleForm.wsend_num"
                    maxlength="10"
                    placeholder="无地址数"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="发货数量" prop="send_num">
                  <el-input
                    v-model="ruleForm.send_num"
                    placeholder="收货数量"
                    maxlength="10"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系人" prop="contactor">
                  <el-input
                    v-model="ruleForm.contactor"
                    maxlength="250"
                    placeholder="联系人"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系电话" prop="mobile">
                  <el-input
                    v-model="ruleForm.mobile"
                    maxlength="11"
                    placeholder="联系电话"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item
                  :label="'收货省市区'"
                  prop="addr_code"
                  :label-width="'99px'"
                >
                  <select-area
                    :value="ruleForm.addr_code"
                    :size="'mini'"
                    placeholder="收货省市区"
                    @selectChange="select_area_change($event)"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="详细地址" prop="addr">
                  <el-input
                    v-model="ruleForm.addr"
                    maxlength="250"
                    placeholder="详细地址"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24" style="text-align: right">
                <el-button type="primary" @click="submitForm" :size="'mini'"
                  >保 存
                </el-button>
                <el-button @click="showModelThis = false" :size="'mini'">{{
                  isDetail ? "关 闭" : "取 消"
                }}</el-button>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/salesOrder/detail";
import resToken from "@/mixins/resToken";
import {
  isnumber,
  isMobile,
  isChinese,
  isEmoticon,
  isSpecialSymbol,
  hasSpace,
  isAddr,
} from "@/utils/validate";
export default {
  name: "Account",
  props: ["showModel", "id", "sitem"],
  mixins: [resToken],
  data() {
    const validateWeight = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("不能为空!"));
      } else {
        if (!isnumber(value)) {
          callback(new Error("仅支持整数!"));
        } else {
          callback();
        }
      }
    };
    const validatemobile = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("手机号不能为空！"));
      } else {
        if (!isMobile(value)) {
          callback(new Error("手机号格式不正确！"));
        } else {
          callback();
        }
      }
    };
    const validatecontactor = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("联系人不能为空！"));
      } else {
        if (value.length < 2 || value.length > 10) {
          callback(new Error("联系人规则为2~10位汉字！"));
        } else {
          if (!isChinese(value)) {
            callback(new Error("联系人规则为2~10位汉字！"));
          } else if (isEmoticon(value)) {
            callback(new Error("联系人规则为2~10位汉字！"));
          } else {
            callback();
          }
        }
      }
    };
    const validateAddr = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("详细地址不能为空！"));
      } else {
        if (hasSpace(value)) {
          callback(new Error("不能出现/回车/换行符！"));
        } else if (isSpecialSymbol(value)) {
          callback(new Error("不能使用英文特殊字符！"));
        } else if (isAddr(value)) {
          callback();
        } else {
          callback(new Error("详细地址填写不规范！"));
        }
      }
    };
    return {
      roleList: [],
      loading: false,
      title: "新建发货申请单",
      showModelThis: this.showModel,
      rulesThis: {},
      rules: {
        send_num: [
          {
            required: true,
            validator: validateWeight,
            trigger: "blur",
          },
        ],

        contactor: [
          {
            required: true,
            trigger: "blur",
            validator: validatecontactor,
          },
        ],
        mobile: [
          {
            required: true,
            validator: validatemobile,
            trigger: "blur",
          },
        ],

        addr_code: [
          {
            type: "array",
            required: true,
            message: "收货省市区不能为空",
            trigger: "change",
          },
        ],
        addr: [
          {
            required: true,
            validator: validateAddr,
            trigger: "blur",
          },
        ],
      },
      ruleForm: {
        orderCode: "",
        wsend_num: "",
        send_num: "",
        contactor: "",
        mobile: "",
        addr_code: [],
        addr: "",
        id: "",
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
      this.showModelThis = false;
      this.$emit("closeModel");
    },

    async initForm() {
      this.loading = true;
      this.rulesThis = this.rules;
      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, good_num, addr_sum } = this.sitem;
          const sum = parseInt(good_num + "") - parseInt(addr_sum + "");
          this.ruleForm = {
            orderCode: orderCode || "",
            wsend_num: sum || "0",
            send_num: sum || "0",
            contactor: "",
            mobile: "",
            addr: "",
            addr_code: [],
          };
        }
      });
    },
    //省市区选择
    async select_area_change(e) {
      this.ruleForm.addr_code = e;
      this.$refs.ruleForm.validateField("addr_code");
    },
    // 保存更改
    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));
          model.addr_code = model.addr_code.toString();
          const res = await asyncRequest.saleout(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;
        }
      });
    },
    handleCompany(e) {
      console.log(e);
      if (e && e.code) {
        this.ruleForm.post_name = [e.shortName];
      }
      this.$refs.ruleForm.validateField("post_name");
    },
  },
};
</script>

<style lang="scss" scoped>
.account {
  .gongshi {
    span {
      vertical-align: top;
      display: inline-block;
      color: #000;
    }
    .icon-span {
      padding: 0 5px;
      height: 40px;
      line-height: 40px;
      font-size: 20px;
      color: #606266;
      display: inline-block;
      // vertical-align: top;
      // display: inline-block;
    }
    .label {
      height: 40px;
      line-height: 40px;
    }
    .tuan {
      &.chu {
        width: 60px;
        height: 40px;
        display: inline-block;
        span {
          width: 60px;
          display: inline-block;
          line-height: 20px;
          text-align: center;
          font-size: 12px;
          height: 20px;
          &:last-child {
            border-top: 1px solid #606266;
          }
        }
      }
      &.cheng {
        .name {
          height: 40px;
          line-height: 40px;
        }
        .icon-span {
          line-height: 40px;
          font-size: 16px;
          padding: 0 1px;
        }
      }
    }
  }
}
</style>
