<template>
  <el-dialog
    :title="title"
    :center="true"
    align="left"
    top="10vh"
    width="600px"
    @close="closeModel"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-card style="margin: -20px 0 0 0">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            ref="ruleForm"
            :size="'small'"
            label-width="108px"
            class="demo-ruleForm"
            ><el-row>
              <el-col :span="24">
                <el-form-item label="抬头类型" prop="title_type">
                  <el-select
                    :disabled="isDetail"
                    placeholder="发票类型"
                    size="mini"
                    style="width: 100%"
                    v-model="ruleForm.title_type"
                  >
                    <el-option
                      v-for="item in typeOptins"
                      :key="item.code + 'indexs'"
                      :label="item.name"
                      :value="item.code"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="发票抬头" prop="title">
                  <el-input
                    :disabled="isDetail"
                    placeholder="抬头类型"
                    maxlength="100"
                    v-model="ruleForm.title"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="统一社会信用代码" prop="tax_no" label-width="140px">
                  <el-input
                    placeholder="统一社会信用代码"
                    :disabled="isDetail"
                    maxlength="100"
                    v-model="ruleForm.tax_no"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="开户银行名称" prop="bank_name">
                  <el-input
                    placeholder="开户银行名称"
                    :disabled="isDetail"
                    maxlength="100"
                    v-model="ruleForm.bank_name"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="开户银行账号" prop="bank_account">
                  <el-input
                    :disabled="isDetail"
                    placeholder="开户银行账号"
                    maxlength="100"
                    v-model="ruleForm.bank_account"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="纳税登记地址" prop="address">
                  <el-input
                    :disabled="isDetail"
                    placeholder="纳税登记地址"
                    maxlength="100"
                    v-model="ruleForm.address"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="联系电话" prop="tel">
                  <el-input
                    :disabled="isDetail"
                    placeholder="联系电话"
                    maxlength="100"
                    v-model="ruleForm.tel"
                  >
                    <el-select
                      v-model="schange"
                      slot="prepend"
                      style="width: 110px"
                      placeholder="电话类型"
                      @change="selectChangeMobile"
                    >
                      <el-option
                        v-for="item in soptins"
                        :key="item.code + 'indexs'"
                        :label="item.name"
                        :value="item.code"
                      />
                    </el-select> </el-input
                ></el-form-item>
              </el-col>
            </el-row>

            <el-col :span="24" style="text-align: right">
              <el-button
                type="primary"
                :size="'small'"
                @click="submitForm"
                v-if="!isDetail"
                >保 存
              </el-button>
              <el-button @click="showModelThis = false" :size="'small'">{{
                isDetail ? "关 闭" : "取 消"
              }}</el-button>
            </el-col>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/operate/invoicing";
import { isLicense, isqzf, isqz, iszf, isz, isMobile } from "@/utils/validate";

/**
 * @props
 * title_type 抬头类型
 * title 发票抬头
 * */

