<template>
  <div class='app-container'>
    <el-form :model="form" ref="form" :rules="rules" label-width="100px" :disabled="disabled">
      <el-form-item label="协议名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入协议名称" />
      </el-form-item>
      <el-form-item label="协议类型：" prop="type">
        <el-select v-model="form.type" placeholder="请选择协议类型">
          <el-option v-for="item in typeOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="协议内容：" prop="content">
        <Editor v-model="form.content" :height="500" :readOnly="disabled" />
      </el-form-item>
    </el-form>
    <div class="form-btn">
      <el-button @click="cancel">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="getSubmit">确定</el-button>
    </div>
  </div>
</template>

<script>
import Editor from "@/components/Editor/index";
import { submit, detail } from '@/api/operation/agreement'
export default {
  components: {
    Editor
  },
  data() {
    return {
      // 表单
      form: {},
      // 协议类型
      typeOptions: [{
        value: 0,
        label: '用户协议'
      }, {
        value: 1,
        label: '隐私协议'
      }, {
        value: 2,
        label: '音乐服务许可协议'
      }, {
        value: 3,
        label: '儿童隐私保护指南'
      }, {
        value: 4,
        label: 'VIP音乐服务协议'
      }, {
        value: 5,
        label: '爱听付费协议'
      }, {
        value: 6,
        label: '关于我们'
      }],
      // 校验
      rules: {
        name: [{
          required: true, message: '请输入协议名称', trigger: 'blur'
        }],
        type: [{
          required: true, message: '请选择协议类型', trigger: 'change'
        }],
        content: [{
          required: true, message: '请输入协议内容', trigger: 'blur'
        }]
      },
      // 只读
      disabled: this.$route.query.boolean ? true : false
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.getDetail()
    }
  },
  methods: {
    // 详情
    getDetail() {
      detail(this.$route.query.id).then(res => {
        if (res.code === 0) {
          this.form = res.data
        }
      })
    },

    // 取消
    cancel() {
      this.$tab.closeOpenPage('/operation/agreement')
    },
    // 提交
    getSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          submit(this.form).then(res => {
            if (res.code === 0) {
              this.$message.success('提交成功！')
              this.cancel()
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form-item {
  width: 500px;
}
</style>