/*
 * @Author: Anmin
 * @Email:changanmin1@crc.com.hk
 * @Date: 2019-01-15 13:42:01
 * @Last Modified by: Anmin
 * @Last Modified time: 2019-01-28 18:02:38
 * @description:  配置中英繁 字段
 */
<template>
  <Form :label-width="120">
    <FormItem
      prop="zh-CN"
      label="简体中文"
    >
      <Input
        v-model="lang['zh-CN']"
        type="text"
        placeholder
      ></Input>
    </FormItem>
    <FormItem
      prop="zh-TW"
      label="繁體中文"
    >
      <Input
        v-model="lang['zh-TW']"
        type="text"
        placeholder
      ></Input>
    </FormItem>
    <FormItem
      prop="en-US"
      label="English"
    >
      <Input
        v-model="lang['en-US']"
        type="text"
        placeholder
      ></Input>
    </FormItem>
  </Form>
</template>

<script>
import { QueryLangValByField, SaveLangValByField } from '../server.js';
export default {
  name: 'LangConfig',
  props: {
    // 弹框标题
    title: {
      type: String,
      default: '请配置国际化值'
    },
    // 当前默认值
    item: [Boolean, Object],
    // 参数对象 用来处理请求和保存
    config: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      lang: {
        'zh-TW': '',
        'en-US': '',
        'zh-CN': ''
      },
      dataMap: new Map()
    };
  },
  created() {},
  mounted() {
    this.$emit('SetTitle', this.title);
    this.$emit('SetPageActions', [
      {
        text: '确定',
        theme: 'primary',
        icon: 'add',
        handle: this.save
      },
      {
        text: '关闭',
        theme: 'primary',
        icon: 'close',
        handle: this.close
      }
    ]);
    this.initialData();
  },
  methods: {
    /**
     * 初始化三种语言数据
     * 新增 直接取本地
     * 编辑 拉去接口数据
     */
    initialData() {
      if (this.item) {
        this.lang = Object.assign({}, this.item);
      } else {
        const config = this.config;
        // 查询数据
        QueryLangValByField(`${config.module}/${config.code}`).then(data => {
          // 解析数据
          // 解析语言关键字 lang langCode
          data.forEach(item => {
            if (item.lang) { // lang
              this.lang[item.lang] = item[this.config.field];
              this.dataMap.set(item.lang, item);
            } else if (item.langCode) { // langCode
              this.lang[item.langCode] = item[this.config.field];
              this.dataMap.set(item.langCode, item);
            } else if (item.language) {
              this.lang[item.language] = item[this.config.field];
              this.dataMap.set(item.language, item);
            }
          });
        });
      }
    },
    save() {
      // 如果是单独保存
      // 1. 获取接口地址 2.获取传参 3.整理数据 4.发起保存请求
      if (!this.item) {
        const field = this.config.field;
        const arr = [];
        for (const item in this.lang) {
          const data = this.dataMap.get(item) || {};

          data[field] = this.lang[item];
          arr.push(data);
        }
        const param = {};
        param[this.config.module] = arr;
        SaveLangValByField({ url: this.config.module, param }).then(data => {
          this.$Message.success(data.msg || data.message || '修改成功');
          this.$emit('Close');
        });
      } else {
        this.$emit('Close', this.lang);
      }
    },
    close() {
      this.$emit('Close');
    }
  }
};
</script>

<style>
</style>
