<template>
  <div>
      <basic-container v-if="isShow">
          <avue-crud :option="option"
                     :table-loading="loading"
                     :data="data"
                     :page.sync="page"
                     :permission="permissionList"
                     :before-open="beforeOpen"
                     v-model="form"
                     ref="crud"
                     id="out-table"
                     :header-cell-class-name="headerClassName"
                     @row-update="rowUpdate"
                     @row-save="rowSave"
                     @row-del="rowDel"
                     @search-change="searchChange"
                     @search-reset="searchReset"
                     @selection-change="selectionChange"
                     @current-change="currentChange"
                     @size-change="sizeChange"
                     @refresh-change="refreshChange"
                     @resetColumn="resetColumnTwo('crud', 'option', 'optionBack', 320)"
                     @saveColumn="saveColumnTwo('crud', 'option', 'optionBack', 320)"
                     @on-load="onLoad">
              <template slot="menuLeft">
                  <el-button type="success"
                             size="small"
                             icon="el-icon-plus"
                             plain
                             @click="newbillFun">新建申请
                  </el-button>
                  <el-button type="danger"
                             size="small"
                             icon="el-icon-delete"
                             plain
                             @click="handleDelete">删 除
                  </el-button>
              </template>
              <template slot="menu" slot-scope="{row}">
                  <el-button type="text"
                             size="small"
                             @click="editFun(row)">编辑
                  </el-button>
                  <el-button type="text"
                             size="small"
                             @click="rowDel(row)">删除
                  </el-button>
              </template>
              <template slot-scope="scope" slot="stlMode">
                  <span>{{scope.row.stlMode?'月结':'票结'}}</span>
              </template>
          </avue-crud>
      </basic-container>
      <fininvoicesDetails ref="fininvoicesDetails" :editSave="editSave" v-if="!isShow" @goBack="goBack"></fininvoicesDetails>
  </div>
</template>

