<!--
 * @Author: niezch@dashoo.cn
 * @Date: 2023-02-16 15:55:15
 * @LastEditors: niezch@dashoo.cn
 * @LastEditTime: 2023-02-17 15:53:42
 * @Description: file content
 * @FilePath: \opms_frontend\src\views\system\report\components\ReportEdit.vue
-->
<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="报表名称" prop="reportName">
        <el-input v-model="form.reportName" placeholder="请输入报表名称" />
      </el-form-item>
      <el-form-item label="报表类型" prop="reportType">
        <el-radio-group v-model="form.reportType">
          <el-radio-group v-model="form.reportType">
            <el-radio label="10">数值指标</el-radio>
            <el-radio label="20">数据报表</el-radio>
          </el-radio-group>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="报表描述" prop="reportDesc">
        <el-input v-model="form.reportDesc" placeholder="请输入报表描述" />
      </el-form-item>
      <el-form-item label="报表代码" prop="reportCode">
        <el-input v-model="form.reportCode" placeholder="请输入报表代码" />
      </el-form-item>
      <el-form-item label="报表图标" prop="reportIcon">
        <el-popover popper-class="icon-selector-popper" trigger="hover" width="292">
          <template #reference>
            <el-input v-model="form.reportIcon" />
          </template>
          <vab-icon-selector @handle-icon="handleIcon" />
        </el-popover>
        <!-- <el-input v-model="form.reportIcon" placeholder="请输入报表图标" /> -->
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入内容" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import reportApi from '@/api/system/report'
  import roleApi from '@/api/system/role'

  export default {
    name: 'PostEdit',
    data() {
      return {
        form: { reportType: '10', reportIcon: undefined },
        rules: {
          reportName: [{ required: true, message: '报表名称不能为空', trigger: 'blur' }],
          reportDesc: [{ required: true, message: '报表描述不能为空', trigger: 'blur' }],
        },
        title: '',
        dialogFormVisible: false,
        // 角色选项
        roleOptions: [],
      }
    },
    mounted() {
      // 获取角色
      roleApi.getList({ status: '10' }).then((response) => {
        this.roleOptions = response.data.list
      })
    },
    methods: {
      handleIcon(item) {
        this.form.icon = item
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.getDetailsInfo(row.id)
        }
        this.dialogFormVisible = true
      },
      async getDetailsInfo(id) {
        const { data: data } = await reportApi.getEntityById({ id: id })
        this.form = data
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              const { msg } = await reportApi.doEdit(this.form)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            } else {
              const { msg } = await reportApi.doAdd(this.form)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            }
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
