<template>
  <base-model :config="config">
    <div slot="dialog-body">
      <el-form ref="fileForm" :model="form" label-width="100px" size="small" :rules="rules">
        <div class="base-dialog-body">
          <el-form-item :label="$t('dictionary:file:form:name')" prop="Name">
            <el-input v-model="form.Name" />
          </el-form-item>
          <el-form-item :label="$t('dictionary:file:form:nameCN')" prop="NameCN">
            <el-input v-model="form.NameCN" />
          </el-form-item>
          <el-form-item :label="$t('dictionary:browser:form:ArchiveTypeEnum')" prop="ArchiveTypeEnum">
            <el-select v-model="form.ArchiveTypeEnum" clearable placeholder="" style="width: 100%">
              <el-option v-for="item in $d.ArchiveType" :key="item.id" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('dictionary:browser:form:SubIdentificationEnum')" prop="SubIdentificationEnum">
            <el-select style="width: 100%" v-model="form.SubIdentificationEnum" clearable placeholder="">
              <el-option v-for="item in $d.SubIdentification" :key="item.id" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('dictionary:browser:form:SysTemplateTypeEnum')" prop="SysTemplateTypeEnum">
            <el-select style="width: 100%" v-model="form.SysTemplateTypeEnum" clearable placeholder="">
              <el-option v-for="item in $d.SysTemplateType" :key="item.id" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('dictionary:file:form:ShowOrder')" prop="ShowOrder">
            <el-input-number v-model="form.ShowOrder" :step="1" step-strictly></el-input-number>
          </el-form-item>
          <el-form-item :label="$t('dictionary:file:form:IsEnable')">
            <el-switch v-model="form.IsEnable" :active-value="true" :inactive-value="false">
            </el-switch>
          </el-form-item>
          <el-form-item :label="$t('dictionary:file:form:IsConfirmRecord')">
            <el-switch v-model="form.IsConfirmRecord" :active-value="true" :inactive-value="false">
            </el-switch>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div slot="dialog-footer">
      <el-button size="small" @click="canel">
        {{ $t('dictionary:browser:button:canel') }}
      </el-button>
      <el-button size="small" type="primary" :loading="loading" @click="save">
        {{ $t('dictionary:browser:button:save') }}
      </el-button>
    </div>
  </base-model>
</template>
<script>
import baseModel from '@/components/BaseModel'
import { addOrUpdateSysFileType } from '@/api/dictionary'
export default {
  props: {
    config: {
      required: true,
      default: () => {
        return {}
      },
    },
    data: {
      required: true,
      default: () => {
        return {}
      },
    },
  },
  components: {
    'base-model': baseModel,
  },
  data() {
    return {
      form: {
        ArchiveTypeEnum: null,
        IsConfirmRecord: true,
        IsEnable: true,
        Name: null,
        NameCN: null,
        SubIdentificationEnum: null,
        ShowOrder: 0,
        SysTemplateTypeEnum: null
      },
      rules: {
        Name: [
          {
            required: true,
            message: this.$t('common:ruleMessage:specify'),
            trigger: ['blur', 'change'],
          },
        ],
        NameCN: [
          {
            required: true,
            message: this.$t('common:ruleMessage:specify'),
            trigger: ['blur', 'change'],
          },
        ],
        SubIdentificationEnum: [
          {
            required: true,
            message: this.$t('common:ruleMessage:select'),
            trigger: ['blur', 'change'],
          },
        ],
        SysTemplateTypeEnum: [
          {
            required: true,
            message: this.$t('common:ruleMessage:select'),
            trigger: ['blur', 'change'],
          },
        ],
        ArchiveTypeEnum: [
          {
            required: true,
            message: this.$t('common:ruleMessage:select'),
            trigger: ['blur', 'change'],
          },
        ],
      },
      fileList: [],
      loading: false,
    }
  },
  created() {
    if (this.config.status === 'edit') {
      Object.keys(this.form).forEach((key) => {
        this.form[key] = this.data[key]
      })
      if (this.form.FileName) {
        this.fileList[0] = {
          name: this.form.FileName,
          path: this.form.Path,
          fullPath: this.form.Path,
        }
      }
    }
  },
  methods: {
    async save() {
      try {
        let validate = await this.$refs.fileForm.validate()
        if (!validate) return false
        this.loading = true
        if (this.config.status === 'edit') {
          this.form.Id = this.data.Id
        }
        let res = await addOrUpdateSysFileType(this.form)
        if (res.IsSuccess) {
          this.$emit('close')
          this.$emit('getList')
        }
      } catch (err) {
        console.log(err)
        this.loading = false
      }
    },
    canel() {
      this.$emit('close')
    },
  },
}
</script>