<template>
  <el-dialog title="试题" :close-on-click-modal="false" append-to-body :visible.sync="visible" width="800px" @close="closeDialog">
    <div>
      <el-form ref="dialogForm" :model="dialogForm" :rules="dataRule" label-position="left" @keyup.enter.native="dialogFormSubmit()">
        <el-row>
          <el-col :span="24">
            <el-form-item label="题目" prop="question">
              <div>{{ dialogForm.question }}</div>
              <img v-if="dialogForm.imageUrl" :src="dialogForm.imageUrl" width="200" alt="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="替换图片">
            <el-upload
              action="#"
              accept=".png,.jpg,.jpeg,.gif"
              :limit="1"
              :http-request="handleImport"
              :on-exceed="handleExceed"
              :show-file-list="false"
            >
              <img v-if="dialogForm.imageUrl" :src="dialogForm.imageUrl" style="width: 300px" />
              <i v-else class="el-icon-plus" />
            </el-upload>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="选项" prop="question">
              <span v-if="dialogForm.chooseA"
                >A:{{ dialogForm.chooseA
                }}<span>
                  <span v-if="dialogForm.chooseB">B:{{ dialogForm.chooseB }} </span>
                  <span v-if="dialogForm.chooseC">C:{{ dialogForm.chooseC }} </span>
                  <span v-if="dialogForm.chooseD">D:{{ dialogForm.chooseD }}</span>
                  <span v-if="dialogForm.chooseE">E:{{ dialogForm.chooseE }}</span>
                  <span v-if="dialogForm.chooseF">F:{{ dialogForm.chooseF }}</span>
                  <span v-if="dialogForm.chooseG">G:{{ dialogForm.chooseG }}</span>
                </span></span
              ></el-form-item
            >
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="答案" prop="trueAnswer">
              <span>{{ dialogForm.trueAnswer }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="科目" prop="subject">
              <span v-if="dialogForm.subject == 1">科一</span>
              <span v-if="dialogForm.subject == 4">科四</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="精选500题" prop="isVip">
              <el-radio-group v-model="dialogForm.isVip" size="small">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="精选600题" prop="isVip2">
              <el-radio-group v-model="dialogForm.isVip2" size="small">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否易错题" prop="isError">
              <el-radio-group v-model="dialogForm.isError" size="small">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否新规" prop="isNew">
              <el-radio-group v-model="dialogForm.isNew" size="small">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否密卷1" prop="isExam1">
              <el-radio-group v-model="dialogForm.isExam1" size="small">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否密卷2" prop="isExam2">
              <el-radio-group v-model="dialogForm.isExam2" size="small">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="24">
            <el-form-item label="考点" prop="examKeys">
              <el-input v-model="dialogForm.examKeys" maxlength="200" placeholder="请输入考点" clearable />
              <span style="color:red;">包含多个考点,以英文";"分割！</span>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="24">
            <el-form-item label="答题技巧" prop="skillInfo">
              <el-input v-model="dialogForm.skillInfo" maxlength="200" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button plain @click="visible = false">取消</el-button>
      <el-button v-jclick type="primary" :disabled="!canSubmit" @click="dialogFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updateQuestion, uploadFile } from '@/api/question';
// import axios from 'axios';
export default {
  data() {
    return {
      visible: false,
      canSubmit: true,
      dialogForm: {
        questionId: undefined,
        isVip: 0,
        isVip2: 0,
        isError: 0,
        isNew: 0,
        isExam1: 0,
        isExam2: 0,
        examKeys: undefined
      },
      dataRule: {
        schoolName: [{ required: true, message: '驾校名称不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
      }
    };
  },
  methods: {
    init(info = undefined) {
      // debugger
      this.visible = true;
      this.$nextTick(() => {
        this.resetDialogForm();
        // this.$refs['dialogForm'].resetFields();
        if (info) {
          this.dialogForm = this.deepClone(info);
          this.fileUrl = info.imageUrl;
        }
      });
    },
    resetDialogForm() {
      this.dialogForm = {
        questionId: undefined,
        isVip: 0,
        isVip2: 0,
        isError: 0,
        isNew: 0,
        isExam1: 0,
        isExam2: 0,
        examKeys: undefined
      };
    },
    closeDialog() {
      this.$emit('update:dialogVisible', false);
    },
    // 表单提交
    dialogFormSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          updateQuestion(this.dialogForm).then((response) => {
            if (response.code == 200) {
              this.$modal.msgSuccess('修改成功');
              this.$emit('update');
              // this.visible = true;
            }
          });
        }
      });
    },
    handleImport(opt) {
      const data = new FormData();
      data.append('file', opt.file);
      // axios({
      //   method: 'post',
      //   url: 'http://xj.ahduima.com/xunjia/tiku/question/upload',
      //   data: data
      // }).then((resp) => {
      //   console.log(resp.data);
      //   if (resp.data.code == 200) {
      //     this.$modal.msgSuccess('文件上传成功');
      //     this.dialogForm.imageUrl = resp.data.msg;
      //   }
      //   console.log(this.dialogForm)
      // });
      // 上传
      uploadFile(data).then(resp => {
        if (resp.code == 200) {
          this.$modal.msgSuccess('文件上传成功');
          this.dialogForm.imageUrl = resp.msg;
        }
      });
    },
    handleExceed(files) {
      this.handleImport({ file: files[0] });
    }
  }
};
</script>
