<template>
  <!-- 表单渲染 -->
  <el-dialog append-to-body :close-on-click-modal="false" :before-close="cancelView" :visible="visible" :title="title" width="860px">
    <img v-if="stampImgSrc" :src="stampImgSrc" class="stamp-review">
    <el-form ref="formViewRef" :model="formData" :status-icon="true" label-width="240px">
      <el-form-item label="标识设置：" class="form-cell" prop="supplierLevel">
        <div class="cell-box">
          <el-select v-model="formData.supplierLevel" disabled placeholder="无" class="cell-select">
            <el-option v-for="item in dict.supplier_rank" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="企业名称：" class="form-cell" prop="enterpriseName">
        <div class="cell-box">
          <el-input v-model="formData.enterpriseName" disabled placeholder="请输入企业名称" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item label="企业联系人：" class="form-cell" prop="enterpriseContactPerson">
        <div class="cell-box">
          <el-input v-model="formData.enterpriseContactPerson" disabled placeholder="请输入联系人" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item label="企业联系人手机号：" class="form-cell" prop="enterpriseContactPersonPhone">
        <div class="cell-box">
          <el-input v-model="formData.enterpriseContactPersonPhone" disabled placeholder="请输入手机号" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item label="企业地址：" class="form-cell" prop="enterpriseAddress">
        <div class="cell-box">
          <el-input v-model="formData.enterpriseAddress" disabled placeholder="请输入地址" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item disabled label="企业邮箱：" class="form-cell" prop="enterpriseEmail">
        <div class="cell-box">
          <el-input v-model="formData.enterpriseEmail" disabled placeholder="请输入邮箱" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item label="营业执照：" class="form-cell" prop="businessLicense">
        <div class="cell-box">
          <ul class="el-upload-list el-upload-list--picture-card">
            <li v-for="(item, index) in imgList" :key="index" tabindex="0" class="el-upload-list__item is-success">
              <img :src="imgSrcStart+'/businessLicense/'+item" class="el-upload-list__item-thumbnail">
              <label class="el-upload-list__item-status-label"><i class="el-icon-upload-success el-icon-check" /></label>
              <span class="el-upload-list__item-actions">
                <span class="el-upload-list__item-preview" @click="dialogImgUrl=item;dialogImgVisible=true"><i class="el-icon-zoom-in" /></span>
              </span>
            </li>
          </ul>
          <el-dialog append-to-body :visible.sync="dialogImgVisible">
            <img width="100%" :src="imgSrcStart+'/businessLicense/'+dialogImgUrl">
          </el-dialog>
        </div>
      </el-form-item>
      <el-form-item label="统一社会信用代码：" class="form-cell" prop="unifiedSocialCreditCode">
        <div class="cell-box">
          <el-input v-model="formData.unifiedSocialCreditCode" disabled placeholder="请输入18位统一社会信用代码" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item label="纳税人识别号：" class="form-cell" prop="taxpayerIdentificationNumber">
        <div class="cell-box">
          <el-input v-model="formData.taxpayerIdentificationNumber" disabled placeholder="请输入纳税人识别号" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item label="所属行业：" class="form-cell" prop="industry">
        <div class="cell-box">
          <el-select v-model="formData.industry" disabled placeholder="无" class="cell-select">
            <el-option v-for="item in dict.industry_type" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="登记机关：" class="form-cell" prop="registrationAuthority">
        <div class="cell-box">
          <el-input v-model="formData.registrationAuthority" disabled placeholder="请输入登记机关" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item label="法定代表人：" class="form-cell" prop="legalRepresentative">
        <div class="cell-box">
          <el-input v-model="formData.legalRepresentative" disabled placeholder="请输入法定代表人" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item label="经营状态：" class="form-cell" prop="businessStatus">
        <div class="cell-box">
          <el-select v-model="formData.businessStatus" disabled placeholder="无" class="cell-select">
            <el-option v-for="item in dict.business_status" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="成立日期：" class="form-cell" prop="establishmentDate">
        <div class="cell-box">
          <el-date-picker v-model="formData.establishmentDate" disabled type="date" clearable placeholder="请选择日期" />
        </div>
      </el-form-item>
      <el-form-item label="经营期限（年）：" class="form-cell" prop="operatingPeriod">
        <div class="cell-box">
          <el-input v-model="formData.operatingPeriod" disabled type="number" placeholder="请输入数字" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item label="审核/年检日期：" class="form-cell" prop="annualInspectionDate">
        <div class="cell-box">
          <el-date-picker v-model="formData.annualInspectionDate" disabled type="date" clearable placeholder="请选择日期" />
        </div>
      </el-form-item>
      <el-form-item label="企业类型：" class="form-cell" prop="enterpriseType">
        <div class="cell-box">
          <el-select v-model="formData.enterpriseType" disabled placeholder="无" class="cell-select">
            <el-option v-for="item in dict.company_type" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="注册资本（万元）：" class="form-cell" prop="registeredCapital">
        <div class="cell-box">
          <el-input v-model="formData.registeredCapital" disabled type="number" placeholder="请输入数字" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item label="注册地址：" class="form-cell">
        <div class="cell-box">
          <el-select v-model="formData.registeredAddressProvince" disabled placeholder="省" style="width: 120px" @change="findAreaByCodeAndType(formData.registeredAddressProvince,2, 'cityOpts')">
            <el-option v-for="item in provinceOpts" :key="item.id" :label="item.name" :value="item.code" />
          </el-select>
          <el-select v-model="formData.registeredAddressCity" disabled placeholder="市" style="width: 160px" @change="findAreaByCodeAndType(formData.registeredAddressCity,3, 'countyOpts')">
            <el-option v-for="item in cityOpts" :key="item.id" :label="item.name" :value="item.code" />
          </el-select>
          <el-select v-model="formData.registeredAddressArea" disabled placeholder="区/县" style="width: 160px">
            <el-option v-for="item in countyOpts" :key="item.id" :label="item.name" :value="item.code" />
          </el-select>
        </div>
        <div class="cell-box">
          <el-input v-model="formData.registeredAddressStreet" disabled placeholder="详细地址（例如：和平路北大街106号）" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item label="经营范围：" class="form-cell" prop="businessScope">
        <div class="cell-box">
          <div class="cell-textarea">{{ formData.businessScope }}</div>
        </div>
      </el-form-item>
      <el-form-item label="企业介绍：" class="form-cell" prop="enterpriseIntroduction">
        <div class="cell-box">
          <div class="cell-textarea">{{ formData.enterpriseIntroduction }}</div>
        </div>
      </el-form-item>
      <el-form-item label="主营产品介绍：" class="form-cell" prop="mainProductsIntroduction">
        <div class="cell-box">
          <div class="cell-textarea">{{ formData.mainProductsIntroduction }}</div>
        </div>
      </el-form-item>
      <el-form-item label="业务范围：" class="form-cell" prop="businessScopeIntroduction">
        <div class="cell-box">
          <div class="cell-textarea">{{ formData.businessScopeIntroduction }}</div>
        </div>
      </el-form-item>
      <el-form-item v-if="!isReview && formData.status!=='3'" label="审核意见：" class="form-cell">
        <div class="cell-box">
          <div class="cell-textarea">{{ formData.reviewComments }}</div>
        </div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: center">
      <el-button @click="cancelView">关闭</el-button>
      <el-button v-if="isReview" type="primary" @click="submitForm(true)">通过</el-button>
      <el-button v-if="isReview" type="primary" @click="reviewVisible=true">驳回</el-button>
    </div>
    <el-dialog append-to-body :close-on-click-modal="false" :before-close="cancelReview" :visible="reviewVisible" title="审核意见" width="600px">
      <el-form ref="formReviewRef" :model="formReview" :rules="rules" :status-icon="true" label-width="120px">
        <el-form-item label="审核意见：" class="form-cell" prop="reviewComments">
          <div class="cell-box">
            <el-input v-model="formReview.reviewComments" type="textarea" placeholder="请输入审核意见" maxlength="100" :autosize="{ minRows: 4, maxRows: 6}" show-word-limit resize="none" style="width: 300px" />
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="submitForm(false)">提交</el-button>
      </div>
    </el-dialog>
  </el-dialog>
