<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="5vh"
    width="1040px"
    :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="closeModel"
  >
    <el-card>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            label-width="110px"
            class="demo-ruleForm"
          >
            <el-row :gutter="10">
              <el-col :span="12">
                <el-form-item label="单位名称" prop="company_name">
                  <el-input
                    v-model="ruleForm.company_name"
                    :disabled="isDetail"
                  />
                </el-form-item> </el-col
              ><el-col :span="12">
                <el-form-item label="纳税人识别号" prop="company_license">
                  <el-input
                    v-model="ruleForm.company_license"
                    :disabled="isDetail"
                  />
                </el-form-item> </el-col
            ></el-row>
            <el-col :span="12">
              <el-form-item label="银行名称" prop="bank_name">
                <el-input v-model="ruleForm.bank_name" :disabled="isDetail" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="银行卡号" prop="bankNo">
                <el-input v-model="ruleForm.bankNo" :disabled="isDetail" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系人" prop="contector">
                <el-input
                  v-model="ruleForm.contector"
                  :disabled="isDetail"
                /> </el-form-item
            ></el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="mobile">
                <el-input
                  v-model="ruleForm.mobile"
                  :disabled="isDetail"
                /> </el-form-item
            ></el-col>
            <el-col :span="24">
              <el-form-item label="联系地址" prop="company_address">
                <el-input
                  v-model="ruleForm.company_address"
                  type="textarea"
                  :rows="3"
                  :disabled="isDetail"
                />
              </el-form-item>
            </el-col>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button v-if="!isDetail" type="primary" @click="submitForm"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            isDetail ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/parameter/finance";
import resToken from "@/mixins/resToken";
import {
  isMobile,
  isLicense,
  isPhone,
} from "@/utils/validate";
export default {
  name: "Account",
  props: ["showModel", "id", "isDetail"],
  mixins:[resToken],
  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 (isPhone(value) || isMobile(value)) {
          callback();
        } else {
          callback(new Error("联系电话格式不正确！"));
        }
      } else {
        callback(new Error("请输入联系电话！"));
      }
    };

    return {
      company_img: "",
      roleList: [],
      loading: false,
      title: "添加财务信息",
      showModelThis: this.showModel,
      ruleForm: {
        companyNo: this.id,
        company_name: "", // 企业名称
        company_license: "", // 纳税人识别号
        mobile: "", //联系电话
        bank_name: "", //银行名称
        bankNo: "", //银行卡号
        contector: "", //联系人
        company_address: "", //联系地址
        company_img: "", //营业执照
      },
      rulesThis: this.rules,
      rules: {
        company_license: [
          {
            required: true,
            validator: validateLicense,
            trigger: "blur",
          },
        ],
        company_name: [
          {
            required: true,
            message: "请输入单位名称",
            trigger: "blur",
          },
        ],
        bank_name: [
          {
            required: true,
            message: "请输入银行名称",
            trigger: "blur",
          },
        ],
        bankNo: [
          {
            required: true,
            message: "请输入银行卡号",
            trigger: "blur",
          },
        ],
        mobile: [
          {
            required: true,
            validator: validatemobile,
            trigger: "blur",
          },
        ],
        company_address: [
          {
            required: true,
            message: "请输入联系地址",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    async initForm() {
      console.log(this.id);
      this.loading = true;
      // await this.getRole();
      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 getRole() {
      const model = {
        level: "", // 姓名
        role_name: "",
      };
      const res = await asyncRequest.getRole(model);
      if (res.code === 0 && res.data) {
        this.roleList = res.data;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      }else{
        this.$message.warning(res.message);
      }
    },
    async initData() {
      const res = await asyncRequest.detail({ companyNo: this.id });
      if (res && res.code === 0) {
        this.ruleForm = res.data;
        this.ruleForm.companyNo = this.id;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      }else{
        this.$message.warning(res.message);
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            companyNo: this.id,
            company_name: "", // 企业名称
            company_license: "", // 纳税人识别号
            mobile: "", //联系电话
            bank_name: "", //银行名称
            bankNo: "", //银行卡号
            contector: "", //联系人
            company_address: "", //联系地址
            company_img: "", //营业执照
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const 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 && res.code === 0) {
            const title = this.id === "add" ? "添加成功" : "修改成功";
            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;
        }
      });
    },
    //图片上传失败
    UploadErrorEvent() {
      this.$message.error("图片上传失败!");
      this.ruleForm.company_img = "";
      this.company_img = "";
      this.$refs.ruleForm.validateField("company_img");
    },

    //图片上传成功
    UploadSuccessEvent(data) {
      this.company_img = data.url;
      this.ruleForm.company_img = data.url;
      this.$message.success("图片上传成功!");
      this.$refs.ruleForm.validateField("company_img");
    },

    //判断图片规格
    beforeAvatarUpload(file) {
      let isJPG = false;
      if (
        file.type === "image/jpg" ||
        file.type === "image/png" ||
        file.type === "image/bmp" ||
        file.type === "image/jpeg" ||
        file.type === "image/gif"
      ) {
        isJPG = true;
      }
      const isLt2M = file.size / 1024 / 1024 < 1;
      if (!isJPG) {
        this.$message.error("图片格式不正确！");
      }
      if (!isLt2M) {
        this.$message.error("图片大小不能超过 1MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

   <style lang="scss" scoped>
.finance {
  .activity-upload {
    .btnupload {
      float: left;
      border: 1px solid #cccccc;
      box-sizing: border-box;
      width: 135px;
      height: 135px;
      line-height: 135px;
      text-align: center;
    }
    .Upload {
      width: 135px;
      height: 135px;
      line-height: 135px;
      text-align: center;
      position: absolute;
      line-height: 0px;
      top: 0;
      left: 0;
      line-height: 135px;
    }
    .fileUp {
      vertical-align: top;
    }
    .avatar {
      width: 135px;
      height: 135px;
      line-height: 135px;
      text-align: center;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 50px;
      height: 50px;
      line-height: 50px;
      text-align: center;
    }
    .avatar {
      width: 100%;
      height: 100%;
      display: block;
    }
    .txt-tips {
      display: inline-block;
      font-size: 13px;
      color: #606266;
      padding: 18px 0 0 18px;
      p {
        margin: 0;
        line-height: 30px;
      }
    }
    .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
  }
}
</style>
