<template>
  <el-dialog :visible="visible" title="上架" width="450px" @close="dialogClose">
    <div class="listing">
      <div class="authorized-agency">
        <div class="authorized-agency-title">选择授权机构（可多选）</div>
        <el-select
          v-model="org_id_list"
          multiple
          placeholder="选择机构"
          :disabled="publish_scope === publishList[0].value"
        >
          <el-option
            v-for="{ id: org_id, name: org_name } in org_list"
            :key="org_id"
            :label="org_name"
            :value="org_id"
          />
        </el-select>
      </div>
    </div>

    <div slot="footer" class="footer">
      <el-button round @click="dialogClose">取消</el-button>
      <el-button type="primary" round @click="listing">上架</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { SetPublishStatusForBook, PageQueryOrgIndexList_OpenQuery } from '@/api/book';

export default {
  name: 'ListingDialog',
  props: {
    id: {
      type: String,
      required: true,
    },
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      publish_scope: 0, // 可见范围
      publishList: [
        { label: '全部机构可见', value: 1 },
        { label: '仅授权机构可见', value: 0 },
      ],
      cur_page: 1,
      org_list: [], // 机构列表
      org_id_list: [], // 授权机构
    };
  },
  created() {
    this.pageQueryOrgIndexList_OpenQuery();
  },
  methods: {
    dialogClose() {
      this.$emit('update:visible', false);
    },
    // 上架
    listing() {
      SetPublishStatusForBook({
        book_id: this.id,
        publish_status: 1,
        publish_scope: this.publish_scope,
        org_id_list: this.org_id_list,
      }).then(() => {
        this.dialogClose();
        this.$emit('PublishSuccess');
      });
    },
    pageQueryOrgIndexList_OpenQuery() {
      PageQueryOrgIndexList_OpenQuery({ name: '', page_capacity: 50, cur_page: this.cur_page }).then(
        ({ org_list, total_count, cur_page_end_index }) => {
          this.org_list.push(...org_list);
          if (cur_page_end_index < total_count) {
            this.cur_page += 1;
            this.pageQueryOrgIndexList_OpenQuery();
          }
        },
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.el-dialog__wrapper {
  :deep .el-dialog {
    .listing {
      padding: 0 16px;

      .authorized-agency {
        display: flex;
        flex-direction: column;
        row-gap: 8px;
        margin-top: 20px;
      }
    }

    &__body {
      padding: 0 20px 16px;
    }

    &__header {
      display: flex;
      align-items: center;
      padding: 16px 20px;
      border-bottom: 1px solid #ebebeb;
    }

    &__title {
      font-size: 14px;
      font-weight: bold;
      color: $font-color;
    }

    &__headerbtn .el-dialog__close {
      color: $font-color;
    }

    .footer {
      display: flex;
      padding: 0 16px;

      .el-button {
        flex: 1;
      }
    }
  }
}
</style>
