<template>
  <div class="mod-config">
    <el-form :inline="true" label-position="top" label-width="100px" style="margin-top: 0px;">
      <el-button @click="getData()" type="primary" style="margin-left: 2px;margin-top: 0px">{{buttons.search}}</el-button>
      <el-button @click="addModal()" :disabled = "authAdd" type="primary" style="margin-left: 2px;margin-top: 0px">{{buttons.add}}</el-button>
      <el-button @click="userSetting" type="primary">{{ buttons.settingTable }}</el-button>
      <el-button v-if="isAuth('sys:setting')" @click="saveColumnList()" type="primary">{{ buttons.defaultTable }}</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>
    <el-table
      :height="height"
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        :label=buttons.work>
        <template slot-scope="scope">
          <a type="text" size="small" v-if="!authEdit" @click="editModel(scope.row)">{{buttons.update}}</a>
          <a type="text" size="small" v-if="!authDelete" @click="deleteData(scope.row)">{{buttons.delete}}</a>
        </template>
      </el-table-column>
      <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"
        :min-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>
    <el-dialog :title="buttons.umidModal" :close-on-click-modal="false" v-drag :visible.sync="modelFlag" width="350px" >
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item :label=buttons.site>
          <el-input v-model="modelData.site" :disabled="modelInputFlag" style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label=buttons.umid>
          <el-input v-model="modelData.umid" :disabled="modelInputFlag" style="width: 130px" onblur="this.value=this.value.toUpperCase()"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item :label=buttons.uMName>
          <el-input v-model="modelData.uMName"   style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label=buttons.active>
          <el-select filterable v-model="modelData.active" style="width: 130px">
            <el-option :label=buttons.yes value="Y"></el-option>
            <el-option :label=buttons.no value="N"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-footer  style="height:40px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="ModelSave()">{{buttons.save}}</el-button>
        <el-button type="primary" @click="modelFlag = false">{{buttons.close}}</el-button>
      </el-footer>
    </el-dialog>

    <!-- 动态列    -->
    <column v-if="visible" ref="column" @refreshData="getTableUserColumn"></column>
  </div>
</template>