<script>
import {
    fininvoicesList,
    fininvoicesDetail,
    fininvoicesRemove, fininvoicesSubmit
} from "@/api/iosBasicData/fininvoices";
import fininvoicesDetails from "@/views/iosBasicData/fininvoicesApplyfor/fininvoicesDetails.vue";
  import {mapGetters} from "vuex";

  export default {
      components:{ fininvoicesDetails},
    data() {
      return {
          // 详情页面和列表切换
          isShow:true,
          editSave:false, // 编辑还是保存
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option:{},
        optionBack: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
            menuWidth:100,
          column: [
            {
              label: "单据编号",
              prop: "billNo",
              search:true,
              rules: [{
                required: true,
                message: "请输入单据编号",
                trigger: "blur"
              }],
              overHidden:true,
            },
            {
                label: "客户中文名称",
                prop: "corpCnName",
                width:"120",
                overHidden:true,
            },
            {
                label: "开票单位中文名称",
                prop: "invCorpCnName",
                width:"160",
                overHidden:true,
            },
            {
                label: "合计本位币(CNY)",
                prop: "amountLoc",
                width: "120",
                overHidden:true,
            },
            {
              label: "单据日期",
              prop: "billDate",
              rules: [{
                required: true,
                message: "请输入单据日期",
                trigger: "blur"
              }],
                overHidden:true,
            },
            {
              label: "发票号码",
              prop: "invoiceNo",
              rules: [{
                required: true,
                message: "请输入发票号码",
                trigger: "blur"
              }],
                overHidden:true,
            },
            {
              label: "开票日期",
              prop: "invoiceDate",
              rules: [{
                required: true,
                message: "请输入开票日期",
                trigger: "blur"
              }],
                overHidden:true,
            },
            {
              label: "原发票号码",
              prop: "origInvoiceNo",
                width:"120",
                overHidden:true,
            },
            {
              label: "税务发票号码",
              prop: "taxInvoiceNo",
                width:"150",
                overHidden:true,
            },
            {
              label: "全电发票",
              prop: "invType",
                overHidden:true,
            },
            {
              label: "普通发票/专用发票",
              prop: "taxType",
                width:"160",
                overHidden:true,
            },
            {
              label: "税率 %",
              prop: "taxRate",
              rules: [{
                required: true,
                message: "请输入税率 %",
                trigger: "blur"
              }],
                overHidden:true,
            },
            {
              label: "结算方式",
              prop: "stlMode",
                overHidden:true,
            },
            {
              label: "发票币种",
              prop: "invCurCode",
              rules: [{
                required: true,
                message: "请输入发票币种",
                trigger: "blur"
              }],
                overHidden:true,
            },
            {
              label: "客户英文名称",
              prop: "corpEnName",
                width:"120",
                overHidden:true,
            },
            {
              label: "开票单位英文名称",
              prop: "invCorpEnName",
                width:"150",
                overHidden:true,
            },
            {
              label: "开票单位税号",
              prop: "invCorpTaxNo",
                width:"140",
                overHidden:true,
            },
            {
              label: "开票单位CNY开户银行",
              prop: "invCorpAccountBankCny",
                width:"160",
                overHidden:true,
            },
            {
              label: "开票单位CNY银行账号",
              prop: "invCorpAccountNoCny",
                width:"160",
                overHidden:true,
            },
            {
              label: "开票单位USD开户银行",
              prop: "invCorpAccountBankUsd",
                width:"160",
                overHidden:true,
            },
            {
              label: "开票单位USD银行账号",
              prop: "invCorpAccountNoUsd",
                width:"160",
                overHidden:true,
            },
            {
              label: "船名/航次",
              prop: "vesselVoyno",
                width:"100",
                overHidden:true,
            },
            {
              label: "开船日",
              prop: "etd",
                overHidden:true,
            },
            {
              label: "装货港",
              prop: "pol",
                overHidden:true,
            },
            {
              label: "卸货港",
              prop: "pod",
                overHidden:true,
            },
            {
              label: "目的地",
              prop: "destination",
                overHidden:true,
            },
            {
              label: "箱型/箱量",
              prop: "containers",
                width:"100",
                overHidden:true,
            },
            {
              label: "币种",
              prop: "curCode",
                overHidden:true,
            },
            {
              label: "原汇率",
              prop: "exrate",
                overHidden:true,
            },
            {
              label: "是否转换为人民币",
              prop: "isExchangeToCny",
                width:"140",
                dicData:[{
                    label:'否',
                    value:0
                },{
                    label:'是',
                    value:1
                }],
                overHidden:true,
            },
            {
              label: "业务类型",
              prop: "businessTypes",
                overHidden:true,
            },
            {
              label: "业务单据编号",
              prop: "businessNo",
                width:"120",
                overHidden:true,
            },
            {
              label: "对账单编号",
              prop: "checkNo",
                width:"120",
                overHidden:true,
            },
            {
              label: "MBL NO",
              prop: "mblno",
                overHidden:true,
            },
            {
              label: "HBL NO",
              prop: "hblno",
                overHidden:true,
            },
            {
              label: "订舱",
              prop: "bookingNo",
                overHidden:true,
            },
            {
              label: "中文船名",
              prop: "vesselCnName",
                overHidden:true,
            },
            {
              label: "英文船名",
              prop: "vesselEnName",
                overHidden:true,
            },
            {
              label: "航次",
              prop: "voyageNo",
                overHidden:true,
            },
            {
              label: "财务开始日期",
              prop: "accountDateFrom",
                width:"100",
                overHidden:true,
            },
            {
              label: "财务结束日期",
              prop: "accountDateTo",
                width:"100",
                overHidden:true,
            },
            {
              label: "申请人",
              prop: "applicantName",
                overHidden:true,
            },
            {
              label: "审核人",
              prop: "approverName",
                overHidden:true,
            },
            {
              label: "是否代理发票",
              prop: "isAgent",
                width:"120",
                dicData:[{
                    label:'否',
                    value:0
                },{
                    label:'是',
                    value:1
                }],
                overHidden:true,
            },
            {
              label: "是否DK",
              prop: "isDk",
                dicData:[{
                    label:'否',
                    value:0
                },{
                    label:'是',
                    value:1
                }],
                overHidden:true,
            },
            {
              label: "是否RP",
              prop: "isRp",
                dicData:[{
                    label:'否',
                    value:0
                },{
                    label:'是',
                    value:1
                }],
                overHidden:true,
            },
            {
              label: "RP名称",
              prop: "rpName",
                overHidden:true,
            },
            {
              label: "导出日期",
              prop: "exportDate",
              width: "100",
                overHidden:true,
            },
            {
              label: "导出人",
              prop: "exportName",
                overHidden:true,
            },
            {
              label: "是否导出船名航次",
              prop: "isExportVslvoy",
                width: "140",
                dicData:[{
                    label:'否',
                    value:0
                },{
                    label:'是',
                    value:1
                }],
                overHidden:true,
            },
            {
              label: "是否导出主单号",
              prop: "isExportMblno",
                width: "140",
                dicData:[{
                    label:'否',
                    value:0
                },{
                    label:'是',
                    value:1
                }],
                overHidden:true,
            },
            {
              label: "是否导出分单号",
              prop: "isExportHblno",
                width: "140",
                dicData:[{
                    label:'否',
                    value:0
                },{
                    label:'是',
                    value:1
                }],
                overHidden:true,
            },
            {
              label: "发票本位币(CNY)",
              prop: "amountCny",
                width: "140",
                overHidden:true,
            },
            {
              label: "发票USD",
              prop: "amountUsd",
              width: "100",
                overHidden:true,
            },
            {
              label: "税额(CNY)",
              prop: "amountTax",
                width: "100",
                overHidden:true,
            },
            {
              label: "全电发票备注",
              prop: "elecRemarks",
                width: "120",
                overHidden:true,
            },
            {
              label: "银行进账单出票人名称",
              prop: "bankReceiptCorpName",
                width: "160",
                overHidden:true,
            },
            {
              label: "银行进账单出票银行",
              prop: "bankReceiptBankName",
                width: "160",
                overHidden:true,
            },
            {
              label: "银行进账单出票银行账号",
              prop: "bankReceiptAccountNo",
                width: "160",
                overHidden:true,
            },
            {
              label: "银行进账单票据种类",
              prop: "bankReceiptBillType",
                width: "160",
                overHidden:true,
            },
            {
              label: "银行进账单票据张数",
              prop: "bankReceiptBillCount",
                width: "160",
                overHidden:true,
            },
            {
              label: "银行进账单是否已进账(收款)",
              prop: "bankReceiptIsIncome",
                width: "180",
                overHidden:true,
            },
            {
              label: "备注",
              prop: "remarks",
              rules: [{
                required: true,
                message: "请输入备注",
                trigger: "blur"
              }],
                overHidden:true,
            },
          ]
        },
        data: []
      };
    },
      watch:{

      },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.fininvoices_add, false),
          viewBtn: this.vaildData(this.permission.fininvoices_view, false),
          delBtn: this.vaildData(this.permission.fininvoices_delete, false),
          editBtn: this.vaildData(this.permission.fininvoices_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
      async created() {
          this.option = await this.getColumnData(this.getColumnName(320), this.optionBack);
      },
    methods: {
        // 新建发票
      newbillFun(){
          this.isShow = false;
          this.editSave = false
      },
        // 编辑
        editFun(row) {
            this.isShow = false
            this.editSave = true
            this.$nextTick(()=>{
                this.$refs.fininvoicesDetails.fininvoicesDetailfun(row.id)
            })
        },
        // 详情的返回列表
        goBack() {
            // 初始化数据
            // this.detailData = this.$options.data().detailData;
            if (JSON.stringify(this.$route.query) != "{}") {
                this.$router.$avueRouter.closeTag();
                this.$router.push({
                    path: "/iosBasicData/fininvoices/index"
                });
            }
            this.isShow = true;
            this.onLoad(this.page, this.search);
        },


      rowSave(row, done, loading) {
        fininvoicesSubmit(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        fininvoicesSubmit(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return fininvoicesRemove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return fininvoicesRemove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          fininvoicesDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        fininvoicesList(page.currentPage, page.pageSize,
            {...Object.assign(params, this.query),type:"申请",billNoFormat:'FPSQ',businessTypeCode:'FPSQ'}).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },

        //自定义列保存
        async saveColumnTwo(ref, option, optionBack, code) {
            /**
             * 已定义全局方法，直接使用，saveColumnData保存列数据方法,参数传值(表格名称，当前表格的option数据)
             * 已定义全局方法，直接使用，getColumnName方法用来获取枚举值，参数根据自己定义的code值获取中文名
             * 一定要执行异步操作，要等接口成功返回，才能执行下一行代码
             */
            const inSave = await this.saveColumnData(this.getColumnName(code), this[option]);
            if (inSave) {
                this.$message.success("保存成功");
                //关闭窗口
                this.$refs[ref].$refs.dialogColumn.columnBox = false;
            }
        },
        //自定义列重置
        async resetColumnTwo(ref, option, optionBack, code) {
            this[option] = this[optionBack];
            const inSave = await this.delColumnData(this.getColumnName(code), this[optionBack]);
            if (inSave) {
                this.$message.success("重置成功");
                this.$refs[ref].$refs.dialogColumn.columnBox = false;
            }
        },

        // 更改表格颜色
        headerClassName(tab) {
            //颜色间隔
            let back = ""
            if (tab.columnIndex >= 0 && tab.column.level === 1) {
                if (tab.columnIndex % 2 === 0) {
                    back = "back-one"
                } else if (tab.columnIndex % 2 === 1) {
                    back = "back-two"
                }
            }
            return back;
        },

    }
  };
</script>

<style scoped>
::v-deep#out-table .back-one {
    background: #ecf5ff !important;
    text-align: center;
}

::v-deep#out-table .back-two {
    background: #ecf5ff !important;
    text-align: center;
}
</style>