</template>
<script>
import { getToken } from '@/utils/auth'
import { HttpReq } from '@/api/common'
import review_pass from '@/assets/images/review_pass.png'
import review_await from '@/assets/images/review_await.png'
import review_failed from '@/assets/images/review_failed.png'

export default {
  dicts: ['industry_type', 'supplier_rank', 'company_type', 'business_status'],
  data() {
    return {
      uploadHeaders: { 'Authorization': getToken() },
      stampImgSrc: '',
      imgSrcStart: process.env.VUE_APP_BASE_API,
      dialogImgUrl: '',
      dialogImgVisible: false,
      visible: false,
      title: '客户信息详情',
      customerId: '',
      isReview: false,
      imgList: [],
      provinceOpts: [],
      cityOpts: [],
      countyOpts: [],
      formData: {
        annualInspectionDate: '', // 审核日期
        businessLicense: '', // 营业执照的照片名称
        businessScope: '', // 经营范围
        businessScopeIntroduction: '', // 业务范围介绍
        businessStatus: '', // 经营状态
        enterpriseAddress: '', // 企业地址
        enterpriseContactPerson: '', // 企业联系人
        enterpriseContactPersonPhone: '', // 企业联系人手机号
        enterpriseEmail: '', // 企业邮箱
        enterpriseIntroduction: '', // 企业介绍
        enterpriseName: '', // 企业名称
        enterpriseType: '', // 企业类型
        establishmentDate: '', // 成立日期
        industry: '', // 所属行业
        legalRepresentative: '', // 法定代表人
        mainProductsIntroduction: '', // 主营产品介绍
        operatingPeriod: '', // 经营期限
        registeredAddressArea: '', // 注册地址-区
        registeredAddressCity: '', // 注册地址-市
        registeredAddressProvince: '', // 注册地址-省
        registeredAddressStreet: '', // 注册地址-街道
        registeredCapital: '', // 注册资本
        registrationAuthority: '', // 登记机关
        supplierLevel: '1', // 供应商级别
        taxpayerIdentificationNumber: '', // 纳税人识别号
        unifiedSocialCreditCode: '', // 统一社会信用代码
        reviewComments: '', // 审核意见
        sysUserId: null
      },
      reviewVisible: false,
      formReview: {
        reviewComments: ''
      },
      rules: {
        reviewComments: { required: true, message: '请填写审核意见', trigger: 'blur' }
      }
    }
  },
  mounted() {
    this.$nextTick(() => {

    })
  },
  methods: {
    showView() {
      this.visible = true
    },
    hideView() {
      this.$refs.formViewRef.resetFields()
      this.cityOpts = []
      this.countyOpts = []
      this.imgList = []
      this.visible = false
    },
    cancelView() {
      this.cancelReview()
      this.hideView()
      this.stampImgSrc = ''
    },
    cancelReview() {
      if (this.$refs.formReviewRef) {
        this.$refs.formReviewRef.resetFields()
      }
      this.formReview.reviewComments = ''
      this.reviewVisible = false
    },
    submitForm(isPass) {
      if (isPass) {
        this.formReview.reviewComments = '审核通过'
        HttpReq.backstageApi.reviewCustomer({
          customerId: this.customerId,
          reviewComments: this.formReview.reviewComments,
          type: 1
        }).then((res) => {
          this.$notify({
            title: res.msg,
            type: 'success',
            duration: 2500
          })
          if (res.code === 200) {
            this.cancelView()
            this.$parent.loadData()
          }
        })
        return
      }
      this.$refs.formReviewRef.validate((valid, obj) => {
        if (valid) {
          HttpReq.backstageApi.reviewCustomer({
            customerId: this.customerId,
            reviewComments: this.formReview.reviewComments,
            type: 0
          }).then((res) => {
            this.$notify({
              title: res.msg,
              type: 'success',
              duration: 2500
            })
            if (res.code === 200) {
              this.cancelView()
              this.$parent.loadData()
            }
          })
        } else {
          this.$message({
            message: '表单信息有误，请核对无误后提交！',
            type: 'error'
          })
        }
      })
    },
    findAreaByCodeAndType(code, type, optsName, fn) {
      HttpReq.backstageApi.findAreaByCodeAndType({
        code, type
      }).then((res) => {
        this[optsName] = res
      })
    },
    loadData(customerId, isReview) {
      this.customerId = customerId
      this.isReview = isReview
      this.title = `${isReview ? '审核' : '查看'}客户信息详情`
      this.findAreaByCodeAndType(0, 1, 'provinceOpts')
      HttpReq.backstageApi.queryCustomerDetail({ customerId }).then((res) => {
        if (res.code === 200) {
          this.formData = Object.assign(this.formData, res.data)
          if (this.formData.businessLicense) {
            this.imgList = this.formData.businessLicense.split(';')
          }
          switch (this.formData.status) {
            case '1': this.stampImgSrc = review_pass
              break
            case '3': this.stampImgSrc = review_await
              break
            case '4': this.stampImgSrc = review_failed
              break
            default: this.stampImgSrc = ''
          }
          this.findAreaByCodeAndType(this.formData.registeredAddressProvince, 2, 'cityOpts')
          this.findAreaByCodeAndType(this.formData.registeredAddressCity, 3, 'countyOpts')
          this.showView()
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.stamp-review {
  border-style: none;
  position: absolute;
  right: 15%;
  top: 40%;
  z-index: 99;
}
.cell-box {
  min-width: 120px;
  .cell-input {
    width: 420px;
  }
  .cell-select {
    width: 220px;
  }
  .cell-textarea {
    min-height: 50px;
    color: #777;
    font-style: oblique;
    font-size: 14px;
    text-decoration: underline; //下划线
    text-underline-offset: 5px; //下划线和文字间距
    text-decoration-color: rgba(100, 100, 100, 0.4);
  }
  >>>.el-input__inner {
    border: 1px solid rgba(100, 100, 100, 0.1);
    border-bottom: 1px solid rgba(100, 100, 100, 0.2);
    border-radius: 5px;
  }
  >>>.el-input.is-disabled .el-input__inner {
    border-radius: 0;
    border: 0;
    border-bottom: 1px solid rgba(100, 100, 100, 0.4);
    background: white;
    color: #777;
    font-style: oblique;
    font-size: 14px;
    cursor: text;
  }
  >>>.el-input.is-disabled .el-input__icon {
    cursor: text;
  }
  >>>.el-icon-circle-check {
    color: #13ce66;
  }
  >>>.el-icon-arrow-up:before {
    content: '';
  }
}
</style>
