<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :exportList="exportList"
    :columnParsing="columnParsing"
    :optionsEvensGroup="optionsEvensGroup"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
    :operation="operation()"
  >
    <div class="cartographer_big">
      <el-dialog title="配置" width="100%" :modal="false" :visible.sync="formBool" :before-close="handleClose">
        <zj-page-container v-if="formBool">
          <zj-page-fill class="neibuview">
            <zj-form-container
              ref="formRef"
              :form-data="formData"
              :form-rules="formRules"
              :form-attributes="{ size: 'mini' }"
            >
              <zj-form-module title="基础信息" label-width="140px" :form-data="formData" :form-items="items" />
            </zj-form-container>
          </zj-page-fill>
          <!-- 操作按钮 -->
          <div style="box-sizing: border-box; padding: 10px; text-align: right">
            <el-button size="mini" @click="handleClose">取 消</el-button>
            <el-button v-if="~[0, 1].indexOf(formType)" size="mini" @click="formConfirm" type="primary"
              >确 定</el-button
            >
          </div>
        </zj-page-container>
      </el-dialog>
    </div>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import pagingTransfer from '@/components/paging-transfer.vue'
import {
  companyCaAuthList,
  companyCaAuthListExport,
  companyCaAuthAdd,
  companyCaAuthUpdate,
  companyCaAuthDetail,
  companyCaAuthDel,
  companyCaAuthGetAuth
} from '@/api/enterpriseCaCertification'
import { commonTemplateDownload } from '@/api/common.js'
import operation_mixin from '@/components/template/operation_mixin.js'
import { required, requiredValueMin } from '@/components/template/rules_verify.js'
import { getWebsit } from '@/api/customerManagement.js'
import { EventBus } from '@/utils/eventBus'
import ImageUpload from '@/components/file-upload'

