<template>
  <div>
    <a-table
        ref="tableRef"
        :customRow="customRow"
        :rowSelection="{
          type: 'radio',
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
          getCheckboxProps: record => ({
              props: {
                disabled: $api.isLook === '1'
              }
          })
        }"
        :loading="loading"
        bordered
        :columns="tableColumns"
        :rowKey="(record, index) => {return index;}"
        size="small"
        :dataSource="tableData"
        :scroll="{ y: 'calc(88vh - 400px)' }"
        :pagination="false">
    </a-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      /** 表头 */
      tableColumns: [
        {
          title: () => this.$t('functional-analysis.top-btn.language'),
          dataIndex: 'lname',
          align: 'center',
        },
      ],
      /** 表格 */
      tableData: [],
      selectedRowKeys: [],
      selectedRow: {},
      /** 表格 */
      loading: false,
    };
  },
  methods: {
    /**
     * 初始化左侧树
     */
    initLeftTree() {
      this.$emit("initLeftTree");
    },
    /**
     * 初始化右侧树
     */
    initRightTree() {
      this.$emit("initRightTree");
    },
    /**
     * 单选框改变回调
     */
    onSelectChange(selectedRowKeys, selectedRows) {
      let row = selectedRows[0];
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRow = row;
      this.update(row.lkey);
    },
    /**
     * 操作行回调
     */
    customRow(record, index) {
      let _that = this;
      if (_that.$api.isLook === '1') return;
      return {
        on: {
          // 鼠标单击行
          click: () => {
            _that.selectedRowKeys = [index];
            _that.selectedRow = record;
            this.update(record.lkey);
          }
        }
      };
    },
    /**
     * 列表
     */
    list() {
      let _that = this;
      _that.loading = true;
      _that.$api.getSelectLanguages({
        fmeaId: _that.$store.getters.rfmeaId
      }).then(res => {
        if (res.flag) {
          _that.tableData = res.data.rdfmcurrentNls.map((item, index) => {
            item.lname = this.$t('global-dict.select-nls.' + item.lkey);
            if (res.data.lkey === item.lkey) {
              _that.selectedRowKeys[0] = index;
            }
            return item;
          });
        } else {
          _that.$message.error(res.message, 1);
        }
      }).finally(() => {
        _that.loading = false;
      });
    },
    /**
     * 修改
     */
    update(lkey) {
      let _that = this;
      if (_that.$api.isLook === '1') return;
      _that.$api.updateSelectLanguages({
        fmeaId: _that.$store.getters.rfmeaId,
        lkey: lkey
      }).then(res => {
        if (res.flag) {
          // 操作成功。
          _that.$message.success(res.message, 1);
          // 初始化左右侧树
          _that.initLeftTree();
          _that.initRightTree();
          // 关闭模态框
          _that.closeModal();
        } else {
          _that.$message.error(res.message, 1);
        }
      });
    },
    /**
     * 关闭对话框
     */
    closeModal() {
      this.$emit("closeModal");
    },
  },
  mounted() {
    /**
     * 列表
     */
    this.list();
  }
};
</script>
<style lang="less" scoped>

</style>
