<template>
  <div class="hupdateFlaglo">
    <el-row>
      <el-col :span="24">
        <div>
        <span @click="favoriteFunction()">
         <icon-svg :name="favorite?'xiangqufill':'xiangqu'" class="sl-svg"></icon-svg>
        </span>
        </div>
      </el-col>
      <el-col :span="24">
        <el-form :inline="true" :model="querySysLanguagePack" @keyup.enter.native="getDataList()">
          <el-form-item>
            <el-input filterable v-model="querySysLanguagePack.key" clearable>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()" type="primary">{{ buttons.search }}</el-button>
            <download-excel
              :fields="fields()"
              :data="exportData"
              type="xls"
              :name="exportName"
              :header="exportHeader"
              :footer="exportFooter"
              :fetch="createExportData"
              :before-generate="startDownload"
              :before-finish="finishDownload"
              worksheet="导出信息"
              class="el-button el-button--primary el-button--medium">
              {{ buttons.download }}
            </download-excel>
          </el-form-item>

        </el-form>
      </el-col>
      <el-col :span="24">
        <el-table
          id="commmon"
          :height="height"
          :data="dataList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          style="width: 100%;">
          <el-table-column
            v-for="(item,index) in columnList" :key="index"
            :sortable="item.columnSortable"
            :prop="item.columnProp"
            :header-align="item.headerAlign"
            :show-overflow-tooltip="item.showOverflowTooltip"
            :align="item.align"
            :fixed="item.fixed==''?false:item.fixed"
            :width="item.columnWidth"
            :label="item.columnLabel">
            <template slot-scope="scope">
              <span v-if="!item.columnHidden"> {{ scope.row[item.columnProp] }}</span>
              <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]"
                                                 style="width: 100px; height: 80px"/></span>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            header-align="center"
            align="center"
            width="150"
            :label="buttons.cz">
            <template slot-scope="scope">
              <a type="text" size="small"
                 @click="addOrUpdateLanguageList(scope.row.functionId)">{{ buttons.settingLanguageList }}
              </a>
              |
              <a type="text" size="small" @click="addOrUpdateLanguage(scope.row)">{{ buttons.settingLanguage }}</a>
            </template>
          </el-table-column>

        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[20, 50, 200, 500]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-col>
    </el-row>
    <!-- 动态列    -->
    <column v-if="visible" ref="column" @refreshData="getTableUserColumn" v-drag></column>
    <!--  语言组件   -->
    <commonLanguageBase ref="commonLanguageAdd" v-drag></commonLanguageBase>
    <commonLanguageList ref="commonLanguageList" v-drag></commonLanguageList>
  </div>
</template>

<script>
import column from "../../common/column";
import contextButton from '../../common/right_click_menu'
import commonLanguageBase from "./common-language-base";
import commonLanguageList from "./common-language-list";
import {
  searchSysLanguagePackList,
  searchFunctionButtonList,
} from "@/api/sysLanguage.js"

import {
  saveTableDefaultList,
  getTableDefaultListLanguage,
  getTableUserListLanguage,
  removerDefault,
  removerUser
} from "@/api/table.js"
import {userFavoriteList, saveUserFavorite, removeUserFavorite} from '@/api/userFavorite.js'

