<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="80"
        >
          <Form-item label="按钮名称" prop="buttonName">
            <el-input
              v-model="formObj.buttonName"
              name="buttonName"
              readonly
              placeholder="请选择按钮"
              @click.native="_selectZtree"
            />
          </Form-item>
          <Form-item label="按钮编码" prop="key">
            <el-input v-model="formObj.key" name="key" placeholder="请输入按钮编码，只读" readonly />
          </Form-item>
          <Form-item label="导出方式" prop="mode">
            <el-select v-model="formObj.mode" placeholder="请选择导出方式" style="width: 100%" @change="_exportChange">
              <el-option
                v-for="item in modeList"
                :key="item.value"
                :label="item.name"
                :value="item.name"
              />
            </el-select>
          </Form-item>
          <Form-item v-show="!isApi" label="报表名称" prop="reportName">
            <el-input
              v-model="formObj.reportName"
              readonly
              placeholder="请选择报表"
              @click.native="_selectReport"
            />
          </Form-item>
          <Form-item v-show="isApi" label="url" prop="uri">
            <el-input v-model="formObj.uri" name="uri" placeholder="请输入url" clearable/>
          </Form-item>
          <Form-item label="备注" prop="remark">
            <el-input v-model="formObj.remark" name="remark" placeholder="请输入备注" clearable/>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
    <!--所属按钮tree-->
    <selectBtnReportTree ref="treeModal" @on-result-change="_treeData" />
    <!--报表列表-->
    <selReportXMLModal ref="reportModal" @on-result-change="_reportChange" />
  </div>
</template>
<script>
/**
 * 添加编辑报表配置
 */
import { printExport } from '../../../api'
import selectBtnReportTree from '../resource/tree/selectBtnReportTree'
import selReportXMLModal from '../report-design/selReportXMLModal'

export default {
  components: { selectBtnReportTree, selReportXMLModal },
  data() {
    const validateName = (rule, value, callback) => {
      // 验证报表名称
      if (this.formObj.reportName === '' && this.isApi === false) {
        callback(new Error('报表名称不能为空！'))
      } else {
        callback()
      }
    }
    const validateUrl = (rule, value, callback) => {
      // 验证url
      if (this.formObj.uri === '' && this.isApi === true) {
        callback(new Error('url不能为空！'))
      } else {
        callback()
      }
    }
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        reportName: '',
        key: '',
        remark: '',
        buttonName: '',
        mode: 'excel',
        service: '',
        reportId: '',
        uri: ''
      },
      ruleValidate: {
        reportName: [
          { required: true, validator: validateName, trigger: 'blur' }
        ],
        uri: [{ required: true, validator: validateUrl, trigger: 'blur' }],
        key: [
          { required: true, message: '按钮权限编码不能为空', trigger: 'blur' }
        ],
        buttonName: [
          { required: true, message: '按钮名称不能为空', trigger: 'blur' }
        ]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      judgeList: [{ value: 0, name: '否' }, { value: 1, name: '是' }],
      modeList: [
        { value: 0, name: 'word' },
        { value: 1, name: 'excel' },
        { value: 2, name: 'pdf' },
        { value: 3, name: 'preview' },
        { value: 4, name: 'sheet' },
        { value: 5, name: 'api' } // url形式
      ],
      isApi: false // 选择方式是否为api
    }
  },
  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')
          if (this.formObj.reportId) {
            data.reportId = this.formObj.reportId
          }
          data.service = this.formObj.service
          data.mode = this.formObj.mode
          this._save(data)
        } else {
          this.$message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    // 添加、编辑保存
    _save: async function(data) {
      // 添加、编辑
      if (this.$string(this.id).isEmpty()) {
        const result = await printExport.add(data)
        this._resultChange(result, '添加成功')
      } else {
        const result = await printExport.edit({
          id: this.id,
          obj: data
        })
        this._resultChange(result, '编辑成功')
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _open(treeObj, lastNode, formObj) {
      this.showEditModal = true
      this.$nextTick(() => {
        this.$refs.formObj.resetFields()
        this.isApi = false
        this.showModal = true
        this._hideLoading()
        if (this.$string(formObj).isEmpty()) {
          this.modalTitle = '添加报表'
          this.id = ''
          if (JSON.stringify(treeObj) !== '{}') {
            if (treeObj.uri === 'reportBtn') {
              // 【是否报表】为是的才赋值
              this.formObj.key = treeObj.code
              this.formObj.buttonName = treeObj.name
              this.formObj.service = lastNode.code
            } else {
              this._treeEmpty()
            }
          } else {
            this._treeEmpty()
          }
        } else {
          this.id = formObj.id
          const tempData = formObj
          this.formObj = {
            reportName: tempData.reportName ? tempData.reportName : '',
            key: tempData.key,
            remark: tempData.remark ? tempData.remark : '',
            buttonName: tempData.buttonName ? tempData.buttonName : '',
            mode: tempData.mode,
            service: tempData.service,
            reportId: tempData.reportId ? tempData.reportId : '',
            uri: tempData.uri ? tempData.uri : ''
          }

          if (tempData.mode === 'api') {
            this.isApi = true
          } else {
            this.isApi = false
          }
          this.modalTitle = '编辑报表'
        }
      })
    },
    _treeEmpty() {
      this.formObj.key = ''
      this.formObj.buttonName = ''
      this.formObj.service = ''
    },
    // 选择tree
    _selectZtree() {
      this.$refs.treeModal._open()
    },
    _treeData(result, lastNode) {
      this.formObj.buttonName = result.name
      this.formObj.key = result.code
      this.formObj.service = lastNode.code
    },
    // 选择报表
    _selectReport() {
      this.$refs.reportModal._open()
    },
    // 报表返回的数据
    _reportChange(data) {
      this.formObj.reportId = data[0].id
      this.formObj.reportName = data[0].name
    },
    // 选择导出方式
    _exportChange(val) {
      if (val === 'api') {
        this.isApi = true
        this.formObj.reportName = ''
        this.formObj.reportId = ''
      } else {
        this.isApi = false
        this.formObj.uri = ''
      }
    }
  }
}
</script>
