<template>
  <div class="page">
    <easy-form
      ref="formRef"
      v-model="formData"
      :form-config="formConfig"
      :fields="fields"
      :buttons="buttons"
    />
  </div>
</template>

<script>
import { annualFillBaseGetTempEnt, annualFillBaseSaveEnt } from '@/api/年报填报相关'
import { createGlobalLoading } from '@/utils'
import { email, tel } from '@/utils/validate'
export default {
  props: {
    isView: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {},
      fields: [
        // 报告年度（只读）
        {
          type: 'input',
          prop: 'ancheyear',
          label: '报告年度',
          span: 24,
          required: true,
          isView() {
            return true
          },
          buttons: [
            {
              type: 'primary',
              label: '导入上一年度信息',
              show: () => {
                return !this.isView && this.$route.query.isView !== '1'
              },
              handler: () => {
                return createGlobalLoading(() => {
                  return annualFillBaseGetTempEnt({
                    year: Number(this.$route.query.year) - 1
                  }).then((data) => {
                    this.$refs.formRef.setFieldValue(data, {
                      fieldExclude: ['ancheyear']
                    })
                  })
                })
              }
            },
            {
              type: 'default',
              label: '重置',
              show: () => {
                return !this.isView && this.$route.query.isView !== '1'
              },
              handler: () => {
                return this.load()
              }
            }
          ]
        },
        // 企业名称（只读）
        {
          type: 'input',
          prop: 'entname',
          label: '企业名称',
          span: 24,
          required: true,
          isView() {
            return true
          }
        },
        // 统一社会信用代码/注册号（只读）
        {
          type: 'input',
          prop: 'uniscid',
          label: '统一社会信用代码/注册号',
          span: 24,
          required: true,
          isView() {
            return true
          }
        },
        // 隶属企业名称（分支机构特有字段）
        {
          type: 'input',
          prop: 'dependententname',
          label: '隶属企业名称',
          span: 24,
          required: true
        },
        // 隶属企业统一社会信用代码/注册号（分支机构特有字段）
        {
          type: 'input',
          prop: 'puniscid',
          label: '隶属企业统一社会信用代码/注册号',
          span: 24,
          required: true
        },
        // 企业通讯地址
        {
          type: 'input',
          prop: 'addr',
          label: '企业通讯地址',
          span: 24,
          required: true
        },
        // 邮政编码
        {
          type: 'input',
          prop: 'postalcode',
          label: '邮政编码',
          span: 24,
          required: true
        },
        // 企业联系电话
        {
          type: 'input',
          prop: 'tel',
          label: '企业联系电话',
          span: 24,
          required: true,
          rules: [tel]
        },
        // 电子邮箱
        {
          type: 'input',
          prop: 'email',
          label: '电子邮箱',
          span: 24,
          required: true,
          rules: [email]
        },
        // 企业主营业务活动
        {
          type: 'input',
          prop: 'mainbusiact',
          label: '企业主营业务活动',
          span: 24,
          required: true
        },
        // 从业人数
        {
          type: 'number',
          prop: 'empnum',
          label: '从业人数',
          span: 24,
          required: true
        },
        {
          type: 'radio',
          prop: 'empnumdis',
          label: '从业人数公示',
          options: [
            { label: '公示', value: '1' },
            { label: '不公示', value: '2' }
          ],
          required: true
        },
        // 女性从业人数
        {
          type: 'number',
          prop: 'womempnum',
          label: '（其中女性从业人数）',
          span: 24,
          required: true
        },
        {
          type: 'radio',
          prop: 'womempnumdis',
          label: '（其中女性从业人数）公示',
          options: [
            { label: '公示', value: '1' },
            { label: '不公示', value: '2' }
          ],
          required: true
        },
        // 企业经营状态
        {
          type: 'select',
          prop: 'busst',
          label: '企业经营状态',
          span: 24,
          optionsBaseCode: 'CD24',
          required: true
        },
        // 是否有对外担保
        {
          type: 'radio',
          prop: 'hasGuarantee',
          label: '是否有对外担保',
          span: 24,
          options: [
            { label: '是', value: '1' },
            { label: '否', value: '2' }
          ],
          required: true
        },
        // 是否有网站或网店
        {
          type: 'radio',
          prop: 'hasWebsite',
          label: '是否有网站或网店',
          span: 24,
          options: [
            { label: '是', value: '1' },
            { label: '否', value: '2' }
          ],
          required: true
        }
      ],
      buttons: [
        {
          type: 'primary',
          label: '下一步',
          show: () => {
            return !this.isView && this.$route.query.isView !== '1'
          },
          handler: (scope) => {
            return scope.formInstance.submitForm().then((formData) => {
              formData.bizSource = 6
              return annualFillBaseSaveEnt(formData).then(() => {
                this.$message.success('保存成功')
                this.$router.push({
                  path: '/report/public/index',
                  query: {
                    year: this.$route.query.year
                  }
                })
              })
            })
          }
        }
      ]
    }
  },
  computed: {
    formConfig() {
      return {
        labelPosition: 'top',
        isView: this.isView || this.$route.query.isView === '1'
      }
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      return createGlobalLoading(() => {
        return annualFillBaseGetTempEnt({
          year: this.$route.query.year
        }).then((data) => {
          this.formData = data || {
            ancheyear: this.$route.query.year,
            entname: this.$store.getters.userInfo.entName,
            uniscid: this.$store.getters.userInfo.uniscid
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/bus-page.scss";
.page{
  margin: 24px;
}
</style>
