<template>
  <ai-detail>
    <ai-title slot="title" :title="detailTitle" isShowBottomBorder isShowBack @onBackClick="$emit('back')">
    </ai-title>
    <template #content>
      <ai-card v-if="isEdit" title="基本信息">
        <el-form slot="content" :model="form" label-width="120px" ref="ruleForm" :rules="rules">
          <el-form-item label="题目描述" prop="title">
            <el-input size="small" v-model="form.title" type="textarea" :rows="6" clearable placeholder="请输入..."
                      maxlength="1000"
                      show-word-limit/>
          </el-form-item>
          <el-form-item label="题目类型" prop="type">
            <el-radio-group v-model="form.type" @change="radioChange">
              <el-radio label="1">单选题</el-radio>
              <el-radio label="2">多选题</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="题目选项" prop="items">
            <el-row type="flex" v-for="(question,index) in form.items" :key="index" style="width: 100%; margin-bottom: 20px;">
              <div>
                <el-form-item :prop="'items.' + index + '.content'" :rules="[{ required: true, message: '选项不能为空', trigger: 'blur' }]">
                  <div class="display: flex;">
                    <label>{{map(index)}}</label>
                        <el-input  style="width: 500px; margin: 0 20px;" placeholder="请输入选项内容" size="small" v-model="question.content" clearable></el-input>
                    <el-checkbox :value="question.checked" @change="handleChange(index)">设为答案</el-checkbox>
                    <el-button type="text" icon="iconfont iconDelete" style="margin-top: 6px;"
                                :disabled="form.items.length<=1" @click="handleDel(index)"></el-button>
                  </div>
                </el-form-item>
              </div>
            </el-row>
            <el-button type="text" @click="handleAdd" v-if="form.items && form.items.length<7">添加选项</el-button>
          </el-form-item>
          <el-form-item label="答案解析" prop="analysis">
            <ai-editor v-model="form.analysis" :instance="instance"/>
          </el-form-item>
        </el-form>
      </ai-card>
      <!--详情-->
      <ai-card  class="village_detail" v-else :title="form.title">
        <template #content>
          <ai-wrapper :columnsNumber="2" label-width="80px">
            <ai-info-item label="题目类型：">{{form.type==1?'单选题':'多选题'}}</ai-info-item>
            <ai-info-item label="正确答案：">
              <span
                style="color: #2EA222;">{{form.items && form.items.filter(e=>e.checked).map(e=>e.sort).join(',')}}</span>
            </ai-info-item>
            <ai-info-item label="题目选项：" isLine>
              <div v-for="(item,index) in form.items" :key="index" style="margin-bottom: 8px">
                {{item.sort}}.{{item.content}}
              </div>
            </ai-info-item>
            <ai-info-item label="答案解析：" isLine>
              <span v-html="form.analysis"></span>
            </ai-info-item>
          </ai-wrapper>
        </template>
      </ai-card>
    </template>
    <template #footer>
      <template v-if="isEdit">
        <el-button size="small" @click="$emit('back')">取消</el-button>
        <el-button type="primary" size="small" @click="saveAdd(1)">保存
        </el-button>
      </template>
    </template>
  </ai-detail>
</template>

<script>
  export default {
    name: "questionBankAdd",
    props: {
      instance: Function,
      dict: Object,
      permissions: Function,
      row: Object,
      isEdit: Boolean
    },
    computed: {
      detailTitle() {
        return this.isEdit ? '编辑党史题库' : '党史题库详情'
      }
    },
    data() {
      return {
        form: {
          title: '',
          type: '1',
          analysis: '',
          items: [{
            content: "",
            sort: "A",
            checked: false,
          }]
        },
        rules: {
          title: [{required: true, message: "请填写题目描述"}],
          type: [{required: true, message: "请选择类型"}],
          items: [{required: true, message: ""}],
          analysis: [{required: true, message: "请填写答案解析"}],
        },
        fileList: [],
        isDetail: true,
        count: 0,
        cropOps: {
          width: "336px",
          height: "210px"
        }
      }
    },
    methods: {
      radioChange(){
        this.form.items = [...this.form.items.map(e=>({...e,checked:false}))];
      },
      handleChange(index) {
        if (this.form.type == 1) {
          this.form.items = [...this.form.items.map(e=>({...e,checked:false}))];
          this.form.items[index].checked = !this.form.items[index].checked;
        }else {
          this.form.items[index].checked = !this.form.items[index].checked;
        }
      },
      handleDel(index) {
        this.form.items.splice(index, 1);
      },
      handleAdd() {
        this.form.items.push({
          content: "",
          sort: this.map(this.form.items.length),
          checked: false,
        })
      },
      map(index) {
        return {
          "0": "A",
          "1": "B",
          "2": "C",
          "3": "D",
          "4": "E",
          "5": "F",
          "6": "G",
        }[index]
      },
      downLoad(url) {
        window.open(url);
      },
      saveAdd() {
        this.$refs.ruleForm.validate(v => {
          if (v) {
            if (this.form.items.length < 2) {
              return this.$message.error("题目选项至少保留两项");
            }
            const flag = this.form.items.filter(e=>e.checked).length==0
            if(flag){
              return this.$message.error("请设置题目选项答案");
            }
            this.instance.post('/app/apppartyquestion/addOrUpdate', {
              ...this.form,
              items: this.form.items?.map(e => ({...e, checked: e.checked ? 1 : 0})),
              id: this.row?.id
            }).then((res) => {
              if (res && res.code == 0) {
                this.row?.id ? this.$message.success('修改成功') : this.$message.success('添加成功');
                this.$emit("back");
              }
            })
          }
        })
      },
      // 详情
      checkDetaiList(id) {
        id && this.instance.post('/app/apppartyquestion/queryDetailById', null, {
          params: {id}
        }).then((res) => {
          if (res?.data) {
            const items = res.data.items?.map(e => ({...e, checked: e.checked != 0}));
            this.form = {...res.data, items};
          }
        })
      },
    },
    created() {
      this.dict.load('partyHistoryType0', 'partyPublicCommentStatus')
      this.checkDetaiList(this.row?.id);
    },
  }
</script>

<style scoped lang="scss">
  .cover {
    display: block;
    width: 300px;
    height: 140px;
    margin: 20px auto;
  }

  .add_Party {
    height: 100%;
    position: relative;
    background: #fff;
    display: flex;
    flex-direction: column;

    .detail-content {
      padding-bottom: 80px;
    }

    .el-form {
      width: 760px;
      margin: 18px auto 64px;
      padding-right: 3px !important;

      .el-form-item {
        margin-bottom: 18px;
      }

      .upload-tip {
        position: absolute;
        text-align: left;
        left: 100px;
        top: 5px;

        li {
          color: #888;
          font-size: 12px;
          line-height: 14px;
        }

        b {
          color: red;
          font-size: 14px
        }
      }
    }

    .village_detail {
      margin-top: 16px;
      width: 760px;
      overflow-y: auto;
      border: 1px solid #eee;
      border-radius: 4px;
      display: flex;
      flex-direction: column;
      background: #fff;
      box-sizing: border-box;
      padding: 16px;
    }

    &.isDetail {
      ::v-deep .ai-detail__content {
        background: #f3f6f9;
      }
    }
  }
</style>
