<template>
  <el-dialog
    :title="title"
    :center="true"
    align="left"
    top="5vh"
    width="700px"
    @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>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            v-if="isApproval"
            :model="cexForm"
            status-icon
            :rules="cexRules"
            ref="cexForm"
            label-width="80px"
            class="cexForm-ruleForm"
            ><el-row>
              <el-col :span="12">
                <el-row class="cexForm-div">
                  <el-col :span="8" class="tr cexForm-label">企业名称：</el-col>
                  <el-col :span="16" class="cexForm-main">{{
                    ruleForm.company_name
                  }}</el-col>
                </el-row>
                <el-row class="cexForm-div">
                  <el-col :span="8" class="tr cexForm-label clearfix">
                    <span class="fr">统一社会</span><span class="fr">信用代码：</span>
                  </el-col>
                  <el-col :span="16" class="cexForm-main">{{
                    ruleForm.invoice
                  }}</el-col>
                </el-row>
                <el-row class="cexForm-div">
                  <el-col :span="8" class="tr cexForm-label">企业类型：</el-col>
                  <el-col :span="16" class="cexForm-main">
                    <el-tag type="info" v-for="model in typeStr" :key="model">{{
                      model
                    }}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
              <el-col :span="12">
                <el-form-item label="审核状态" prop="status">
                  <el-select
                    v-model="cexForm.status"
                    placeholder="请选择审核状态"
                    @change="selectChange"
                  >
                    <el-option
                      v-for="item in statusList"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="审核说明" prop="remark">
                  <el-input
                    v-model="cexForm.remark"
                    type="textarea"
                    placeholder="请输入审核说明"
                    :rows="5"
                    maxlength="300"
                    show-word-limit
                  ></el-input>
                </el-form-item>
                <el-col :span="24" style="text-align: right">
                  <el-button type="primary" @click="submitCexForm"
                    >保 存
                  </el-button>
                  <el-button @click="showModelThis = false">取 消</el-button>
                </el-col>
              </el-col>
            </el-row></el-form
          >

          <el-form
            v-else
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            ref="ruleForm"
            label-width="140px"
            class="demo-ruleForm"
            ><el-row>
              <el-col :span="24">
                <el-form-item
                  label="企业名称"
                  prop="company_name"
                  :disabled="isDetail"
                >
                  <el-input
                    placeholder="请输入企业名称"
                    v-model="ruleForm.company_name"
                    :disabled="isDetail"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="统一社会信用代码" prop="invoice">
                  <el-input
                    v-model="ruleForm.invoice"
                    placeholder="请输入统一社会信用代码"
                    :disabled="isDetail"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="企业类型" prop="type">
                  <el-checkbox-group v-model="ruleForm.type">
                    <el-checkbox
                      v-for="(item, i) in typeList"
                      :key="item.code + i"
                      :label="item.code"
                      >{{ item.name }}</el-checkbox
                    >
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
            </el-row>

            <el-col :span="24" style="text-align: right">
              <el-tag
                type="warning"
                class="fl"
                style="max-width: 500px"
                v-if="dataInfo.ex_remark && dataInfo.ex_status === '2'"
                >审核备注：{{ dataInfo.ex_remark }}</el-tag
              >

              <el-button type="primary" @click="submitForm" v-if="!isDetail"
                >保 存
              </el-button>
              <el-button @click="showModelThis = false">{{
                isDetail ? "关 闭" : "取 消"
              }}</el-button>
            </el-col>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
   <script>