export default {
  components: {
    column,
    commonLanguageBase,
    commonLanguageList,
    contextButton
  },
  data() {
    return {
      menuVisible: false,
      listShow: false,
      // table高度
      height: 450,
      // 是否收藏
      favorite: false,
      addLanguage: false,
      functionId: this.$route.meta.menuId,
      tableId: "common1001",
      value1: true,
      visible: false,
      showDefault: false,
      // 语言词典集合
      sysLanguageParams: [],
      // 用户table 配置集合
      userColumnList: [],
      // 展示列集
      columnList: [
        {
          "tableId": "common1001",
          "tableName": "common",
          "columnProp": "functionId",
          "columnLabel": "功能编码",
          "columnHidden": false,
          "columnImage": false,
          "columnSortable": false,
          "columnWidth": null,
          "format": null,
          "functionId": this.$route.meta.menuId,
          "sortLv": 0,
          "status": true,
          "fixed": '',
          "serialNumber": null,
          "columnType": null,
          "align": null
        },
        {
          "tableId": "common1001",
          "tableName": "common",
          "columnProp": "funDesc",
          "columnLabel": "功能名称",
          "columnHidden": false,
          "columnImage": false,
          "columnSortable": false,
          "columnWidth": null,
          "format": null,
          "functionId": this.$route.meta.menuId,
          "sortLv": 0,
          "status": true,
          "fixed": '',
          "serialNumber": null,
          "columnType": null,
          "align": null
        },
        {
          "tableId": "common1001",
          "tableName": "common",
          "columnProp": "objectId",
          "columnLabel": "控件编码",
          "columnHidden": false,
          "columnImage": false,
          "columnSortable": false,
          "columnWidth": null,
          "format": null,
          "functionId": this.$route.meta.menuId,
          "sortLv": 0,
          "status": true,
          "fixed": '',
          "serialNumber": null,
          "columnType": null,
          "align": null
        },
        {
          "tableId": "common1001",
          "tableName": "common",
          "columnProp": "objectType",
          "columnLabel": "类型",
          "columnHidden": false,
          "columnImage": false,
          "columnSortable": false,
          "columnWidth": null,
          "format": null,
          "functionId": this.$route.meta.menuId,
          "sortLv": 0,
          "status": true,
          "fixed": '',
          "serialNumber": null,
          "columnType": null,
          "align": null
        },
        {
          "tableId": "common1001",
          "tableName": "common",
          "columnProp": "languageValue",
          "columnLabel": "控件名称",
          "columnHidden": false,
          "columnImage": false,
          "columnSortable": false,
          "columnWidth": null,
          "format": null,
          "functionId": this.$route.meta.menuId,
          "sortLv": 0,
          "status": true,
          "fixed": '',
          "serialNumber": null,
          "columnType": null,
          "align": null
        }
      ],
      // 展示列集
      columnList1: [
        {
          "tableId": "common1002",
          "tableName": "commonLanguage",
          "columnProp": "functionId",
          "columnLabel": "功能编码",
          "columnHidden": false,
          "columnImage": false,
          "columnSortable": false,
          "columnWidth": null,
          "format": null,
          "functionId": this.$route.meta.menuId,
          "sortLv": 0,
          "status": true,
          "fixed": '',
          "serialNumber": null,
          "columnType": null,
          "align": null
        },
        {
          "tableId": "common1002",
          "tableName": "commonLanguage",
          "columnProp": "funDesc",
          "columnLabel": "功能名称",
          "columnHidden": false,
          "columnImage": false,
          "columnSortable": false,
          "columnWidth": null,
          "format": null,
          "functionId": this.$route.meta.menuId,
          "sortLv": 0,
          "status": true,
          "fixed": '',
          "serialNumber": null,
          "columnType": null,
          "align": null
        },
        {
          "tableId": "common1002",
          "tableName": "commonLanguage",
          "columnProp": "languageCode",
          "columnLabel": "控件语言编码",
          "columnHidden": false,
          "columnImage": false,
          "columnSortable": false,
          "columnWidth": null,
          "format": null,
          "functionId": this.$route.meta.menuId,
          "sortLv": 0,
          "status": true,
          "fixed": '',
          "serialNumber": null,
          "columnType": null,
          "align": null
        },
        {
          "tableId": "common1002",
          "tableName": "commonLanguage",
          "columnProp": "objectId",
          "columnLabel": "控件编码",
          "columnHidden": false,
          "columnImage": false,
          "columnSortable": false,
          "columnWidth": null,
          "format": null,
          "functionId": this.$route.meta.menuId,
          "sortLv": 1,
          "status": true,
          "fixed": '',
          "serialNumber": null,
          "columnType": null,
          "align": null
        },
        {
          "tableId": "common1002",
          "tableName": "commonLanguage",
          "columnProp": "objectType",
          "columnLabel": "控件类型",
          "columnHidden": false,
          "columnImage": false,
          "columnSortable": false,
          "columnWidth": null,
          "format": null,
          "functionId": this.$route.meta.menuId,
          "sortLv": 1,
          "status": true,
          "fixed": '',
          "serialNumber": null,
          "columnType": null,
          "align": null
        },
        {
          "tableId": "common1002",
          "tableName": "commonLanguage",
          "columnProp": "baseName",
          "columnLabel": "控件名称",
          "columnHidden": false,
          "columnImage": false,
          "columnSortable": false,
          "columnWidth": null,
          "format": null,
          "functionId": "9001",
          "sortLv": 2,
          "status": true,
          "fixed": '',
          "serialNumber": null,
          "columnType": null,
          "align": null
        },
        {
          "tableId": "common1002",
          "tableName": "commonLanguage",
          "columnProp": "languageValue",
          "columnLabel": "控件值",
          "columnHidden": false,
          "columnImage": false,
          "columnSortable": false,
          "columnWidth": null,
          "format": null,
          "functionId": this.$route.meta.menuId,
          "sortLv": 2,
          "status": true,
          "fixed": '',
          "serialNumber": null,
          "columnType": null,
          "align": null
        }
      ],
      // 数据集
      dataList: [],
      queryButton: {
        functionId: this.$route.meta.menuId,
        table_id: 'common1001',
        languageCode: this.$i18n.locale,
        objectType: 'button'
      },
      buttons: {
        settingLanguageList: '按语言设置',
        edit: '编辑',
        delete: '删除',
        deleteList: '批量删除',
        cz: '操作',
        search: '查询',
        download: '导出',
        settingTable: '设置列表',
        defaultTable: '设置默认配置',
        list: "列表",
        addList: '批量添加',
        settingLanguage: "按控件设置"
      },
      // 导出 start
      exportData: [],
      exportName: "页面功能语言",
      exportHeader: ["页面功能语言"],
      exportFooter: [],
      // 导出 end
      languageList: [],
      languageColumnList: [],
      languageDataList: [],
      queryLanguage: {},
      // 数据集条件
      querySysLanguagePack: {
        key: '',
        page: 1,
        limit: 1,
        languageValue: '',
        objectType: '',
        objectId: ''
      },
      addQuery: {
        functionId: '',
        languageCode: ''
      },
      // 批量选中对象
      // 分页
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.height = window.innerHeight - 180;
    })
  },
  activated() {
    this.getDataList()

  },
  methods: {

    // 校验用户是否收藏
    favoriteIsOk() {
      let userFavorite = {
        userId: this.$store.state.user.id,
        languageCode: this.$i18n.locale
      }
      userFavoriteList(userFavorite).then(({data}) => {
        console.log(data.list)
        let size = data.list.filter(item => item.menuId == this.$route.meta.menuId).length;
        console.log(size)
        if (size > 0) {
          this.favorite = true
        } else {
          this.favorite = false
        }
      })
    },
    // 收藏 OR 取消收藏
    favoriteFunction() {
      let userFavorite = {
        userId: this.$store.state.user.id,
        functionId: this.$route.meta.menuId,
      }
      if (this.favorite) {
        // 取消收藏
        this.$confirm(`确定取消收藏`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          removeUserFavorite(userFavorite).then(({data}) => {
            this.$message.success(data.msg)
            this.favorite = false
          })
        })
      } else {
        // 收藏
        saveUserFavorite(userFavorite).then(({data}) => {
          this.$message.success(data.msg)
          this.favorite = true
        })
      }

    },
    // 单个语言编辑
    addOrUpdateLanguage(val) {
      this.commonLanguageVisible = true
      this.$nextTick(() => {
        this.$refs.commonLanguageAdd.init(val)
      })
    },
    // 语言列表编辑
    addOrUpdateLanguageList(functionId) {
      this.$nextTick(() => {
        this.$refs.commonLanguageList.init(functionId, this.$route.meta.menuId)
      })
    },
    // 获取button的词典
    getFunctionButtonList() {
      searchFunctionButtonList(this.queryButton).then(({data}) => {
        if (data.code == 0) {
          this.buttons = data.data
        }
      })
    },
    // 获取 用户保存的 格式列
    getTableUserColumn(tableId) {
      let queryTableUser = {
        userId: this.$store.state.user.name,
        functionId: this.$route.meta.menuId,
        tableId: "common1001",
        status: true,
        languageCode: this.$i18n.locale
      }
      getTableUserListLanguage(queryTableUser).then(({data}) => {
        if (data.rows.length > 0) {
          this.columnList = data.rows
        } else {
          this.getColumnList()
        }
      })
    },
    // 获取 tableDefault 列
    getColumnList() {
      let queryTable = {
        functionId: this.$route.meta.menuId,
        tableId: "common1001",
        languageCode: this.$i18n.locale
      }
      getTableDefaultListLanguage(queryTable).then(({data}) => {
        if (data.code == 0 && !data.rows) {
          this.columnList = data.rows
        }
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.querySysLanguagePack.limit = this.pageSize
      this.querySysLanguagePack.page = this.pageIndex
      searchSysLanguagePackList(this.querySysLanguagePack).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 导出
    fields() {
      let json = "{"
      this.columnList.forEach((item, index) => {
        if (index == this.columnList.length - 1) {
          json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\""
        } else {
          json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\"" + ","
        }
      })
      json += "}"
      let s = eval("(" + json + ")")
      return s
    },
    createExportData() {
      // 点击导出按钮之后,开始导出数据之前的执行函数,返回值为需要下载的数据
      return this.dataList;
    },
    startDownload() {
    },
    finishDownload() {
    }
  },
  created() {
    this.getTableUserColumn()
    this.getFunctionButtonList()
    this.favoriteIsOk()
  }
}
</script>
<style scoped>
.wrapper {
  height: calc(100% - 40px);
}

.sl-svg {
  overflow: hidden;
  float: right;
}

</style>
