<template>
  <div class="app-container" v-loading="loading">
    <el-form ref="form" :model="form" :rules="registerRules">
      <table class="table_register">
        <tr>
          <th rowspan="6">企业信息</th>
          <th><span style="color: red;">*</span>企业名称</th>
          <td>
            <el-form-item label-width="0" prop="entName">
              <el-input v-model="form.entName" type="text" auto-complete="off" placeholder="请输入企业名称">
              </el-input>
            </el-form-item>
          </td>
          <th><span style="color: red;">*</span>统一社会信用代码</th>
          <td>
            <el-form-item label-width="0" prop="license">
              <el-input type="text" @change="validLicense"
                v-model="form.license"
                placeholder="请输入统一社会信用代码">
              </el-input>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th><span style="color: red;">*</span>注册资金</th>
          <td>
            <el-form-item label-width="0" prop="regCapital">
              <el-input type="text"
                v-model="form.regCapital"
                placeholder="请输入注册资金">
                <template slot="append">万元</template>
              </el-input>
            </el-form-item>
          </td>
          <th><span style="color: red;">*</span>工商注册时间</th>
          <td>
            <el-form-item label-width="0" prop="regDate">
              <el-date-picker v-model="form.regDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th><span style="color: red;">*</span>工商注册类型</th>
          <td>
            <el-form-item label-width="0" prop="regType">
              <el-select v-model="form.regType" filterable>
                <el-option v-for="item in regTypeOptions" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </td>
          <th><span style="color: red;">*</span>主营产品(服务)所属技术领域</th>
          <td>
            <el-form-item label-width="0" prop="mainProducts">
              <el-select v-model="form.mainProducts" style="width: 220px;" filterable>
                <el-option v-for="(item,index) in mainProductsOptions" :label="item.dictLabel" :key="index" :value="item.dictValue">{{item.dictLabel}}</el-option>
              </el-select>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th><span style="color: red;">*</span>通信地址</th>
          <td>
            <el-form-item label-width="0" prop="address">
              <el-input v-model="form.address" type="text" auto-complete="off" placeholder="请输入通信地址">
              </el-input>
            </el-form-item>
          </td>
          <th><span style="color: red;">*</span>邮政编码</th>
          <td>
            <el-form-item label-width="0" prop="zipCode">
              <el-input v-model="form.zipCode" type="text" auto-complete="off" placeholder="请输入邮政编码">
              </el-input>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th><span style="color: red;">*</span>企业所属行政区域</th>
          <td colspan="3">
            <el-col :span="8">
              <el-form-item label-width="0" prop="provinceId">
                <el-select v-model="form.provinceId" placeholder="省" @change="getCityData" style="width: 220px;" filterable>
                  <el-option v-for="(item,index) in provinceDataList" :key="index" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="cityId" label-width="0px">
                <el-select v-model="form.cityId" placeholder="市" @change="getDistrictData" style="width: 220px;" filterable>
                  <el-option v-for="(item, index) in cityDataList" :key="index" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label-width="0px" prop="districtId">
                <el-select v-model="form.districtId" placeholder="区" style="width: 220px;" filterable>
                  <el-option v-for="(item, index) in districtDataList" :key="index" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </td>
        </tr>
        <tr>
          <th><span style="color: red;">*</span>企业所得税主管税务机关</th>
          <td colspan="3">
            <el-form-item label-width="0" prop="taxOffice">
              <el-input v-model="form.taxOffice" type="text" auto-complete="off" placeholder="请输入企业所得税主管税务机关" disabled>
              </el-input>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th rowspan="3">法人信息</th>
          <th><span style="color: red;">*</span>姓名</th>
          <td>
            <el-form-item label-width="0" prop="legalName">
              <el-input v-model="form.legalName" type="text" auto-complete="off" placeholder="请输入法人姓名">
              </el-input>
            </el-form-item>
          </td>
          <th><span style="color: red;">*</span>身份证号码</th>
          <td>
            <el-form-item label-width="0" prop="legalIdCard">
              <el-input v-model="form.legalIdCard" type="text" auto-complete="off" placeholder="请输入法人身份证号码">
              </el-input>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th><span style="color: red;">*</span>手机号码</th>
          <td>
            <el-form-item label-width="0" prop="legalMobile">
              <el-input v-model="form.legalMobile" type="text" auto-complete="off" placeholder="手机号码">
              </el-input>
            </el-form-item>
          </td>
          <th>固定电话</th>
          <td>
            <el-form-item label-width="0" prop="legalPhone">
              <el-input v-model="form.legalPhone" type="text" auto-complete="off" placeholder="请输入固定电话">
              </el-input>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th>传真</th>
          <td>
            <el-form-item label-width="0" prop="legalFax">
              <el-input v-model="form.legalFax" type="text" auto-complete="off" placeholder="请输入法人传真">
              </el-input>
            </el-form-item>
          </td>
          <th>E-mail</th>
          <td>
            <el-form-item label-width="0" prop="legalEmail">
              <el-input v-model="form.legalEmail" type="text" auto-complete="off" placeholder="请输入法人邮箱">
              </el-input>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th rowspan="3">联系人信息</th>
          <th><span style="color: red;">*</span>姓名</th>
          <td>
            <el-form-item label-width="0" prop="linkName">
              <el-input v-model="form.linkName" type="text" auto-complete="off" placeholder="请输入联系人姓名">
              </el-input>
            </el-form-item>
          </td>
          <th><span style="color: red;">*</span>身份证号码</th>
          <td>
            <el-form-item label-width="0" prop="linkIdCard">
              <el-input v-model="form.linkIdCard" type="text" auto-complete="off" placeholder="请输入联系人身份证号码">
              </el-input>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th><span style="color: red;">*</span>手机号码</th>
          <td>
            <el-form-item label-width="0" prop="linkMobile">
              <el-input v-model="form.linkMobile" type="text" auto-complete="off" placeholder="请填入手机号码">
              </el-input>
            </el-form-item>
          </td>
          <th>固定电话</th>
          <td>
            <el-form-item label-width="0" prop="linkPhone">
              <el-input v-model="form.linkPhone" type="text" auto-complete="off" placeholder="请输入联系人固定电话">
              </el-input>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th>传真</th>
          <td>
            <el-form-item label-width="0" prop="linkFax">
              <el-input v-model="form.linkFax" type="text" auto-complete="off" placeholder="请输入联系人传真">
              </el-input>
            </el-form-item>
          </td>
          <th>E-mail</th>
          <td>
            <el-form-item label-width="0" prop="linkEmail">
              <el-input v-model="form.linkEmail" type="text" auto-complete="off" placeholder="请输入联系人邮箱">
              </el-input>
            </el-form-item>
          </td>
        </tr>

        <tr>
          <th>企业上市信息</th>
          <th><span style="color: red;">*</span>企业是否上市</th>
          <td>
            <el-form-item label-width="0" prop="listedFlag">
              <el-radio-group v-model="form.listedFlag">
                <el-radio v-for="(item, index) in yesOrNoOptions" :key="index" :label="item.dictValue">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </td>
          <template v-if="form.listedFlag == 1">
            <th><span style="color: red;">*</span>企业上市代码</th>
            <td>
              <el-form-item label-width="0" prop="listingCode">
                <el-input v-model="form.listingCode" type="text" auto-complete="off" placeholder="请输入企业上市代码">
                </el-input>
              </el-form-item>
            </td>
          </template>
        </tr>
        <tr>
          <th>风险投资信息</th>
          <th><span style="color: red;">*</span>是否引入风险投资</th>
          <td>
            <el-form-item label-width="0" prop="investFlag">
              <el-radio-group v-model="form.investFlag">
                <el-radio v-for="(item, index) in yesOrNoOptions" :key="index" :label="item.dictValue">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </td>
          <template v-if="form.investFlag == 1">
            <th><span style="color: red;">*</span>风险投资金额</th>
            <td>
              <el-form-item label-width="0" prop="investCapital">
                <el-input v-model="form.investCapital" type="text" auto-complete="off" placeholder="请输入风险投资金额">
                  <template slot="append">万元</template>
                </el-input>
              </el-form-item>
            </td>
          </template>
        </tr>
        <tr>
          <th>高新企业</th>
          <th><span style="color: red;">*</span>是否为高新企业</th>
          <td>
            <el-form-item label-width="0" prop="techFlag">
              <el-radio-group v-model="form.techFlag">
                <el-radio v-for="(item, index) in yesOrNoOptions" :key="index" :label="item.dictValue">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </td>
          <template v-if="form.techFlag == 1">
            <th><span style="color: red;">*</span>高新企业编号</th>
            <td>
              <el-form-item label-width="0" prop="techCode">
                <el-input v-model="form.techCode" type="text" auto-complete="off" placeholder="请输入高新企业编号">
                </el-input>
              </el-form-item>
            </td>
          </template>
        </tr>
        <tr>
          <th>上年度是否做加计扣除</th>
          <th><span style="color: red;">*</span>上年度是否做加计扣除</th>
          <td colspan="3">
            <el-form-item label-width="0" prop="deductionFlag">
              <el-radio-group v-model="form.deductionFlag">
                <el-radio v-for="(item, index) in yesOrNoOptions" :key="index" :label="item.dictValue">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </td>
        </tr>
        <tr v-if="type == 'add'">
          <th>服务时间</th>
          <th><span style="color: red;">*</span>服务时间段</th>
          <td colspan="3">
            <el-form-item prop="serviceDate" label-width="0">
              <el-date-picker v-model="form.serviceDate" placeholder="选择日期" type="daterange" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th rowspan="2">附件</th>
          <th><span style="color: red;">*</span>企业营业执照</th>
          <td colspan="3">
            <el-form-item label-width="3" prop="licensePath" ref="licenseUpload">
              <ImageUpload @input="handleLicenseSuccess" :params="params" :limit="limit" :value="form.licensePath" :fileSize="fileSize"></ImageUpload>
              <!-- <FileUpload :file="{url: form.licensePath, name: form.fileName}" @input="handleSuccess">上传材料</FileUpload> -->
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th>其他附件</th>
          <td colspan="3">
            <el-form-item prop="attachIds" label-width="0">
              <!-- <FileUpload :files="form.fileList" @input="handleSaleSuccess" :multiple="true" :params="params">上传材料</FileUpload>
              <el-table v-loading="loading" :data="list" border style="width: 100%;margin-top: 5px;margin-bottom: 5px;" v-if="type == 'update' || type == 'updateEnt'">
                <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
                <el-table-column label="文件名称" prop="fileName" align="center"></el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-button type="text" @click="handleView(scope.row)">查看</el-button>
                    <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table> -->
              <el-upload
                class="upload-demo"
                :action="action"
                :headers="headers"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                :on-success="handleFileSuccess"
                multiple
                :file-list="form.fileList">
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </td>
        </tr>
        <tr>
          <th>文件水印</th>
          <th>文件水印</th>
          <td colspan="3">
            <el-form-item label-width="0" prop="logo" ref="logoUpload">
              <ImageUpload @input="handleLogoSuccess" :params="params" :limit="limit" :value="form.logo" :fileSize="fileSize"></ImageUpload>
            </el-form-item>
          </td>
        </tr>
      </table>
      <el-row :gutter="40" style="margin-top: 20px;">
        <el-col :span="24">
          <el-form-item style="width:100%;text-align: center;" label-width="0px">
            <el-button :loading="loading" size="medium" type="primary" @click.native.prevent="handleRegister" style="width: 100px;">
              提交
            </el-button>
            <el-button size="medium" type="primary" style="width: 100px;" @click="cancel">取消</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <upload-enclosure ref="uploadEnclosure" v-if="enclosureVisible" @refreshData="getList()" :attachType="attachType"></upload-enclosure>
    <el-dialog
      :visible.sync="dialogVisible"
      title="预览"
      width="800"
      append-to-body>
      <img
        :src="dialogImageUrl"
        style="display: block; max-width: 100%; margin: 0 auto"/>
    </el-dialog>
  </div>