import { lbsAmapRegion } from '@/api/common.js'
import geographicalPosi from '@/components/geographicalPosi/index.vue'
export default {
  components: { TemplatePage },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      // 表格属性
      tableAttributes: {
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],
      formBool: false,
      formType: 0,
      formData: {
        businessLicenseUrl: [],
        companyAddress: '',
        companyCaAuthId: '',
        companyName: '',
        companyWechatId: '',
        companyWechatName: '',
        shortName: '',
        createBy: '',
        createTime: '',
        fadadaAuditFailReason: '',
        fadadaAuthAutoContractId: '',
        fadadaAuthAutoDownloadUrl: '',
        fadadaAuthAutoId: '',
        fadadaAuthAutoStatus: '',
        fadadaAuthAutoUrl: '',
        fadadaAuthAutoViewpdfUrl: '',
        fadadaAuthUrl: '',
        fadadaCustomerId: '',
        fadadaIsAuth: '',
        fadadaStatus: '',
        fadadaTransactionNo: '',
        legalIdCard: '',
        legalMobile: '',
        legalName: '',
        taxNo: '',
        updateBy: '',
        updateTime: '',
        websitId: '',
        websitName: '',
        lng: '',
        lat: ''
      },
      formRules: {},
      websitList: []
    }
  },
  computed: {
    optionsEvensGroup() {
      return [
        [
          [
            this.optionsEvensAuth('add', {
              click: () => {
                this.openWindow(() => {
                  this.formType = 0
                  this.formBool = true
                })
              }
            })
          ]
        ]
      ]
    },

    items() {
      return [
        {
          name: 'el-select',
          md: 6,
          options: this.websitList.map(item => ({ label: item.name, value: item.websitId })),
          attributes: {
            clearable: true,
            filterable: true,
            disabled: false,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '网点名称',
            prop: 'websitId',
            rules: [...required]
          },
          events: {
            change: val => {
              var data = this.websitList.find(item => item.websitId === val)
              this.formData.websitName = data?.name || ''
            }
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '网点简称',
            prop: 'shortName',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 9,
          attributes: {
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '网点地址',
            prop: 'companyAddress',
            rules: [...required]
          }
        },
        {
          name: 'slot-component',
          md: 3,
          formItemAttributes: {
            'label-width': '0px',
            prop: ''
          },
          render: (h, { props }) => {
            return (
              <geographicalPosi
                formData={{
                  lng: this.formData?.lng || '',
                  lat: this.formData?.lat || '',
                  address: this.formData?.companyAddress || ''
                }}
                onSelectPosi={data => {
                  this.formData.lng = data.center[0]
                  this.formData.lat = data.center[1]
                  this.formData.companyAddress = data.name
                }}
              />
            )
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '统一社会信用代码',
            prop: 'taxNo',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '法人代表',
            prop: 'legalName',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '法人联系方式',
            prop: 'legalMobile',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '法人身份证',
            prop: 'legalIdCard',
            rules: [...required]
          }
        },
        {
          md: 24,
          isShow: true,
          name: 'slot-component',
          formItemAttributes: {
            label: '营业执照',
            prop: 'businessLicenseUrl',
            rules: [...required]
          },
          render: (h, { props, onInput }) => {
            var { value } = props
            return (
              <div>
                <ImageUpload fileList={this.formData.businessLicenseUrl} limit={1} fileType={['image']} />
                <h4 style="color:#ffa700">上传图片</h4>
              </div>
            )
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '创建人',
            prop: 'createBy'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '创建时间',
            prop: 'createTime'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '修改人',
            prop: 'updateBy'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '修改时间',
            prop: 'updateTime'
          }
        }
      ]
    }
  },
  methods: {
    // 列表请求函数
    getList: companyCaAuthList,
    exportList: companyCaAuthListExport,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 获取勾选框数据
    selectionChange(data) {
      this.recordSelected = data
    },
    handleClose() {
      this.$refs?.pageRef?.refreshList()
      this.$data.formData = this.$options.data().formData
      this.formType = 0
      this.formBool = false
    },
    formConfirm() {
      this.$refs.formRef.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          ;(this.formType ? companyCaAuthUpdate : companyCaAuthAdd)({
            ...this.formData,
            businessLicenseUrl: this.formData?.businessLicenseUrl?.[0]?.url || ''
          }).then(res => {
            this.$message({ type: 'success', message: '配置成功!' })
            this.$refs.pageRef.refreshList()
            this.handleClose()
          })
        }
      })
    },
    operation() {
      return this.operationBtn({
        edit: {
          click: ({ row, index, column }) => {
            this.openWindow(() => {
              companyCaAuthDetail({
                companyCaAuthId: row.companyCaAuthId
              }).then(res => {
                this.formData = {
                  ...res.data,
                  businessLicenseUrl: res.data?.businessLicenseUrl ? [{ url: res.data?.businessLicenseUrl }] : []
                }
                this.$nextTick(() => {
                  this.formType = 1
                  this.formBool = true
                })
              })
            })
          }
        },
        del: {
          prompt: '此操作将永久删除数据, 是否继续?',
          click: ({ row, index, column }) => {
            companyCaAuthDel({
              companyCaAuthId: row.companyCaAuthId
            }).then(res => {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.$refs?.pageRef?.refreshList()
            })
          }
        },
        authentication: {
          prompt: '是否确定认证?',
          conditions: ({ row, index, column }) => {
            return row.fadadaIsAuth === 'NO'
          },
          click: ({ row, index, column }) => {
            companyCaAuthGetAuth({
              companyCaAuthId: row.companyCaAuthId
            }).then(res => {
              window.open(res.data, '_blank')
              this.$refs?.pageRef?.refreshList()
            })
          }
        },
        authorizeAutomaticSignature: {
          prompt: '是否授权自动签章?',
          conditions: ({ row, index, column }) => {
            return row.fadadaIsAuth === 'YES' && row.fadadaAuthAutoStatus === 'NO' && row.fadadaAuthAutoUrl
          },
          click: ({ row, index, column }) => {
            window.open(row.fadadaAuthAutoUrl, '_blank')
          }
        }
      })
    },
    openWindow(cb) {
      Promise.all([getWebsit({ type: 'C', status: true, level: 1 })]).then(([res1]) => {
        this.websitList = res1.data
        this.$nextTick(() => {
          cb?.()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.neibuview {
  box-sizing: border-box;
  padding-left: 16px;
  ::v-deep & > .zj-page-fill-scroll {
    box-sizing: border-box;
    padding-right: 16px;

    & > div:nth-child(1) {
      margin-top: 20px;
    }
  }
}
</style>
