<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showModal"
      :close-on-click-modal="false"
      width="700px"
      :title="modalTitle"
    >
      <div>
        <Form
          id="edit-form"
          ref="formObj"
          :model="formObj"
          :rules="ruleValidate"
          :label-width="80"
          inline
        >
          <Form-item label="标题" prop="title" class="width-48">
            <el-input v-model="formObj.title" name="title" placeholder="请输入标题" />
          </Form-item>
          <Form-item label="名称" prop="name" class="width-48">
            <el-input v-model="formObj.name" name="name" placeholder="请输入名称" />
          </Form-item>
          <Form-item label="主页" prop="home" class="width-48">
            <el-input v-model="formObj.home" name="home" placeholder="请输入主页" />
          </Form-item>
          <Form-item label="图标" prop="icon" class="width-48">
            <el-input v-model="formObj.icon" name="icon" placeholder="请输入图标" />
          </Form-item>
          <Form-item label="主题" prop="theme" class="width-48">
            <el-input v-model="formObj.theme" name="theme" placeholder="请输入主题" />
          </Form-item>
          <Form-item label="背景" prop="banner" class="width-48">
            <el-input v-model="formObj.banner" name="banner" placeholder="请输入背景" />
          </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="sort" class="width-48">
            <el-input-number
              v-model.number="formObj.sort"
              name="sort"
              placeholder="请输入排序"
              :min="1"
              style="width: 100%"
              size="small"
              controls-position="right"
              step-strictly
            />
          </Form-item>
          <Form-item label="备注" prop="remark" class="width-48">
            <el-input v-model="formObj.remark" name="remark" placeholder="请输入备注" />
          </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 } from '../../../api'

export default {
  data() {
    return {
      id: '',
      modalTitle: '添加系统表',
      formObj: {
        banner: '',
        home: '',
        icon: '',
        name: '',
        remark: '',
        sort: 0,
        status: 1,
        theme: '',
        title: ''
      },
      ruleValidate: {
        banner: [{ required: true, message: '背景不能为空', trigger: 'blur' }],
        home: [{ required: true, message: '主页不能为空', trigger: 'blur' }],
        icon: [{ required: true, message: '图标不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        sort: [
          {
            required: true,
            message: '排序不能为空',
            trigger: 'blur',
            type: 'number'
          }
        ],
        theme: [{ required: true, message: '主题不能为空', trigger: 'blur' }],
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }]
      },
      showModal: false,
      statusList: [{ value: 0, label: '禁用' }, { value: 1, label: '正常' }],
      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 })
          this._save(data)
        } else {
          this.$refs.footerModal._hideLoading()
          this.$message.error('表单验证失败!')
        }
      })
    },
    _save: async function(data) {
      if (this.$string(this.id).isEmpty()) {
        const result = await sysSystem.add(data)
        this._resultChange(result, '添加成功!')
      } else {
        const result = await sysSystem.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 = '编辑平台'
        }
      })
    }
  }
}
</script>
