<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showEditModal"
      :close-on-click-modal="false"
      width="700px"
      :title="modalTitle"
    >
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="90">
          <Form-item label="模块名称" prop="moduleId">
            <el-select v-model="formObj.moduleId" placeholder="请选择模块名称" style="width:100%">
              <el-option
                v-for="item in moduleList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </Form-item>
          <Form-item label="执行 SQL" prop="sql">
            <el-input v-model="formObj.sql" name="sql" placeholder="请输入执行 SQL" clearable />
          </Form-item>
          <Form-item label="模板 HTML" prop="html">
            <el-input
              v-model="formObj.html"
              name="html"
              placeholder="请输入html模板..."
              type="textarea"
              :autosize="{minRows: 6,maxRows: 15}"
            />
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
/**
 * 添加编辑打印模板表
 */
import { printTemplate, printModule } from '../../../api'

export default {
  data() {
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        moduleId: '',
        sql: '',
        html: ''
      },
      ruleValidate: {
        moduleId: [
          { required: true, message: '模块名称不能为空', trigger: 'change' }
        ],
        sql: [{ required: true, message: '执行 SQL不能为空', trigger: 'blur' }],
        html: [
          { required: true, message: '模板 HTML不能为空', trigger: 'blur' }
        ]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      moduleList: []
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showEditModal = false
        this.$message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          data.moduleId = this.formObj.moduleId
          this._save(data)
        } else {
          this.$message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    // 添加、编辑保存
    _save: async function(data) {
      // 添加
      if (this.$string(this.id).isEmpty()) {
        const result = await printTemplate.add(data)
        this._resultChange(result, '添加成功')
      } else {
        // 编辑
        const result = await printTemplate.edit({
          id: this.formObj.id,
          obj: data
        })
        this._resultChange(result, '编辑成功')
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _open(formObj) {
      this.showEditModal = true
      this.$nextTick(() => {
        this.$refs.formObj.resetFields()
        this._getModuleList()
        this._hideLoading()
        if (this.$string(formObj).isEmpty()) {
          this.id = ''
          this.modalTitle = '添加打印模板'
        } else {
          this.id = formObj.id
          this.formObj = formObj
          this.modalTitle = '编辑打印模板'
        }
      })
    },
    // 获取模版列表
    _getModuleList: async function() {
      this.moduleList = await printModule.getModule()
    }
  }
}
</script>