<script>
  import column from "../common/column";
  import {
    searchSysLanguagePackList,
    searchSysLanguageParam,
    searchFunctionButtonList,
    saveButtonList,
    searchSysLanguage,
    searchLanguageListByLanguageCode,
    saveSysLanguageOne,
    searchPageLanguageData,
    removerLanguage
  } from "@/api/sysLanguage.js"
  import {
    saveTableDefaultList,
    saveTableUser,
    getTableDefaultListLanguage,
    getTableUserListLanguage,
    removerDefault,
    removerUser
  } from "@/api/table.js"
  import {
    getUMData,
    saveUMData,
    deleteUMData
  } from "@/api/base/site.js"

  export default {
    name: "unitMeasure",
    components: {column},
    data() {
      return {
        // 多语言 start
        buttonList: [
          {
            functionId: this.$route.meta.menuId,
            languageValue: '计量单位',
            objectId: 'umidModal',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '设置默认配置',
            objectId: 'defaultTable',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '查询',
            objectId: 'search',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '新增',
            objectId: 'add',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '导出',
            objectId: 'download',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '设置列表',
            objectId: 'settingTable',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '操作',
            objectId: 'work',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '数据维护',
            objectId: 'dataUpdate',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '工厂编码',
            objectId: 'site',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '编辑',
            objectId: 'update',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '删除',
            objectId: 'delete',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '保存',
            objectId: 'save',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '关闭',
            objectId: 'close',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '计量单位',
            objectId: 'umid',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '计量单位名称',
            objectId: 'uMName',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '是',
            objectId: 'yes',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '否',
            objectId: 'no',
            objectType: "button",
            tableId: "6003Table"
          },
          {
            functionId: this.$route.meta.menuId,
            languageValue: '是否在用',
            objectId: 'active',
            objectType: "button",
            tableId: "6003Table"
          }
        ],
        showDefault:false,
        // 语言词典集合
        sysLanguageParams: [],
        dataListLoading: false,
        // 用户table 配置集合
        userColumnList: [],
        queryTableUser: {
          userId: this.$store.state.user.name,
          functionId: this.$route.meta.menuId,
          tableId: "6003Table",
          status: true,
          languageCode: this.$i18n.locale
        },
        buttons:{
          umidModal:'计量单位',
          defaultTable:'设置默认配置',
          search:'查询',
          add:'新增',
          download:'导出',
          settingTable: '设置列表',
          work:'操作',
          dataUpdate:'数据维护',
          site:'工厂编码',
          update:'编辑',
          delete:'删除',
          save:'保存',
          close:'关闭',
          umid:'计量单位',
          uMName:'计量单位名称',
          active:'是否在用',
          yes:'是',
          no:'否',
        },
        // 默认table 查询参数
        queryTable: {
          functionId: this.$route.meta.menuId,
          tableId: "6003Table",
          languageCode: this.$i18n.locale
        },
        languageList: [],
        queryLanguage: {},
        visible:false,
        // 多语言 end
        // 导出 start
        exportData: [],
        exportName: "计量单位"+this.dayjs().format('YYYYMMDDHHmmss'),
        exportHeader: ["计量单位"],
        exportFooter: [],
        // 导出 end
        height: 200,
        modelFlag:false,
        modelInputFlag:true,
        selectList:[],
        modelData: {
          add:'',
          site:'',
          umid:'',
          uMName:'',
          active:'',
          user:'',
        },
        dataList:[],
        columnList: [
          {
            userId: this.$store.state.user.name,
            functionId: this.$route.meta.menuId,
            serialNumber: '6003TableUmid',
            tableId: "6003Table",
            tableName: "计量单位表",
            columnProp: "umid",
            headerAlign: "center",
            align: "left",
            columnLabel: "计量单位",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: this.$route.meta.menuId,
            serialNumber: '6003TableUMName',
            tableId: "6003Table",
            tableName: "计量单位表",
            columnProp: "uMName",
            headerAlign: "center",
            align: "left",
            columnLabel: "名称",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 1,
            status: true,
            fixed: '',
            columnWidth: 200
          },
          {
            userId: this.$store.state.user.name,
            functionId: this.$route.meta.menuId,
            serialNumber: '6003TableActive',
            tableId: "6003Table",
            tableName: "计量单位表",
            columnProp: "active",
            headerAlign: "center",
            align: "left",
            columnLabel: "在用",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 2,
            status: true,
            fixed: '',
            columnWidth: 60
          },
          {
            userId: this.$store.state.user.name,
            functionId: this.$route.meta.menuId,
            serialNumber: '6003TableSite',
            tableId: "6003Table",
            tableName: "物料分类表",
            columnProp: "site",
            headerAlign: "center",
            align: "left",
            columnLabel: "工厂编码",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 3,
            status: true,
            fixed: '',
            columnWidth: 80
          }
        ],
        authEdit: false,
        authAdd: false,
        authDelete: false,
        menuId: this.$route.meta.menuId,
      }
    },
    watch: {
      modelData: {
        deep: true,
        handler: function (newV, oldV) {
          this.modelData.umid = this.modelData.umid.toUpperCase()
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.height = window.innerHeight - 140;
      })
    },
    methods: {
      //多语言start
      // 获取页面多语言
      getMsgLanguage() {
        this.dataListLoading = true
        let queryLang = {
          functionId: this.$route.meta.menuId,
          table_id: '6003Table',
          languageCode: this.$i18n.locale
        }
        searchPageLanguageData(queryLang).then((data) => {
          if (data.code == 0) {
            this.pageLanguageData = data.data
          } else {
          }
        })
        this.dataListLoading = false
      },
      // 获取button的词典
      getFunctionButtonList() {
        searchFunctionButtonList(this.queryTable).then(({data}) => {
          if (data.code == 0) {
            this.buttons = data.data
          } else {
            // saveButtonList(this.buttonList).then(({data}) => {
            // })
          }
        })
      },
      // 获取语言词典
      getSysLanguageParamList() {
        searchSysLanguageParam(this.queryTable).then(({data}) => {
          this.sysLanguageParams = data.rows
        })
      },
      // 打开页面设置
      userSetting() {
        this.visible = true;
        let queryTable = {
          userId: this.$route.meta.menuId,
          functionId: this.$route.meta.menuId,
          tableId: "6003Table",
          languageCode: this.$i18n.locale
        }
        this.$nextTick(() => {
          this.$refs.column.init(queryTable);
        });
      },
      // 获取 用户保存的 格式列
      getTableUserColumn() {
        getTableUserListLanguage(this.queryTableUser).then(({data}) => {
          if (data.rows.length>0) {
            //this.columnList = []
            this.columnList = data.rows
          } else {
            this.getColumnList()
          }

        })
      },
      // 保存 默认配置 列
      saveColumnList() {
        // 删除所有的该页面下保存的数据
        if (this.userColumnList) {
          //删除 user自定义的数据
          removerUser(this.queryTable)
        }
        // 删除默认配置
        removerDefault(this.queryTable)
        // 删除语言
        removerLanguage(this.queryTable)
        // 保存页面 table属性
        let sumColumnList = this.columnList;
        saveTableDefaultList(sumColumnList).then(({data}) => {
        })
        // 保存页面 button label title 属性
        saveButtonList(this.buttonList).then(({data}) => {
        })
        this.getFunctionButtonList()
        this.getColumnList()
      },
      // 获取 tableDefault 列
      getColumnList() {
        getTableDefaultListLanguage(this.queryTable).then(({data}) => {
          if (data.rows.length>0) {
            // this.showDefault = false
            this.columnList = data.rows
          } else {
           // this.showDefault = true
          }
        })
      },
      //多语言 end
      getData(){
        let inData={
          user:this.$store.state.user.name
        }
        getUMData(inData).then(({data}) => {
          this.dataList = data.rows
        })
      },
      addModal(){
        this.modelData={
          add:'',
          site:this.$store.state.user.site,
          umid:'',
          uMName:'',
          active:'',
          user:'',
        };
        this.modelData.add=0;
        this.modelInputFlag=false;
        this.modelData.active='Y';
        this.modelFlag=true;
      },
      editModel(row){
        this.modelData=JSON.parse(JSON.stringify(row));
        this.modelData.add=1;
        this.modelInputFlag=true;
        this.modelFlag=true;
      },
      ModelSave(){
        if(this.modelData.umid==''||this.modelData.umid==null){
          this.$alert("请输入计量单位！",'错误',{
            confirmButtonText:'确定'
          })
          return false;
        }
        if(this.modelData.uMName==''||this.modelData.uMName==null){
          this.$alert("请输入名称！",'错误',{
            confirmButtonText:'确定'
          })
          return false;
        }
        if(this.modelData.active==''||this.modelData.active==null){
          this.$alert("请选择是否在用！",'错误',{
            confirmButtonText:'确定'
          })
          return false;
        }
        if(this.modelData.site==''||this.modelData.site==null){
          this.$alert("请输入工厂编码！",'错误',{
            confirmButtonText:'确定'
          })
          return false;
        }
        this.modelData.user=this.$store.state.user.name;

        saveUMData(this.modelData).then(({data}) => {
          if (data && data.code == 200) {
            this.modelFlag = false
            this.getData();
            this.$message.success( '操作成功')
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },
      deleteData(row){
        this.$confirm(`是否删除此条计量单位?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'

        }).then(() => {
          let inData = {
            umid: row.umid,
            site:row.site
          }
          deleteUMData(inData).then(({data}) => {
            if (data && data.code == 200) {
              this.modelFlag = false
              this.getData();
             this.$message.success('操作成功')
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        })
      },
      //导出excel
      createExportData() {
        return this.dataList;
      },
      startDownload() {
        //  this.exportData = this.dataList

      },
      finishDownload() {

      },
      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
      },
      // 导出 end
      //获取按钮的权限数据
      getButtonAuthData(){
        let updateFlag = this.isAuth(this.menuId+":revise");
        let fullControFlag = this.isAuth(this.menuId+":fullContro");
        let deleteFlag = this.isAuth(this.menuId+":remove");
        //处理页面的权限数据
        this.authEdit = !updateFlag ||!fullControFlag;
        this.authAdd = !fullControFlag;
        this.authDelete = !deleteFlag;
      },
    },
    created() {
      this.getTableUserColumn()
      this.getSysLanguageParamList()
      this.getFunctionButtonList()
      this.getMsgLanguage()
      this.getData();
      //获取按钮的权限
      this.getButtonAuthData();
    }
  }
</script>

<style >
  .el-textarea__inner {
    padding: 5px 5px;
  }
</style>
