<template>
  <el-dialog
    :title="modalTitle"
    :visible.sync="modalVisible"
    :width="formLabelWidth"
    top="2vh"
    @close="handleCancel">
    <div class="mobile">
        <el-form label-width="90px" :model="form" :rules="rules" ref="from" class="from">
            <el-form-item label="问题名称:" prop="problemName">
                <el-input type="text" v-model="form.problemName" :disabled="limit.isRead"></el-input>
            </el-form-item>
            <el-form-item label="问题状态:">
                <el-radio-group v-model="form.state" :disabled="limit.isRead">
                  <el-radio :label="item.id" v-for="(item,index) in issuesSate">{{item.label}}</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="问题内容:" prop="problemAnswer">
                <quill-editor
                  ref="text"
                  :disabled="limit.isRead"
                  v-model="form.problemAnswer"
                  class="myQuillEditor"
                  :options="editorOption" />
            </el-form-item>
        </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="submit" v-if="!limit.isRead">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { addInssues , updateInssues } from "@/api/baseManage/issues";
export default {
  name: 'addMobiel',
  components:{
    quillEditor
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: 'title'
    },
    action: {
      type: String,
      default: 'add'
    },
    selectRow: {
      type: Object,
      default:null
    }
  },
  watch: {
    visible(newVal, oldVal) {
      this.modalVisible = newVal;
    },
    title(newVal, oldVal) {
      this.modalTitle = newVal;
    },
    action(newVal, oldVal) {
      this.modalAction = newVal;
    },
    selectRow(newVal, oldVal) {
      this.modalSelectRow = newVal;
    }
  },
  data() {
    return {
      modalTitle: this.title,
      modalVisible: this.visible,
      modalAction: this.action,
      modalSelectRow: this.selectRow,
      formLabelWidth: '924px',
      //窗口权限
      limit: {
        isRead: false, //是否只读
        isAdd: false //是否可写
      },
      //验证
      rules: {
        problemName: [{ required: true, message: '问题名称不能为空!', trigger: 'blur' }],
      },
      issuesSate:[
        {
          id:0,
          label:'启用'
        },
        {
          id:1,
          label:'禁用'
        }
      ],
      //属性
      content: '',
      editorOption: {},
      //表单
      form: {
        state:0
      }
    };
  },
  created() {
    console.log(this.modalSelectRow)
    switch (this.modalAction) {
      case 'add':
        this.limit.isAdd = true;
        break;
      case 'edit':
        this.limit.isAdd = true;
        this.form = Object.assign({},this.modalSelectRow)
        break;
      case 'view':
        this.limit.isRead = true;
        this.form = Object.assign({},this.modalSelectRow)
        break;
      default:
        break;
    }
  },
  methods: {
    //表单提交
    submit() {
      this.$refs.from.validate(valid => {
        if (valid) {
          let params = Object.assign({},this.form)
          if(this.modalAction === 'add'){
            addInssues(params).then(res => {
                if (res.retHead.errCode === 0) {
                  this.msgSuccess("操作成功");
                  this.modalVisible = false
                 	this.$emit('closeModal', true);
                }
            });
          }else{
            updateInssues(params).then(res => {
                if (res.retHead.errCode === 0) {
                  this.msgSuccess("操作成功");
                  this.modalVisible = false
                 	this.$emit('closeModal', true);
                }
            });
          }
        } else {
          return false;
        }
      });
    },
    //窗口关闭
    handleCancel() {
      this.$emit('closeModal', false);
    }
  }
};
</script>

<style lang="scss" scoped>
  ::v-deep  .ql-editor{
  	height:300px;
  }
</style>
