<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"
              readonly
              placeholder="请选择所属按钮"
              name="buttonName"
              @click.native="_selectZtree"
            />
            <input v-model="formObj.resourceId" name="resourceId" type="hidden">
            <input v-model="formObj.formId" name="formId" type="hidden">
          </Form-item>
          <Form-item label="表单名称" prop="name">
            <el-input
              v-model="formObj.name"
              readonly
              placeholder="请选择表单"
              @click.native="_selectForm"
            />
          </Form-item>
          <Form-item label="备注" prop="remark">
            <el-input v-model="formObj.remark" name="remark" placeholder="请输入备注" clearable />
          </Form-item>
          <Form-item label="状态" prop="status" style="width: 100%">
            <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>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
    <!--选择带表单的按钮btn-form-tree-->
    <selectBtnFormTree ref="treeModal" @on-result-change="_treeData" />
    <!--表单列表-->
    <selFormModal ref="formModal" @on-result-change="_reportChange" />
  </div>
</template>
<script>
/**
 * 添加编辑表单配置
 */
import { sysFormRelResource } from '../../../api'
import selectBtnFormTree from '../resource/tree/selectBtnFormTree'
import selFormModal from '../form-manage/selectFormModal'

export default {
  components: { selectBtnFormTree, selFormModal },
  data() {
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        buttonName: '',
        resourceId: '',
        name: '',
        formId: '',
        remark: '',
        status: 1
      },
      ruleValidate: {
        buttonName: [
          { required: true, message: '按钮名称不能为空', trigger: 'blur' }
        ],
        name: [{ required: true, message: '表单名称不能为空', trigger: 'blur' }]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      statusList: [{ value: 1, label: '正常' }, { value: 0, label: '禁用' }]
    }
  },
  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.status = this.formObj.status
          this._save(data)
        } else {
          this.$message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    // 添加、编辑保存
    _save: async function(data) {
      // 添加、编辑
      if (this.$string(this.id).isEmpty()) {
        const result = await sysFormRelResource.add(data)
        this._resultChange(result, '绑定成功')
      } else {
        const result = await sysFormRelResource.edit({
          id: this.id,
          obj: {
            remark: this.formObj.remark,
            resourceId: this.formObj.resourceId,
            formId: this.formObj.formId,
            status: this.formObj.status
          }
        })
        this._resultChange(result, '编辑成功')
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _open(treeObj, lastNode, formObj) {
      this.showEditModal = true
      this.$nextTick(() => {
        this.$refs.formObj.resetFields()
        this._hideLoading()
        if (this.$string(formObj).isEmpty()) {
          this.id = ''
          this.modalTitle = '绑定表单与按钮关联关系'
          if (JSON.stringify(treeObj) !== '{}') {
            if (treeObj.uri === 'formBtn') {
              // 【是否表单】为是的才赋值
              this.formObj.resourceId = treeObj.id
              this.formObj.buttonName = treeObj.name
              this._editFormTree()
            } else {
              this._treeEmpty()
            }
          } else {
            this._treeEmpty()
          }
        } else {
          this.id = formObj.relId
          const tempData = formObj
          this.formObj = {
            buttonName: tempData.buttonName ? tempData.buttonName : '',
            resourceId: tempData.buttonId ? tempData.buttonId : '',
            formId: tempData.id ? tempData.id : '',
            name: tempData.name ? tempData.name : '',
            remark: tempData.buttonRemark ? tempData.buttonRemark : '',
            status: tempData.status
          }
          this.modalTitle = '编辑表单与按钮关联关系'
          this._editFormTree()
        }
      })
    },
    _treeEmpty() {
      this.formObj.resourceId = ''
      this.formObj.buttonName = ''
    },
    // 选择tree
    _selectZtree() {
      this.$refs.treeModal._open(this.formObj.resourceId)
      this._editFormTree()
    },
    _treeData(result) {
      this.formObj.buttonName = result.name
      this.formObj.resourceId = result.id
    },
    // 回显选中的节点tree
    _editFormTree() {
      this.$refs.treeModal._editFormZtree(this.formObj.resourceId)
    },
    // 选择表单
    _selectForm() {
      this.$refs.formModal._open()
    },
    // 表单返回的数据
    _reportChange(data) {
      this.formObj.formId = data[0].id
      this.formObj.name = data[0].name
    },
    _selectStatus(data) {
      this.formObj.status = data
    }
  }
}
</script>