import asyncRequest from "@/apis/service/client/company";
import {
  isnumber,
  isMobile,
  validEmail,
  isAlphanumeric,
  isChinese,
  isEmoticon,
  validAlphabets,
  isCreditCode,
} from "@/utils/validate";
import statusList from "@/assets/js/cexStatusList";
export default {
  name: "company",
  props: ["showModel", "id", "isDetail", "isApproval"],
  data() {
    const validatename = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("企业名称不能为空！"));
      } else {
        if (value.length > 100) {
          callback(new Error("企业名称不能超过100字符！"));
        } else {
          if (isEmoticon(value)) {
            callback(new Error("企业名称不能使用表情包！"));
          } else {
            callback();
          }
        }
      }
    };
    const validateinvoice = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("统一社会信用代码不能为空！"));
      } else if (!isCreditCode(value)) {
        callback(new Error("统一社会信用代码不正确！"));
      } else {
        callback();
      }
    };
    return {
      statusList: statusList,
      loading: false,
      title: "添加企业",
      showModelThis: this.showModel,
      dataInfo: {},
      typeStr: [],
      typeList: [
        {
          code: "1",
          name: "供应商",
        },
        {
          code: "2",
          name: "企业客户",
        },
      ],
      cexForm: {
        id: this.id,
        remark: "",
        status: "1",
      },
      ruleForm: {
        id: this.id,
        invoice: "",
        type: [],
        company_name: "",
      },
      cexRules: {
        status: {
          required: true,
          message: "请选择审核状态",
          trigger: "change",
        },
        remark: [
          {
            required: false,
            message: "请输入审核说明！",
            trigger: "blur",
          },
        ],
      },
      rulesThis: this.rules,
      rules: {
        company_name: [
          {
            required: true,
            validator: validatename,
            trigger: "blur",
          },
        ],
        type: [
          {
            type: "array",
            required: true,
            message: "请至少选择一个企业类型",
            trigger: "change",
          },
        ],

        invoice: [
          {
            required: true,
            validator: validateinvoice,
            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() {
      if (this.isApproval) {
        this.loading = true;
        this.title = "企业审核";
        await this.initData();
        await this.resetcexForm();
        this.selectChange();
        this.loading = false;
      } else {
        if (this.id === "add") {
          this.title = "添加企业";
          // this.ruleForm.isAdmin = 0;
          this.loading = false;
          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();
        }
      }
    },
    async resetcexForm() {
      if (this.$refs.cexForm) {
        this.$refs.cexForm.resetFields();
        this.$refs.cexForm.clearValidate();
        this.cexForm = {
          id: this.id,
          remark: "",
          status: "1",
        };
      }
    },
    async initData() {
      this.loading = true;
      let res = await asyncRequest.detail({ id: this.id });
      this.loading = false;
      if (res.code === 0) {
        let data = res.data;
        this.dataInfo = data;
        this.ruleForm = {
          id: this.id,
          invoice: data.invoice,
          type: [],
          company_name: data.company,
        };
        this.typeStr=[]
        if (data.is_supplier === "1") {
          this.ruleForm.type.push("1");
          this.typeStr.push("供应商");
        }
        if (data.is_customer === "1") {
          this.ruleForm.type.push("2");
          this.typeStr.push("企业客户");
        }
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            id: this.id,
            invoice: "91110113MA004JNJ28",
            type: ["1"],
            company_name: "北京万宇恒通国际科贸有限公司",
            // isAdmin: 0
          };
        }
      });
    },
    async submitCexForm() {
      await this.$refs.cexForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          console.log(this.id);
          console.log(this.cexForm);
          let obj = JSON.parse(JSON.stringify(this.cexForm));

          obj.id = this.id;
          console.log(obj);
          let res = await asyncRequest.exam(obj);
          this.loading = false;
          if (res.code === 0) {
            let title = "审核审批成功！";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let obj = JSON.parse(JSON.stringify(this.ruleForm));
          // obj.type = obj.type.join();
          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 === 0) {
            let title = this.id === "add" ? "添加成功" : "修改成功";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
};
</script>

   <style lang="scss" scoped>
.company {
  .cexForm-ruleForm {
    .cexForm-div {
      margin-bottom: 22px;
      .cexForm-label {
        padding-right: 12px;
        color: #606266;
        line-height: 40px;
        font-size: 14px;
        span{
          width: 100%;
          display: inline-block;
           line-height: 20px;
           height: 20px;
           vertical-align: top;
          &:first-child{
            padding-right: 13px;
           
          }
        }
      }
      .cexForm-main {
        line-height: 40px;
        color: rgb(151, 168, 190);
        line-height: 40px;
        font-size: 14px;
      }
    }
  }
}
</style>
   