<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 = "authEdit" type="primary" style="margin-left: 2px;margin-top: 0px">{{buttons.add}}</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="操作">
        <template slot-scope="scope">
          <a type="text" size="small" v-if="!authEdit" @click="editData(scope.row)">{{buttons.update}}</a>
          <a type="text" size="small" v-if="!authDelete" @click="deleteData(scope.row)">{{buttons.delete}}</a>
          <a type="text" size="small" v-if="jumpFlag" @click="getFactory(scope.row)">{{buttons.factoryInformation}}</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=labels.companyInformation :close-on-click-modal="false" v-drag :visible.sync="modelFlag" width="480px" >
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item :label=labels.companyID>
          <el-input v-model="modelData.companyID"  :disabled="modelInputFlag" style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label=labels.companyName>
          <el-input v-model="modelData.companyName"   style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label=labels.taxNo>
          <el-input v-model="modelData.taxNo"  style="width: 130px"></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=labels.bankName>
          <el-input v-model="modelData.bankName"   style="width: 204px"></el-input>
        </el-form-item>
        <el-form-item :label=labels.bankAccount>
          <el-input v-model="modelData.bankAccount" style="width: 200px"></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=labels.cutoffDate>
          <el-select filterable v-model="modelData.cutoffDate" style="width: 130px">
            <el-option label="自然月月底" value="自然月月底"></el-option>
            <el-option label="每月固定日" value="每月固定日"></el-option>
            <el-option label="不固定" value="不固定"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label=labels.cutoffDateFixDate>
          <el-input v-model="modelData.cutoffDateFixDate"  style="width: 130px" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')"></el-input>
        </el-form-item>
        <el-form-item :label=labels.active>
          <el-select filterable v-model="modelData.active" style="width: 130px">
            <el-option label="Y" value="Y"></el-option>
            <el-option label="N" 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="saveData()">{{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" v-drag></column>
  </div>
</template>

<script>
  import {
    saveTableDefaultList,
    getTableDefaultListLanguage,
    getTableUserListLanguage,
    removerDefault,
    removerUser
  } from "@/api/table.js"
  import column from "@/views/modules/common/column";
  import {
    getCompanyInformation,
    saveCompanyInformation,
    deleteCompanyInformation,
  } from "@/api/base/site.js"
  import {

    searchFunctionButtonList,

  } from "@/api/sysLanguage.js"
  var functionId='100001001';
  export default {
    name: "companyInformation",
    components: {
      column,
    },
    data() {
      return {
        visible: false,
        queryTable: {
          functionId: this.$route.meta.menuId,
          tableId: "100001001Table",
          languageCode: this.$i18n.locale
        },
        // 用户table 查询参数
        queryTableUser: {
          userId: this.$store.state.user.name,
          functionId: this.$route.meta.menuId,
          tableId: "100001001Table",
          status: true,
          languageCode: this.$i18n.locale
        },
        // 导出 start
        exportData: [],
        exportName: '会计单位信息'+this.dayjs().format('YYYYMMDDHHmmss'),
        exportHeader: ["会计单位信息"],
        exportFooter: [],
        // 导出 end
        height: 200,
        modelFlag:false,
        modelInputFlag:true,
        selectList:[],
        modelData: {
          add:'',
          companyID:'',
          companyName:'',
          active:'',
          taxNo:'',
          bankName:'',
          bankAccount:'',
          cutoffDate:'',
          cutoffDateFixDate:'',
        },
        authEdit: false,
        authAdd: false,
        authDelete: false,
        jumpFlag:true,
        menuId: this.$route.meta.menuId,
        dataList:[],
        dataListLoading: false,
        columnList: [
          {
            userId: this.$store.state.user.name,
            functionId: 100001001,
            serialNumber: '100001001TableCompanyId',
            tableId: "100001001Table",
            tableName: "会计单位信息表",
            columnProp: "companyID",
            headerAlign: "center",
            align: "left",
            columnLabel: "会计单位编码",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 10,
            status: true,
            fixed: '',
            columnWidth: 120
          },
          {
            userId: this.$store.state.user.name,
            functionId: 100001001,
            serialNumber: '100001001TableCompanyName',
            tableId: "100001001Table",
            tableName: "会计单位信息表",
            columnProp: "companyName",
            headerAlign: "center",
            align: "left",
            columnLabel: "会计单位名称",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 20,
            status: true,
            fixed: '',
            columnWidth: 200
          },
          {
            userId: this.$store.state.user.name,
            functionId: 100001001,
            serialNumber: '100001001TableTaxNo',
            tableId: "100001001Table",
            tableName: "会计单位信息表",
            columnProp: "taxNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "税号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 30,
            status: true,
            fixed: '',
            columnWidth: 200
          },
          {
            userId: this.$store.state.user.name,
            functionId: 100001001,
            serialNumber: '100001001TableBankName',
            tableId: "100001001Table",
            tableName: "会计单位信息表",
            columnProp: "bankName",
            headerAlign: "center",
            align: "left",
            columnLabel: "开户行名称",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 40,
            status: true,
            fixed: '',
            columnWidth: 160
          },
          {
            userId: this.$store.state.user.name,
            functionId: 100001001,
            serialNumber: '100001001TableBankAccount',
            tableId: "100001001Table",
            tableName: "会计单位信息表",
            columnProp: "bankAccount",
            headerAlign: "center",
            align: "left",
            columnLabel: "开户行账号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 50,
            status: true,
            fixed: '',
            columnWidth: 200
          },
          {
            userId: this.$store.state.user.name,
            functionId: 100001001,
            serialNumber: '100001001TableActive',
            tableId: "100001001Table",
            tableName: "会计单位信息表",
            columnProp: "active",
            headerAlign: "center",
            align: "left",
            columnLabel: "在用",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 60,
            status: true,
            fixed: '',
            columnWidth: 40
          },
          {
            userId: this.$store.state.user.name,
            functionId: 100001001,
            serialNumber: '100001001TableCutoffDate',
            tableId: "100001001Table",
            tableName: "会计单位信息表",
            columnProp: "cutoffDate",
            headerAlign: "center",
            align: "left",
            columnLabel: "月结日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 70,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 100001001,
            serialNumber: '100001001TableCutoffDateFixDate',
            tableId: "100001001Table",
            tableName: "会计单位信息表",
            columnProp: "cutoffDateFixDate",
            headerAlign: "center",
            align: "right",
            columnLabel: "每月几号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 80,
            status: true,
            fixed: '',
            columnWidth: 80
          }
        ],
        buttonList:[
          {
          functionId: functionId,
          languageValue: '查询',
          objectId: 'search',
          objectType: 'button',
          tableId: '*',
        }, {
          functionId: functionId,
          languageValue: '新增',
          objectId: 'add',
          objectType: 'button',
          tableId: '*',
        }, {
          functionId: functionId,
          languageValue: '导出',
          objectId: 'download',
          objectType: 'button',
          tableId: '*',
        }, {
          functionId: functionId,
          languageValue: '修改',
          objectId: 'update',
          objectType: 'button',
          tableId: '*',
        }, {
          functionId: functionId,
          languageValue: '工厂信息',
          objectId: 'factoryInformation',
          objectType: 'button',
          tableId: '*',
        }, {
          functionId: functionId,
          languageValue: '保存',
          objectId: 'save',
          objectType: 'button',
          tableId: '*',
        }, {
          functionId: functionId,
          languageValue: '关闭',
          objectId: 'close',
          objectType: 'button',
          tableId: '*',
        }, {
          functionId: functionId,
          languageValue: '删除',
          objectId: 'delete',
          objectType: 'button',
          tableId: '*',
        }, ],
        labelsList:[
          {
            functionId: functionId,
            languageValue: '会计单位编号',
            objectId: 'companyID',
            objectType: 'label',
            tableId: '*',
          }, {
            functionId: functionId,
            languageValue: '会计单位信息',
            objectId: 'companyInformation',
            objectType: 'label',
            tableId: '*',
          }, {
            functionId: functionId,
            languageValue: '会计单位名称',
            objectId: 'companyName',
            objectType: 'label',
            tableId: '*',
          }, {
            functionId: functionId,
            languageValue: '税号',
            objectId: 'taxNo',
            objectType: 'label',
            tableId: '*',
          }, {
            functionId: functionId,
            languageValue: '开户行名称',
            objectId: 'bankName',
            objectType: 'label',
            tableId: '*',
          }, {
            functionId: functionId,
            languageValue: '开户行账号',
            objectId: 'bankAccount',
            objectType: 'label',
            tableId: '*',
          }, {
            functionId: functionId,
            languageValue: '月结日期',
            objectId: 'cutoffDate',
            objectType: 'label',
            tableId: '*',
          }, {
            functionId: functionId,
            languageValue: '在用',
            objectId: 'active',
            objectType: 'label',
            tableId: '*',
          }, {
            functionId: functionId,
            languageValue: '每月几号',
            objectId: 'cutoffDateFixDate',
            objectType: 'label',
            tableId: '*',
          },
        ],
        labels: {
          companyID:'会计单位编号',
          companyInformation:'会计单位信息',
          companyName:'会计单位名称',
          taxNo:'税号',
          bankName:'开户行名称',
          bankAccount:'开户行账号',
          cutoffDate:'月结日期',
          active:'在用',
          cutoffDateFixDate:'每月几号',
        },
        buttons:{
          search:'查询',
          add:'新增',
          download:'导出',
          update:'修改',
          factoryInformation:'工厂信息',
          save:'保存',
          close:'关闭',
          delete:'删除',
        },
        queryButton: {
          functionId: functionId,
          table_id: '*',
          languageCode: this.$i18n.locale,
          objectType: 'button'
        },
        queryLabel: {
          functionId: functionId,
          table_id: '*',
          languageCode: this.$i18n.locale,
          objectType: 'label'
        },
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.height = window.innerHeight - 140;
      })
    },
    watch: {
      '$route' (to, from) {
        if(localStorage.getItem('companyInformation')!=undefined){
          this.getData()
        }
      }
    },
    methods: {
      getData(){
        let data={};
        if(localStorage.getItem('companyInformation')!=undefined){
          data=JSON.parse(localStorage.getItem('companyInformation'));
        }
        localStorage.removeItem('companyInformation');
        getCompanyInformation(data).then(({data}) => {
          this.dataList = data.rows
        })
      },
      addModal(){
        this.modelData={};
        this.modelData.add=0;
        this.modelInputFlag=false;
        this.modelData.active='Y';
        this.modelFlag=true;
      },
      editData(row){
        this.modelData=JSON.parse(JSON.stringify(row));
        this.modelData.add=1;
        this.modelInputFlag=true;
        this.modelFlag=true;
      },
      deleteData(row){
        this.$confirm(`是否删除此条会计单位记录?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.modelData=JSON.parse(JSON.stringify(row));
          deleteCompanyInformation(this.modelData).then(({data}) => {
            if (data && data.code == 200) {
              this.modelFlag = false
              this.getData();
             this.$message.success('操作成功')
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        })
      },
      saveData(){
          if(this.modelData.companyID==''||this.modelData.companyID==null){
            this.$alert("请输入会计单位编号！",'错误',{
              confirmButtonText:'确定'
            })
            return false;
          }
          if(this.modelData.companyName==''||this.modelData.companyName==null){
            this.$alert("请输入会计单位名称！",'错误',{
              confirmButtonText:'确定'
            })
            return false;
          }
          if(this.modelData.active==''||this.modelData.active==null){
            this.$alert("请选择是否在用！",'错误',{
              confirmButtonText:'确定'
            })
            return false;
          }
          if(this.modelData.cutoffDate==''||this.modelData.cutoffDate==null){
            this.$alert("请选择月结日期！",'错误',{
              confirmButtonText:'确定'
            })
            return false;
          }
          if(this.modelData.cutoffDateFixDate==''||this.modelData.cutoffDateFixDate==null){
            this.modelData.cutoffDateFixDate=0;
          }
          if(this.modelData.cutoffDateFixDate>31){
            this.$alert("请输入正确的每月几号！",'错误',{
              confirmButtonText:'确定'
            })
            return false;
          }
        saveCompanyInformation(this.modelData).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
      getFactory(row){
        let inData={companyID:row.companyID,companyName:row.companyName};
        localStorage.setItem('factoryInformation', JSON.stringify(inData))
        this.$router.replace('base-factoryInformation')
      },
      //获取按钮的权限数据
      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;
        //跳转页面
        let menList = JSON.parse(sessionStorage.getItem('dynamicMenuRoutes') || '[]').filter(item => item.path == 'base-factoryInformation')
        if (menList.length > 0) {
          this.jumpFlag = false
        }
      },
      //多语言
      getMultiLanguageList() {
        //首先查询当前按钮的多语言
        searchFunctionButtonList(this.queryButton).then(({data}) => {
          if (data.code == 0) {
            this.buttons = data.data
          } else {
            // saveButtonList(this.buttonList).then(({data}) => {
            // })
          }
        });
        //其次查询当前标签的多语言
        searchFunctionButtonList(this.queryLabel).then(({data}) => {
          if (data.code == 0) {
            this.labels = data.data
          } else {
            // saveButtonList(this.buttonList).then(({data}) => {
            // })
          }
        });
      },
      // 打开页面设置
      userSetting() {
        this.visible = true;
        let queryTable = {
          userId: this.$store.state.user.name,
          functionId: this.$route.meta.menuId,
          tableId: "100001001Table",
          languageCode: this.$i18n.locale
        }
        this.$nextTick(() => {
          this.$refs.column.init(queryTable);
        });
      },
      // 获取 用户保存的 格式列
      getTableUserColumn(tableId) {
        console.log(tableId)
        getTableUserListLanguage(this.queryTableUser).then(({data}) => {
          if (data.rows.length > 0) {
            //this.columnList = []
            this.columnList = data.rows
          } else {
            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
          }
        })
      },
    },
    created() {
      this.getMultiLanguageList()//刷新按钮
      this.getData();
      //获取按钮的权限
      this.getButtonAuthData();

      this.getTableUserColumn()
    }
  }
</script>

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