<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showModal"
      :close-on-click-modal="false"
      :title="modalTitle"
    >
      <div>
        <Form
          id="edit-form"
          ref="formObj"
          :model="formObj"
          :rules="ruleValidate"
          :label-width="80"
          inline
        >
          <Form-item label="模块名称" prop="name" class="width-48">
            <el-tooltip content="只能输入中英文字符且长度为0-40" placement="bottom-start">
              <el-input v-model="formObj.name" name="name" placeholder="请输入模块名称" :maxlength="40" style="width: 100%" />
            </el-tooltip>
          </Form-item>
          <Form-item label="别名" prop="alias" class="width-48">
            <el-tooltip content="长度为0-40" placement="bottom-start">
              <el-input v-model="formObj.alias" name="alias" placeholder="请输入别名" :maxlength="40" style="width: 100%" />
            </el-tooltip>
          </Form-item>
          <Form-item label="系统" prop="systemId" class="width-48">
            <el-select v-model="formObj.systemId" name="systemId" placeholder="请选择系统" style="width: 100%">
              <el-option v-for="system in getSystem" :key="system.id" :value="system.id" :label="system.name" />
            </el-select>
          </Form-item>
          <Form-item label="状态" prop="status" class="width-48">
            <el-radio-group v-model="formObj.status" @change="_selectStatus">
              <el-radio v-for="item in statusList" :key="item.value" :label="item.value">
                {{ item.label }}
              </el-radio>
            </el-radio-group>
          </Form-item>
          <Form-item label="图片" prop="img" class="width-48">
            <el-tooltip content="长度为0-100" placement="bottom-start">
              <el-input v-model="formObj.img" name="img" placeholder="请输入图片地址" :maxlength="100" style="width: 100%" />
            </el-tooltip>
          </Form-item>
          <Form-item label="排序" prop="sort" class="width-48">
            <el-input-number
              v-model.number="formObj.sort"
              name="sort"
              placeholder="请输入排序"
              :min="0"
              style="width: 98%"
              size="small"
              controls-position="right"
              step-strictly
            />
          </Form-item>
          <Form-item label="备注" prop="remark" class="width-48">
            <el-tooltip content="长度为0-100" placement="bottom-start">
              <el-input v-model="formObj.remark" name="remark" placeholder="请输入备注" style="width: 100%" :maxlength="100" />
            </el-tooltip>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { sysSystem, sysModule } from '../../../api'
export default {
  data() {
    const validateName = (rule, value, callback) => {
      // 验证模块名称
      const reg = /[\u4E00-\u9FA5_a-zA-Z]+$/
      if (value === '') {
        callback(new Error('模块名称不能为空！'))
      } else if (!reg.test(value)) {
        callback(new Error('请输入中文或英文字符！'))
      } else {
        callback()
      }
    }
    return {
      id: '',
      modalTitle: '',
      formObj: {
        name: '',
        alias: '',
        status: 'NORMAL',
        systemId: '',
        sort: 0
      },
      getSystem: '',
      statusList: [
        { value: 'DISABLED', label: '禁用' },
        { value: 'NORMAL', label: '正常' }
      ],
      ruleValidate: {
        name: [{ required: true, validator: validateName, trigger: 'blur' }],
        alias: [{ required: true, message: '别名不能为空', trigger: 'blur' }],
        systemId: [
          { required: true, trigger: 'change', message: '系统不能为空' }
        ],
        remark: [{ max: 100, message: '备注不能超过100字符', trigger: 'blur' }]
      },
      showModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  methods: {
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _selectStatus(data) {
      this.formObj.status = data
    },
    _resultChange(result, msg) {
      if (result) {
        this.showModal = false
        this.$message.success(msg)
        this.$emit('on-result-change')
      }
      this.$refs.footerModal._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          this.$extend(data, {
            status: this.formObj.status,
            systemId: this.formObj.systemId
          })
          this._save(data)
        } else {
          this.$refs.footerModal._hideLoading()
          this.$message.error('表单验证失败!')
        }
      })
    },
    _save: async function(data) {
      if (this.$string(this.id).isEmpty()) {
        const result = await sysModule.add(data)
        this._resultChange(result, '添加成功!')
      } else {
        const result = await sysModule.edit({ id: this.formObj.id, obj: data })
        this._resultChange(result, '编辑成功!')
      }
    },
    _cancel() {
      this.$refs.footerModal._hideLoading()
      this.showModal = false
    },
    _open(formObj) {
      this.showModal = true
      this.$nextTick(() => {
        this.$refs.footerModal._hideLoading()
        this.$refs.formObj.resetFields()
        if (this.$string(formObj).isEmpty()) {
          this.id = ''
          this.modalTitle = '添加模块表'
        } else {
          this.id = formObj.id
          this.formObj = formObj
          this.modalTitle = '编辑模块表'
          this.formObj.status = formObj.status.value
        }
        this._getSystemId()
      })
    },
    _getSystemId: async function() {
      this.getSystem = await sysSystem.listIdName()
    }
  }
}
</script>