</template>
<script>

import { getToken } from "@/utils/auth";
import { getEntInfoByIdApi,updateEntInfoApi } from "@/api/admin/ent/ent"
import { registerEnterUserApi } from "@/api/admin/ent/ent";
import {getProvinceDataApi,getRegionChildrenApi,validLicenseApi} from "@/api/common/common"
import { regTypeOptions,mainProductsOptions,yesOrNoOptions } from "@/utils/dataFormat"
import FileUpload from '@/components/FileUpload'
import ImageUpload from '@/components/ImageUpload'
import {getEnclosureApi,entUserAudit} from "@/api/enterprise/project/project"
import UploadEnclosure from "./components/UploadEnclosure"
import {deleteEnclosureApi} from '@/api/enterprise/project/project'
import { validIDcard,validAmount,validNumber,validAdress,validPhone,validZhcn,validEmail,validZh } from '@/utils/validate'

export default {
  name: "First",
  components: {
    FileUpload,
    ImageUpload,
    UploadEnclosure
  },
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.form.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    const validateLicense = (rule, value, callback) => {
            if (value === '') {
              callback(new Error('请上传附件'));
            }
          };
    return {
      action: process.env.VUE_APP_BASE_API + "/common/attach/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      dialogVisible: false,
      dialogImageUrl: '',
      height: window.innerHeight + 'px',
      width: '100%',
      downloadUrl: '',
      fileUrl: '',
      fileVisible: false,
      limit: 1, //长传图片数量
      fileSize: 10,
      enclosureVisible: false,
      type: this.$route.query.type,
      attachType: 'ENT',
      dataId: this.$route.params.id,
      params: {
        attachType: 'ENT',
        dataId: ''
      }, //附件上传需要
      activeName: '3',
      list: [],
      provinceName: '',
      cityName: '',
      districtName: '',
      codeUrl: "",
      regTypeOptions: regTypeOptions,
      mainProductsOptions: mainProductsOptions,
      yesOrNoOptions: yesOrNoOptions,
      provinceDataList: [],
      cityDataList: [],
      districtDataList: [],
      disabledVisible: false,
      form: {
        id: '',
        fileList: [],
        serviceDate: [],
        fileList: [],
        listedFlag: 1,
        investFlag: 1,
        investFlag: 1,
        techFlag: 1,
        startDate: '',
        endDate: '',
        attachIds: [],
        provinceId: '',
        cityId: '',
        districtId: '',
        mainProducts: '',
        mainProducts: '',
        userName: "",
        password: "",
        confirmPassword: "",
      },
      total: 0,
      registerRules: {
        linkPhone: [
          { validator: validPhone, trigger: "blur" },
        ],
        legalPhone: [
          { validator: validPhone, trigger: "blur" },
        ],
        userName: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        nickName: [
          { required: true, trigger: "blur", message: "请输入姓名" },
          {max: 30, message: '最多输入30个字符', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, trigger: "blur", message: "请输入手机号" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        email: [
          { required: true, trigger: "blur", message: "请输入邮箱" },
          { validator: validEmail, trigger: 'blur'}
        ],
        legalEmail: [
          { validator: validEmail, trigger: 'blur'}
        ],
        linkEmail: [
          { validator: validEmail, trigger: 'blur'}
        ],
        entName: [
          { required: true, trigger: "blur", message: "请输入企业名称" },
          { validator: validZh, trigger: 'blur' }
        ],
        license: [
          { required: true, trigger: "blur", message: "请输入统一社会信用代码" },
          {min: 18,max: 18, message: '请输入正确的统一社会信用代码格式', trigger: 'blur' }
        ],
        regCapital: [
          { required: true, trigger: "blur", message: "请输入注册资金" },
          { validator: validAmount, trigger: 'blur'}
        ],
        regDate: [
          { required: true, trigger: "change", message: "请选择工商注册时间" },
        ],
        regType: [
          { required: true, trigger: "change", message: "请选择工商注册类型" },
        ],
        mainProducts: [
          { required: true, message: '请至少选择一个主营产品(服务)所属技术领域', trigger: 'change' }
        ],
        address: [
          { required: true, trigger: "blur", message: "请输入通信地址" },
          { validator: validAdress, trigger: 'blur'},
          {max: 50, message: '最多输入50个字符', trigger: 'blur' }
        ],
        zipCode: [
          { required: true, trigger: "blur", message: "请输入邮政编码" },
          { validator: validNumber, trigger: 'blur'},
          {max: 6, message: '最多输入6位数字', trigger: 'blur' }
        ],
        provinceId: [
          { required: true, trigger: "change", message: "请选择省份" },
        ],
        cityId: [
          { required: true, trigger: "change", message: "请选择市" },
        ],
        districtId: [
          { required: true, trigger: "change", message: "请选择区" },
        ],
        taxOffice: [
          { required: true, trigger: "blur", message: "企业所得税主管税务机关" },
          {max: 30, message: '最多输入30个字符', trigger: 'blur' }
        ],
        legalName: [
          { required: true, trigger: "blur", message: "请输入姓名" },
          {max: 30, message: '最多输入30个字符', trigger: 'blur' }
        ],
        legalIdCard: [
          { required: true, trigger: "blur", message: "请输入身份证号码" },
          { validator: validIDcard, trigger: 'blur'}
        ],
        legalMobile: [
          { required: true, trigger: "blur", message: "请输入手机号码" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        linkName: [
          { required: true, trigger: "blur", message: "请输入姓名" },
          {max: 30, message: '最多输入30个字符', trigger: 'blur' }
        ],
        linkIdCard: [
          { required: true, trigger: "blur", message: "请输入身份证号码" },
          { validator: validIDcard, trigger: 'blur'}
        ],
        linkMobile: [
          { required: true, trigger: "blur", message: "请输入手机号码" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        listedFlag: [
          { required: true, trigger: "change", message: "请选择企业是否上市" },
        ],
        listingCode: [
          { required: true, trigger: "blur", message: "请输入企业上市代码" },
          {max: 30, message: '最多输入30个字符', trigger: 'blur' }
        ],
        investFlag: [
          { required: true, trigger: "change", message: "请选择是否引入风险投资" },
        ],
        investCapital: [
          { required: true, trigger: "blur", message: "请输入风险投资金额" },
          { validator: validAmount, trigger: 'blur'}
        ],
        techFlag: [
          { required: true, trigger: "change", message: "请选择是否为高新企业" },
        ],
        techCode: [
          { required: true, trigger: "blur", message: "请输入高新企业编号" },
          {max: 30, message: '最多输入30个字符', trigger: 'blur' }
        ],
        deductionFlag: [
          { required: true, trigger: "change", message: "请选择是否做加计扣除" },
        ],
        licensePath: [
          { required: true, trigger: "change", message: "请上传营业执照附件" },
        ],
        // logo: [
        //   { required: true, trigger: "change", message: "请上传文件水印" },
        // ],
        serviceDate: [
          { type: 'array', required: true, message: '请选择服务时间', trigger: 'change' }
        ]
      },
      loading: false,
      captchaOnOff: true
    };
  },
  created() {

    this.form.id = this.$route.params.id || undefined;
    this.type = this.$route.query.type || undefined
    if(this.form.id) {
      this.disabledVisible = true
      this.params.dataId = this.$route.params.id
      this.getData()
      this.getEnclosureData()
    }else{
      this.getProvinceData()
    }
  },
  methods: {
    validLicense(val) {
      validLicenseApi(val).then(res => {
        // true存在
        if(res.data) {
          this.$message.error('统一社会信用代码已经存在，不可以重复注册');
        }
      })
    },
    getEnclosureData() {
      getEnclosureApi(this.attachType, this.dataId).then(res => {
        this.list = res.data.records
        this.list.map(item => {
          item.name = item.fileName
        })
        this.$set(this.form, 'fileList', this.list)
      })
    },
    handleUpload() {
      this.enclosureVisible = true
      this.$nextTick(() => {
        this.$refs.uploadEnclosure.init()
      })
    },

    getData() {
      this.loading = true
      getEntInfoByIdApi(this.form.id).then(res => {
        this.loading = false
        this.form = res.data || {}
        // let mainProductArray = this.form.mainProducts.split(',') || ['电子信息'];
        // this.$set(this.form, 'mainProductArray', mainProductArray);
        this.form.serviceDate = []
        if(this.form.startDate) {
          this.form.serviceDate[0] = this.form.startDate
          this.form.serviceDate[1] = this.form.endDate
        }
        getProvinceDataApi().then(res => {
          if(res.code == 200) {
            this.provinceDataList = res.data
          }
        })
        this.form.provinceId = res.data.provinceId
        if(this.form.provinceId) {
          getRegionChildrenApi(this.form.provinceId).then(res => {
            if(res.code == 200) {
              this.cityDataList = res.data
            }
          })
        }
        if(this.form.cityId) {
          getRegionChildrenApi(this.form.cityId).then(res => {
            if(res.code == 200) {
              this.districtDataList = res.data
            }
          })
        }
      })
    },
    handleSuccess(file) {
      this.form.licensePath = file.url;
      this.form.fileName = file.name;
    },
    handleLogoSuccess(file) {
      this.form.logo = file
      this.$refs.logoUpload.clearValidate()
    },
    handleLicenseSuccess(file) {
      this.form.licensePath = file
      this.$refs.licenseUpload.clearValidate()
    },
    handleSaleSuccess(files) {
      this.form.fileList = files
      this.form.attachIds = []
      files.forEach(item => {
        this.form.attachIds.push(item.response.data.id)
      });
    },
    handleFileSuccess(response, file, fileList) {
      console.log("fileList", fileList)
      this.form.attachIds = []
      fileList.forEach(item => {
        if(item.id) {
          this.form.attachIds.push(item.id)
        }else {
          this.form.attachIds.push(item.response.data.id)
        }

      })
    },
    handlePreview(file) {
      console.log(file, "file")
      if (file) {
        const addTypeArray = file.name.split(".");
        const addType = addTypeArray[addTypeArray.length - 1];
        let filePath = ""
        if(file.id) {
          filePath = file.url
        } else {
          filePath = file.response.data.url
        }
        if (addType === "pdf") {
          let routeData = this.$router.resolve({
            path: "/pdfPreview",
            query: { url: filePath },
          });
          window.open(routeData.href, "_blank");
        }
        //".rar, .zip, .doc, .docx, .xls, .txt, .pdf, .jpg,  .png, .jpeg,"
        else if (addType === "doc" || addType === "docx" || addType === "xls" || addType === "xlsx") {
          window.open(filePath);
        } else if (addType === "txt") {
          window.open(filePath);
        } else if (["png", "jpg", "jpeg"].includes(addType)) {
          window.open(filePath);
        } else if (addType === "rar" || addType === "zip") {
          this.$message({
            message: "该文件类型暂不支持预览",
            type: "warning",
          });
           return false;
        }
      }
    },
    beforeRemove(file, fileList) {
      if(this.type == 'update' || this.type == 'updateEnt') {
        if(file.id) {
          const id = file.id
          return this.$confirm('是否确认删除该数据?', "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
          }).then(function() {
            return deleteEnclosureApi(id);
          }).then(() => {
            this.$modal.msgSuccess("删除成功");
          })
        }
      }
    },
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除该数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deleteEnclosureApi(id);
        }).then(() => {
          this.getEnclosureData()
          this.$modal.msgSuccess("删除成功");
        })
    },
    handleRemove(file, fileList) {
      // v-if="type == 'update' || type == 'updateEnt'"
      console.log("handleRemove", fileList, file)
      if(this.type == 'update' || this.type == 'updateEnt') {

      }
    },
    //选择省份
    getCityData() {
      this.form.cityId = ''
      this.form.districtId = ''
      getRegionChildrenApi(this.form.provinceId).then(res => {
        if(res.code == 200) {
          this.cityDataList = res.data

          let obj = this.provinceDataList.find(item => {
            return item.id == this.form.provinceId
          })
          this.provinceName = obj.name
          this.form.taxOffice = '国家税务总局'+this.provinceName+'税务局'

        }
      })
    },
    //选择市
    getDistrictData() {
      this.form.districtId = ''
      getRegionChildrenApi(this.form.cityId).then(res => {
        if(res.code == 200) {
          this.districtDataList = res.data

          let obj = this.cityDataList.find(item => {
            return item.id == this.form.cityId
          })
          this.cityName = obj.name
          this.form.taxOffice = '国家税务总局'+this.cityName+'税务局'

        }
      })
    },
    changeDistrict() {
      let obj = this.districtDataList.find(item => {
        return item.id == this.form.districtId
      })
      this.districtName = obj.name
      this.form.taxOffice = '国家税务总局'+this.districtName+'税务局'
    },
    getProvinceData() {
      getProvinceDataApi().then(res => {
        if(res.code == 200) {
          this.provinceDataList = res.data
          this.form.provinceId = '1481196860266852481'
          getRegionChildrenApi(this.form.provinceId).then(res => {
            if(res.code == 200) {
              this.cityDataList = res.data
              this.form.cityId = '1481196860266852482'
              getRegionChildrenApi(this.form.cityId).then(res => {
                if(res.code == 200) {
                  this.districtDataList = res.data
                }
              })
            }
          })
        }
      })
      this.form.taxOffice = '国家税务总局长沙市税务局'
    },
    handleView(row) {
      // 获取文件路径
      const fileUrl = row.url
      // 获取最后一个.的位置
      const index = fileUrl.lastIndexOf(".")
      // 获取后缀
      const ext = fileUrl.substr(index+1)
      if(ext == 'jpg' || ext == 'png' || ext == 'jpeg') {
        this.dialogVisible = true
        this.dialogImageUrl = fileUrl
      }else {
         window.open(fileUrl)
      }
    },
    cancel() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.go(-1);
    },
    handleRegister() {
      // this.form.mainProducts = this.form.mainProductArray.join(',')
      if(this.type == 'update') {
        this.$refs.form.validate((valid, obj) => {
          if (valid) {
            this.loading = true;
            updateEntInfoApi(this.form).then(res => {
              const userName = this.form.userName;
              this.$notify.success({
                title: '系统提示',
                message: '信息修改成功！',
                showClose: false,
                duration: 2000
              });
              setTimeout(() => {
                this.$store.dispatch("tagsView/delView", this.$route);
                this.$router.go(-1);
              }, 2000);
            }).catch(() => {
              this.loading = false;
            })
          }else {
            const a = []
            for (let key in obj) {
              a.push(obj[key][0].message)
            }
            this.$message({
              message: a[0],
              type: "warning"
            })
            return false
        }
        });
      }else if(this.type == 'add'){

        this.$refs.form.validate((valid, obj) => {
          if (valid) {
            this.loading = true;
            if(this.form.license) {
              validLicenseApi(this.form.license).then(res => {
                // true存在
                if(res.data) {
                  this.$message.error('统一社会信用代码已经存在，不可以重复注册');
                  this.loading = false;
                  return false
                }else {
                  registerEnterUserApi(this.form).then(res => {
                    const userName = this.form.userName;
                    this.$notify.success({
                      title: '系统提示',
                      message: '恭喜你，您的账号注册成功！',
                      showClose: false,
                      duration: 2000
                    });
                    setTimeout(() => {
                      this.$store.dispatch("tagsView/delView", this.$route);
                      this.$router.go(-1);
                    }, 2000);
                  }).catch(() => {
                    this.loading = false;
                  })
                }
              })
            }

          }else {
            const a = []
            for (let key in obj) {
              a.push(obj[key][0].message)
            }
            this.$message({
              message: a[0],
              type: "warning"
            })
            return false
          }
        });
      }else if(this.type == 'updateEnt') {
        this.$refs.form.validate((valid,obj) => {
          if (valid) {
            this.loading = true;
            entUserAudit(this.form).then(res => {
              const userName = this.form.userName;
              this.$notify.success({
                title: '系统提示',
                message: '信息修改成功！',
                showClose: false,
                duration: 2000
              });
              setTimeout(() => {
                this.$store.dispatch("tagsView/delView", this.$route);
                this.$router.go(-1);
              }, 2000);
            }).catch(() => {
              this.loading = false;
            })
          }else {
            const a = []
            for (let key in obj) {
              a.push(obj[key][0].message)
            }
            this.$message({
              message: a[0],
              type: "warning"
            })
            return false
        }
        });
      }

    }
  },
  watch: {
    'form.serviceDate'(val) {
      if(this.form.serviceDate.length > 0) {
        this.form.startDate = this.form.serviceDate[0]
        this.form.endDate = this.form.serviceDate[1]
      }
    }
   }
};
</script>

<style lang="scss" scoped>
  .table_register {
    width: 100%;
  }
</style>
