<template>
  <el-dialog
    :visible="visible"
    width="610px"
    :close-on-click-modal="false"
    :append-to-body="true"
    title="增加语言"
    @close="dialogLangClose"
  >
    <el-transfer
      v-model="langs"
      :titles="['可选', '已选择']"
      :props="{ key: 'code', label: 'name' }"
      :data="langList"
    />

    <div slot="footer">
      <el-button @click="dialogLangClose">取消</el-button>
      <el-button type="primary" @click="updateLangs">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { langList } from '@/views/book/courseware/data/common';

export default {
  name: 'UpdateLang',
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    selectedLangs: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      langList: langList.map((item) => ({ ...item, disabled: false })),
      langs: [],
    };
  },
  watch: {
    selectedLangs: {
      handler(val) {
        if (!val || !Array.isArray(val)) return;

        this.langs = val.map((item) => item.code);
      },
      immediate: true,
    },
  },
  methods: {
    dialogLangClose() {
      this.$emit('update:visible', false);
    },
    updateLangs() {
      this.$emit('update-langs', this.langs);
      this.dialogLangClose();
    },
  },
};
</script>

<style lang="scss" scoped></style>
