<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">{{'查询'}}</el-button>
      <el-button @click="addModal()" type="primary" style="margin-left: 2px;margin-top: 0px">{{'新增'}}</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" @click="editData(scope.row)">修改</a>
          <a type="text" size="small" @click="deleteData(scope.row)">删除</a>
          <a type="text" size="small" @click="getFactory(scope.row)">工厂信息</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"
        :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="数据维护" :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="'会计单位编号：'">
          <el-input v-model="modelData.companyID"  :disabled="modelInputFlag" style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label="'会计单位名称：'">
          <el-input v-model="modelData.companyName"   style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label="'税号：'">
          <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="'开户行名称：'">
          <el-input v-model="modelData.bankName"   style="width: 204px"></el-input>
        </el-form-item>
        <el-form-item :label="'开户行账号：'">
          <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="'月结日期：'">
          <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="'每月几号：'">
          <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="'在用：'">
          <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()">保存</el-button>
        <el-button type="primary" @click="modelFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>
  </div>
</template>

<script>

  import {
    getCompanyInformation,
    saveCompanyInformation,
    deleteCompanyInformation,
  } from "@/api/base/site.js"

  export default {
    name: "companyInformation",
    data() {
      return {
        // 导出 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:'',
        },
        dataList:[],
        dataListLoading: false,
        buttons:{
          download:'导出',
        },
        columnList: [
          {
            userId: this.$store.state.user.name,
            functionId: 6037,
            serialNumber: '6037TableCompanyId',
            tableId: "6037Table",
            tableName: "会计单位信息表",
            columnProp: "companyID",
            headerAlign: "center",
            align: "left",
            columnLabel: "会计单位编码",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 120
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6037,
            serialNumber: '6037TableCompanyName',
            tableId: "6037Table",
            tableName: "会计单位信息表",
            columnProp: "companyName",
            headerAlign: "center",
            align: "left",
            columnLabel: "会计单位名称",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 200
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6037,
            serialNumber: '6037TableTaxNo',
            tableId: "6037Table",
            tableName: "会计单位信息表",
            columnProp: "taxNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "税号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 200
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6037,
            serialNumber: '6037TableBankName',
            tableId: "6037Table",
            tableName: "会计单位信息表",
            columnProp: "bankName",
            headerAlign: "center",
            align: "left",
            columnLabel: "开户行名称",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 160
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6037,
            serialNumber: '6037TableBankAccount',
            tableId: "6037Table",
            tableName: "会计单位信息表",
            columnProp: "bankAccount",
            headerAlign: "center",
            align: "left",
            columnLabel: "开户行账号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 200
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6037,
            serialNumber: '6037TableActive',
            tableId: "6037Table",
            tableName: "会计单位信息表",
            columnProp: "active",
            headerAlign: "center",
            align: "left",
            columnLabel: "在用",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 40
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6037,
            serialNumber: '6037TableCutoffDate',
            tableId: "6037Table",
            tableName: "会计单位信息表",
            columnProp: "cutoffDate",
            headerAlign: "center",
            align: "left",
            columnLabel: "月结日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6037,
            serialNumber: '6037TableCutoffDateFixDate',
            tableId: "6037Table",
            tableName: "会计单位信息表",
            columnProp: "cutoffDateFixDate",
            headerAlign: "center",
            align: "right",
            columnLabel: "每月几号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 80
          }
        ],
      }
    },
    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({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } 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({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
              }
            })
          } 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')
      }
    },
    created() {
      this.getData();
    }
  }
</script>

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