<template>
  <ai-detail>
    <ai-title slot="title" :title="detailTitle" isShowBottomBorder isShowBack @onBackClick="$emit('back')">
    </ai-title>
    <template #content>
      <ai-card title="基本信息">
        <template #content>
          <div class="detail-content" v-if="isEdit">
            <el-form class="ai-form" :model="form" label-width="120px" ref="ruleForm" :rules="rules">
              <el-form-item label="课程主题" prop="title">
                <el-input size="small" v-model="form.title" clearable placeholder="请输入..." maxlength="100"
                          show-word-limit/>
              </el-form-item>
              <el-form-item label="更新状态" prop="updateStatus">
                <ai-select v-model="form.updateStatus" :selectList="dict.getDict('classroomUpdateStatus')"/>
              </el-form-item>
              <el-form-item label="发布组织" prop="organizationName">
                <el-input size="small" v-model="form.organizationName" clearable placeholder="请输入..." maxlength="100"
                          show-word-limit/>
              </el-form-item>
              <el-form-item style="width: 100%;" label="封面" prop="thumbUrl">
                <ai-uploader
                  :instance="instance"
                  v-model="form.thumbUrl"
                  :limit="1"
                  :cropOps="cropOps"
                  is-crop>
                  <template slot="tips">图片比例：1.6：1</template>
                </ai-uploader>
              </el-form-item>
              <el-form-item label="课程简介" style="width: 100%;" prop="content">
                <ai-editor v-model="form.content" :instance="instance"/>
              </el-form-item>
            </el-form>
          </div>
          <!--详情-->
          <div class="village_detail" v-else>
            <img class="cover" :src="form.thumbUrl[0].url" v-if="form.thumbUrl && form.thumbUrl.length">
            <ai-wrapper label-width="100px" :columnsNumber="2">
              <ai-info-item label="课程主题" isLine>{{form.title}}</ai-info-item>
              <ai-info-item label="更新状态">{{dict.getLabel("classroomUpdateStatus",form.updateStatus)}}</ai-info-item>
              <ai-info-item label="更新集数">{{form.episodeNum}}</ai-info-item>
              <ai-info-item label="最后更新时间">{{form.updateDate}}</ai-info-item>
              <ai-info-item label="发布组织">{{form.organizationName}}</ai-info-item>
              <ai-info-item label="课程简介" isLine>
                <div v-html="form.content" style="margin-right: 80px"/>
              </ai-info-item>
            </ai-wrapper>
          </div>
        </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(0)">保存
        </el-button>
        <el-button type="primary" size="small" @click="saveAdd(1)">保存并发布
        </el-button>
      </template>
    </template>
  </ai-detail>
</template>

<script>
  export default {
    name: "partyClassAdd",
    props: {
      instance: Function,
      dict: Object,
      permissions: Function,
      row: Object,
      isEdit: Boolean,
      organizationId: String,
    },
    computed: {
      detailTitle() {
        return this.isEdit ? '编辑党员学习' : '党员学习详情'
      }
    },
    data() {
      return {
        form: {
          organizationId: '',
          title: '',
          type: '',
          content: '',
          thumbUrl: []
        },
        rules: {
          title: [{required: true, message: "请填写标题"}],
          organizationName: [{required: true, message: "请输入发布组织"}],
          updateStatus: [{required: true, message: "请选择更新状态", trigger: "change"}],
          thumbUrl: [{required: true, message: "请上传封面", trigger: "change"}],
          content: [{required: true, message: "请选择正文"}],
        },
        isDetail: true,
        count: 0,
        cropOps: {
          width: "336px",
          height: "210px"
        }
      }
    },
    methods: {
      downLoad(url) {
        window.open(url);
      },
      // 保存
      saveAdd(status) {
        if (this.count != 0) {
          this.$message.error('文件正在上传中');
          return
        }
        this.$refs.ruleForm.validate(v => {
          if (v) {
            this.instance.post('/app/apppartyclassroom/addOrUpdate', {
              ...this.form,
              status,
              style: 1,
              thumbUrl: this.form.thumbUrl.length ? JSON.stringify([{
                url: this.form.thumbUrl[0].url
              }]) : ''
            }).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/apppartyclassroom/queryDetailById', null, {
          params: {id}
        }).then((res) => {
          if (res?.data) {
            this.form = res.data;
            this.form.thumbUrl = res.data.thumbUrl ? JSON.parse(res.data.thumbUrl) : []
          }
        })
      },
    },
    created() {
      this.form.organizationId = this.organizationId;
      this.dict.load('classroomUpdateStatus')
      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;

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

    .el-form {
      width: 760px;
      margin: 18px auto 64px;
      // overflow-x: auto;
      // overflow-y: auto;
      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;
      margin-bottom: 16px;
      width: 760px;
      overflow-y: auto;
      border: 1px solid #eee;
      border-radius: 4px;
      display: flex;
      flex-direction: column;
      background: #fff;

      .village_title {
        padding: 24px;
        border-bottom: 1px solid #eee;

        h3, p {
          text-align: center;
        }
      }

      .village_file {
        display: flex;
        border-bottom: 1px solid #eee;
        padding: 8px 20px;

        span {
          width: 60px;
        }

        div {
          flex: 1;
        }

        .fileSty {
          color: #999;
          font-size: 14px;
          cursor: pointer;
          margin-bottom: 4px;
        }

        .fileSty:hover {
          color: #5088FF;
        }
      }

      .village_cont {
        flex: 1;
        padding: 30px 20px;
      }
    }

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