<template>
  <div class="app-container" v-loading="loading">
    <el-form ref="form" :model="form" :rules="registerRules" label-width="170px">
      <template v-if="type == 'add'">
        <h3 class="toolbar">
          <span class="title">账户信息</span>
        </h3>
        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item label="账户名称" prop="userName">
              <el-input v-model="form.userName" type="text" auto-complete="off" placeholder="账号">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="form.password"
                type="password"
                auto-complete="new-password"
                placeholder="密码">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input
                v-model="form.confirmPassword"
                type="password"
                auto-complete="new-password"
                placeholder="确认密码">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <h3 class="toolbar">
          <span class="title">账户联系人信息</span>
        </h3>
        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item label="姓名" prop="nickName">
              <el-input v-model="form.nickName" type="text" auto-complete="off" placeholder="姓名">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input v-model="form.phonenumber" type="text" auto-complete="off" placeholder="手机号码">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱地址" prop="email">
              <el-input v-model="form.email" type="text" auto-complete="off" placeholder="邮箱地址">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </template>
      <h3 class="toolbar">
        <span class="title">企业信息</span>
      </h3>
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="企业名称" prop="entName">
            <el-input v-model="form.entName" type="text" auto-complete="off" placeholder="请输入企业名称">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="统一社会信用代码" prop="license">
            <el-input type="text"
              v-model="form.license"
              placeholder="请输入统一社会信用代码">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="注册资金" prop="regCapital">
            <el-input type="text"
              v-model="form.regCapital"
              placeholder="请输入注册资金">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工商注册时间" prop="regDate">
            <el-date-picker v-model="form.regDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工商注册类型" 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>
        </el-col>
        <el-col :span="24">
          <el-form-item label="主营产品(服务)所属技术领域" prop="mainProductArray">
            <el-checkbox-group v-model="form.mainProductArray">
              <el-checkbox v-for="(item, index) in mainProductsOptions" :label="item.dictLabel" :key="index">{{item.dictLabel}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="通信地址" prop="address">
            <el-input v-model="form.address" type="text" auto-complete="off" placeholder="请输入通信地址">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮政编码" prop="zipCode">
            <el-input v-model="form.zipCode" type="text" auto-complete="off" placeholder="请输入邮政编码">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="企业所属行政区域" 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="7">
          <el-form-item prop="cityId" label-width="60px">
            <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 @change="changeDistrict">
              <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>
        <el-col :span="12">
          <el-form-item label="企业所得税主管税务机关" prop="taxOffice">
            <el-input v-model="form.taxOffice" type="text" auto-complete="off" placeholder="请输入企业所得税主管税务机关">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <h3 class="toolbar">
        <span class="title">法人信息</span>
      </h3>
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="姓名" prop="legalName">
            <el-input v-model="form.legalName" type="text" auto-complete="off" placeholder="请输入法人姓名">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证号码" prop="legalIdCard">
            <el-input v-model="form.legalIdCard" type="text" auto-complete="off" placeholder="请输入法人身份证号码">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号码" prop="legalMobile">
            <el-input v-model="form.legalMobile" type="text" auto-complete="off" placeholder="手机号码">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="固定电话" prop="legalPhone">
            <el-input v-model="form.legalPhone" type="text" auto-complete="off" placeholder="请输入固定电话">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="传真" prop="legalFax">
            <el-input v-model="form.legalFax" type="text" auto-complete="off" placeholder="请输入法人传真">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="E-mail" prop="legalEmail">
            <el-input v-model="form.legalEmail" type="text" auto-complete="off" placeholder="请输入法人邮箱">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <h3 class="toolbar">
        <span class="title">联系人信息</span>
      </h3>
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="姓名" prop="linkName">
            <el-input v-model="form.linkName" type="text" auto-complete="off" placeholder="请输入联系人姓名">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证号码" prop="linkIdCard">
            <el-input v-model="form.linkIdCard" type="text" auto-complete="off" placeholder="请输入联系人身份证号码">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号码" prop="linkMobile">
            <el-input v-model="form.linkMobile" type="text" auto-complete="off" placeholder="请填入手机号码">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="固定电话" prop="linkPhone">
            <el-input v-model="form.linkPhone" type="text" auto-complete="off" placeholder="请输入联系人固定电话">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="传真" prop="linkFax">
            <el-input v-model="form.linkFax" type="text" auto-complete="off" placeholder="请输入联系人传真">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="E-mail" prop="linkEmail">
            <el-input v-model="form.linkEmail" type="text" auto-complete="off" placeholder="请输入联系人邮箱">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <h3 class="toolbar">
        <span class="title">企业上市信息</span>
      </h3>
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="企业是否上市" 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>
        </el-col>
        <el-col :span="12">
          <el-form-item label="企业上市代码" prop="listingCode">
            <el-input v-model="form.listingCode" type="text" auto-complete="off" placeholder="请输入企业上市代码">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <h3 class="toolbar">
        <span class="title">风险投资信息</span>
      </h3>
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="是否引入风险投资" 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>
        </el-col>
        <el-col :span="12">
          <el-form-item label="风险投资金额" prop="investCapital">
            <el-input v-model="form.investCapital" type="text" auto-complete="off" placeholder="请输入风险投资金额">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <h3 class="toolbar">
        <span class="title">高新企业</span>
      </h3>
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="是否为高新企业" 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>
        </el-col>
        <el-col :span="12">
          <el-form-item label="高新企业编号" prop="techCode">
            <el-input v-model="form.techCode" type="text" auto-complete="off" placeholder="请输入高新企业编号">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <h3 class="toolbar">
        <span class="title">上年度是否做加计扣除</span>
      </h3>
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="上年度是否做加计扣除" 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>
        </el-col>
      </el-row>
      <h3 class="toolbar">
        <span class="title">附件</span>
      </h3>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="企业营业执照" prop="licensePath">
            <FileUpload :file="{url: form.licensePath, name: form.fileName}" @input="handleSuccess">上传材料</FileUpload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="其他附件" prop="attachIds">
            <FileUpload :files="form.attachIds" @input="handleSaleSuccess" :multiple="true">上传材料</FileUpload>
          </el-form-item>
        </el-col>
      </el-row>
      <h3 class="toolbar">
        <span class="title">文件水印</span>
      </h3>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="文件水印" prop="logo">
            <ImageUpload @input="handleLogoSuccess"></ImageUpload>
          </el-form-item>
        </el-col>
      </el-row>
      <h3 class="toolbar">
        <span class="title">服务时间</span>
      </h3>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="服务时间段" prop="serviceDate">
            <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>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <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;">取消</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>

import { getEntInfoByIdApi,updateEntInfoApi } from "@/api/admin/ent/ent"
import { registerEnterUserApi } from "@/api/admin/ent/ent";
import {getProvinceDataApi,getRegionChildrenApi} from "@/api/common/common"
import { regTypeOptions,mainProductsOptions,yesOrNoOptions } from "@/utils/dataFormat"
import FileUpload from '@/components/FileUpload'
import ImageUpload from '@/components/ImageUpload'

export default {
  name: "First",
  components: {
    FileUpload,
    ImageUpload
  },
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.form.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      type: this.$route.query.type,
      activeName: '3',
      provinceName: '',
      cityName: '',
      districtName: '',
      codeUrl: "",
      regTypeOptions: regTypeOptions,
      mainProductsOptions: mainProductsOptions,
      yesOrNoOptions: yesOrNoOptions,
      provinceDataList: [],
      cityDataList: [],
      districtDataList: [],
      form: {
        serviceDate: [],
        listedFlag: 1,
        investFlag: 1,
        startDate: '',
        endDate: '',
        attachIds: [],
        provinceId: '',
        cityId: '',
        districtId: '',
        mainProductArray: ['电子信息'],
        mainProducts: '',
        userName: "",
        password: "",
        confirmPassword: "",
        code: "",
        // uuid: ""
      },
      registerRules: {
        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" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaOnOff: true
    };
  },
  created() {
    this.getProvinceData()
    this.form.id = this.$route.params.id || undefined;
    this.type = this.$route.query.type || undefined
    if(this.form.id) {
      this.getData()
    }
  },
  methods: {
    getData() {
      this.loading = true
      getEntInfoByIdApi(this.form.id).then(res => {
        this.loading = false
        this.form = res.data || {}
        this.form.listedFlag = res.data.listedFlag + ''
        this.form.investFlag = res.data.investFlag + ''
        this.form.techFlag = res.data.techFlag + ''
        this.form.deductionFlag = res.data.deductionFlag +''
        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
        }
        if(this.form.provinceId) {
          this.getCityData()
        }
        if(this.form.cityId) {
          this.getDistrictData()
        }
      })
    },
    handleSuccess(file) {
      this.form.licensePath = file.url;
      this.form.fileName = file.name;
    },
    handleLogoSuccess(file) {
      this.form.logo = file
    },
    handleSaleSuccess(files) {
      files.forEach(item => {
        this.form.attachIds.push(item.response.data.id)
      });
    },
    //选择省份
    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
        }
      })
    },
    handleRegister() {
      this.form.mainProducts = this.form.mainProductArray.join(',')
      if(this.type == 'update') {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true;
            updateEntInfoApi(this.form).then(res => {
              const userName = this.form.userName;
              this.$alert("<font color='red'>恭喜你，您的账号 " + userName + " 信息修改成功！</font>", '系统提示', {
                dangerouslyUseHTMLString: true,
                type: 'success'
              }).then(() => {
                this.$store.dispatch("tagsView/delView", this.$route);
                this.$router.go(-1);
              }).catch(() => {});
            }).catch(() => {
              this.loading = false;
            })
          }
        });
      }else {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true;
            registerEnterUserApi(this.form).then(res => {
              const userName = this.form.userName;
              this.$alert("<font color='red'>恭喜你，您的账号 " + userName + " 注册成功！</font>", '系统提示', {
                dangerouslyUseHTMLString: true,
                type: 'success'
              }).then(() => {
                this.$store.dispatch("tagsView/delView", this.$route);
                this.$router.go(-1);
              }).catch(() => {});
            }).catch(() => {
              this.loading = 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 rel="stylesheet/scss" lang="scss">

</style>
