<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" @click="getDialog()"
          v-hasPermi="['ohplay:programa:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="排序" prop="sort" align="center" />
      <el-table-column label="栏目名称" prop="name" align="center" />
      <el-table-column label="栏目定位" prop="location" align="center" />
      <el-table-column label="当前状态" prop="type" align="center" :formatter="typeFormatter" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDialog(scope.row.id)" v-hasPermi="['ohplay:programa:edit']">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" :title="title" width="500px" :before-close="cancel">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="栏目名称：" prop="name">
          <el-input v-model="form.name" placeholder="请输入栏目名称" />
        </el-form-item>
        <el-form-item label="栏目排序：" prop="sort">
          <el-input-number v-model="form.sort" :min="0" />
        </el-form-item>
        <el-form-item label="栏目状态：" prop="type">
          <el-select v-model="form.type" placeholder="请选择栏目状态">
            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label"
              :value="Number(item.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="栏目定位：" prop="location">
          <el-input v-model="form.location" type="textarea" :rows="4" placeholder="请输入栏目定位" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="getSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, create, edit, detail } from '@/api/ohplay/programa'
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 列表
      tableData: [],
      // 弹窗
      dialogVisible: false,
      title: '',
      // 表单
      form: {},
      // 校验
      rules: {
        name: [{
          required: true, message: '请输入栏目名称', trigger: 'blur'
        }],
        sort: [{
          required: true, message: '请选择栏目排序', trigger: 'change'
        }],
        type: [{
          required: true, message: '请选择栏目状态', trigger: 'change'
        }],
        location: [{
          required: true, message: '请输入栏目定位', trigger: 'blur'
        }]
      },
      // 是否有效
      statusOptions: [{
        value: 0,
        label: '有效'
      }, {
        value: 1,
        label: '无效'
      }]
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      list().then(res => {
        if (res.code === 0) {
          this.tableData = res.data
          this.loading = false
        }
      })
    },

    // 打开弹窗
    getDialog(id) {
      this.dialogVisible = true
      this.title = id ? '编辑' : '新增'
      if (id) {
        detail(id).then(res => {
          if (res.code === 0) {
            this.form = res.data
          }
        })
      }
    },

    // 新增
    getSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id) {
            edit(this.form).then(res => {
              if (res.code === 0) {
                this.$message.success('修改成功！')
                this.dialogVisible = false
                this.getList()
              }
            })
          } else {
            create(this.form).then(res => {
              if (res.code === 0) {
                this.$message.success('创建成功！')
                this.dialogVisible = false
                this.getList()
              }
            })
          }
        } else {
          return false
        }
      })
    },

    // 取消
    cancel() {
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },

    // 字典翻译
    typeFormatter(row) {
      return this.selectDictLabel(this.statusOptions, row.type)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>