export default {
  name: "Invoicing",
  props: ["showModel", "id", "isDetail", "sitem"],
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  data() {
    const validateLicense = (rule, value, callback) => {
      if (value !== "") {
        if (!isLicense(value)) {
          callback(new Error("统一社会信用代码不正确！"));
        } else {
          callback();
        }
      } else {
        callback(new Error("请输入统一社会信用代码！"));
      }
    };

    const validatemobile = (rule, value, callback) => {
      if (value !== "") {
        // 区-固-分
        if (this.schange + "" === "1") {
          if (isqzf(value)) {
            callback();
          } else {
            callback(new Error("规则为区号[0+2~3位]-固定电话[7~8位]-分机号[1~6位]！"));
          }
          //区-固
        } else if (this.schange + "" === "2") {
          if (isqz(value)) {
            callback();
          } else {
            callback(new Error("规则为区号[0+2~3位]-固定电话[7~8位]！"));
          }
          //固-分
        } else if (this.schange + "" === "3") {
          if (iszf(value)) {
            callback();
          } else {
            callback(new Error("规则为固定电话[7~8位]-分机号[1~6位]！"));
          }
          //固
        } else if (this.schange + "" === "4") {
          if (isz(value)) {
            callback();
          } else {
            if (value.length > 3) {
              let a = value.slice(0, 3);
              let b = value.slice(3);
              if ((a === "400" || a === "800") && b.length === 7) {
                callback();
              } else {
                callback(new Error("纯固定电话规则为[7~8位]或10位！"));
              }
            }
          }
          //手机号
        } else {
          if (!isMobile(value)) {
            callback(new Error("手机号格式不正确！"));
          } else {
            callback();
          }
        }
      } else {
        callback(new Error("请输入联系电话！"));
      }
    };
    return {
      statusList: [],
      loading: false,
      title: "添加开票信息",
      showModelThis: this.showModel,
      dataInfo: {},
      companyNoNames: "",
      typeOptins: [
        {
          code: "客户抬头",
          name: "客户抬头",
        },
        {
          code: "供应商抬头",
          name: "供应商抬头",
        },
      ],
      soptins: [
        {
          code: "1",
          name: "区-固-分",
        },
        {
          code: "2",
          name: "区-固",
        },
        {
          code: "3",
          name: "固-分",
        },
        {
          code: "4",
          name: "固",
        },
        {
          code: "5",
          name: "手机号",
        },
      ],
      companyList: [],
      schange: "1",
      ruleForm: {},
      rulesThis: this.rules,
      rules: {
        title_type: [{ required: true, message: "请选择抬头类型", trigger: "change" }],
        title: [{ required: true, message: "请选择发票抬头", trigger: "change" }],
        tax_no: [{ required: true, validator: validateLicense, trigger: "blur" }],
        bank_name: [{ required: true, message: "请输入开户银行名称", trigger: "blur" }],
        bank_account: [
          { required: true, message: "请输入开户银行账号", trigger: "blur" },
        ],
        address: [{ required: true, message: "请输入纳税登记地址", trigger: "blur" }],
        tel: [{ required: true, validator: validatemobile, trigger: "blur" }],
      },
    };
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    selectChange() {
      if (this.cexForm.status + "" === "1") {
        this.cexRules.remark[0].required = false;
      } else {
        this.cexRules.remark[0].required = true;
      }
    },
    async initForm() {
      console.log(this.id, this.isDetail);
      this.loading = true;
      this.resetFormData();
      if (this.id === "add") {
        this.title = "添加开票信息";
        this.rulesThis = this.rules;
        await this.resetForm();
      } else {
        if (this.isDetail) {
          this.title = "开票信息";
          this.rulesThis = {};
        } else {
          this.title = "修改开票信息";
          this.rulesThis = this.rules;
        }
        await this.resetForm();
        // await this.initData();
      }
      this.loading = false;
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.resetFormData();
        }
      });
    },

    resetFormData() {
      const {
        title,
        tax_no,
        bank_name,
        bank_account,
        address,
        tel,
        title_type,
      } = this.sitem;
      this.ruleForm = {
        id: this.id || "",
        title: title || "",
        tax_no: tax_no || "91110114MA004JNJ28",
        bank_name: bank_name || "",
        bank_account: bank_account || "",
        address: address || "",
        tel: tel || "",
        title_type: title_type || "客户抬头",
      };
      this.setTypeM(tel || "")
    },

    selectChangeMobile() {
      this.$refs.ruleForm.validateField("tel");
    },
    //判断联系电话类型
    setTypeM(mobile) {
      if (isMobile(mobile)) {
        this.schange = "5";
      } else if (isqzf(mobile)) {
        this.schange = "1";
      } else if (isqz(mobile)) {
        this.schange = "2";
      } else if (iszf(mobile)) {
        this.schange = "3";
      } else {
        this.schange = "4";
      }
      console.log(this.schange);
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          let obj = JSON.parse(JSON.stringify(this.ruleForm));
          let res = {};
          if (this.id === "add") {
            delete obj["id"];
            res = await asyncRequest.add(obj);
          } else {
            res = await asyncRequest.update(obj);
          }
          this.loading = false;
          if (res.code ===1) {
            let title = this.id === "add" ? "添加成功" : "修改成功";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.Invoicing {
  .cexForm-ruleForm {
    .cexForm-div {
      margin-bottom: 0px;
      .cexForm-label {
        padding-right: 12px;
        color: #606266;
        line-height: 40px;
        font-size: 14px;
      }
      .cexForm-main {
        line-height: 40px;
        color: rgb(151, 168, 190);
        line-height: 40px;
        font-size: 14px;
      }
    }
  }
}
</style>
