<template>
  <div class="eibs-tab">

    <c-col :span="24">
      <c-col :span="18">
        <el-form-item label-width="0px">报关单信息</el-form-item>
      </c-col>
      <c-col :span="3" style="text-align:right">
        <el-form-item label-width="0px">
          <c-button size="small" type="primary" @click="onClrdan">
            全部删除
          </c-button>
        </el-form-item>
      </c-col>
      <c-col :span="3" style="text-align:right">
        <el-form-item label-width="0px">
          <el-upload action="#" :before-upload="beforeUpload" :show-file-list="false" accept=".xlsx, .xls">
            <c-button slot="trigger" size="small" type="primary">Excel导入</c-button>
          </el-upload>
        </el-form-item>
      </c-col>
    </c-col>

    <c-col :span="24" style="margin-top:10px;">
      <c-table style="text-align: center" highlight-current-row @row-click="handleDanlstRowClick" :row-class-name="tableDanlstRowClassName" :list="model.recpan.danlst" :paginationShow="true" :border="true" :total="model.recpan.danlst.length">
        <el-table-column label="进口企业名称" width="auto" prop="clinam">
          <template slot-scope="scope">
            <el-form-item label-width="0" :prop="'recpan.danlst.' + scope.$index + '.clinam'">
              <span>{{scope.row.clinam}}</span>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="发票号" width="auto" prop="ineref">
          <template slot-scope="scope">
            <el-form-item label-width="0" :prop="'recpan.danlst.' + scope.$index + '.ineref'">
              <span>{{scope.row.ineref}}</span>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="发票金额" width="auto" prop="ineamt">
          <template slot-scope="scope">
            <el-form-item label-width="0" :prop="'recpan.danlst.' + scope.$index + '.ineamt'">
              <span>{{scope.row.ineamt}}</span>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="关单币种" width="auto" prop="cur">
          <template slot-scope="scope">
            <el-form-item label-width="0" :prop="'recpan.danlst.' + scope.$index + '.cur'">
              <span>{{scope.row.cur}}</span>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="报关单编号" width="auto" prop="cusref">
          <template slot-scope="scope">
            <el-form-item label-width="0" :prop="'recpan.danlst.' + scope.$index + '.cusref'">
              <span>{{scope.row.cusref}}</span>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="报关单金额" width="auto" prop="cusamt">
          <template slot-scope="scope">
            <el-form-item label-width="0" :prop="'recpan.danlst.' + scope.$index + '.cusamt'">
              <span>{{scope.row.cusamt}}</span>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="" prop="det" width="auto" fixed="right">
          <template slot-scope="scope" slot="header">
            <c-button circle style="padding: 4px" class="el-icon-plus" size="mini" @click="handleEdit()">
            </c-button>
            <c-button style="padding: 4px" circle class="el-icon-minus" size="mini" @click="removeRowDanlst(scope)">
            </c-button>
          </template>
          <template slot-scope="scoped">
            <c-button @click="handleEdit(scoped.row)" style="margin-left: 5px" size="small" type="primary">编辑</c-button>
          </template>
        </el-table-column>
      </c-table>
    </c-col>

    <el-dialog title="单据信息详情" :visible.sync="dialogVisibleFlg" :modal-append-to-body="false" width="70%">
      <el-form :model="dataForm" label-suffix=":">
        <c-col :span="12" style="padding-right: 20px;">
          <c-col :span="20">
            <el-form-item label="进口企业名称" :label-width="formLabelWidth" prop="clinam">
              <c-input v-model="dataForm.clinam" placeholder="请输入进口企业名称"></c-input>
            </el-form-item>
          </c-col>
          <c-col :span="20">
            <el-form-item label="发票号" :label-width="formLabelWidth" prop="ineref">
              <c-input v-model="dataForm.ineref" placeholder="请输入发票号"></c-input>
            </el-form-item>
          </c-col>
          <c-col :span="20">
            <el-form-item label="发票金额" :label-width="formLabelWidth" prop="ineamt">
              <c-input-currency v-model="dataForm.ineamt" :currency="dataForm.cur" placeholder="请输入发票金额"></c-input-currency>
            </el-form-item>
          </c-col>
        </c-col>
        <c-col :span="12" style="padding-right: 20px;">
          <c-col :span="20">
            <el-form-item label="关单币种" :label-width="formLabelWidth" prop="cur">
              <c-select v-model="dataForm.cur" placeholder="币种" dbCode="curtxt" />
            </el-form-item>
          </c-col>
          <c-col :span="20">
            <el-form-item label="报关单编号" :label-width="formLabelWidth" prop="cusref">
              <c-input v-model="dataForm.cusref" placeholder="请输入报关单编号"></c-input>
            </el-form-item>
          </c-col>
          <c-col :span="20">
            <el-form-item label="报关单金额" :label-width="formLabelWidth" prop="cusamt">
              <c-input-currency v-model="dataForm.cusamt" :currency="dataForm.cur" placeholder="请输入报关单金额"></c-input-currency>
            </el-form-item>
          </c-col>
        </c-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleAddNewDan">提 交</el-button>
        <el-button @click="dialogVisibleFlg = false">取 消</el-button>
      </div>
    </el-dialog>

    <c-col :span="24" style="margin-top: 50px;">
      <!-- ======================= 左边 ========================= -->
      <c-col :span="12" style="padding-right: 20px">
        <c-col :span="24" style="display: flex;align-item: center;justify-content: flex-start;">
          <div style="width: 100%;">
            <el-form-item label="发票数量" prop="recgrp.rec.inenum">
              <c-input disabled v-model="model.recgrp.rec.inenum" placeholder="请输入发票数量"></c-input>
            </el-form-item>
          </div>
          <div style="margin-left: 20px;">
            <span style="height:32px;line-height:32px">条</span>
          </div>
        </c-col>

        <c-col :span="24" style="display: flex;align-item: center;justify-content: flex-start;">
          <div style="width: 100%;">
            <el-form-item label="报关单数量" prop="recgrp.rec.cusnum">
              <c-input disabled v-model="model.recgrp.rec.cusnum" placeholder="请输入报关单数量"></c-input>
            </el-form-item>
          </div>
          <div style="margin-left: 20px;">
            <span style="height:32px;line-height:32px">条</span>
          </div>
        </c-col>
      </c-col>

      <!-- ======================右边====================== -->
      <c-col :span="12" style="padding-left: 20px">
        <c-col :span="24">
          <el-form-item label="发票总金额" prop="recpan.totamt">
            <c-input-currency disabled v-model="model.recpan.totamt" placeholder="请输入发票总金额"></c-input-currency>
          </el-form-item>
        </c-col>

        <c-col :span="24">
          <el-form-item label="报关单总金额" prop="recpan.bgdamt">
            <c-input-currency disabled v-model="model.recpan.bgdamt" placeholder="报关单总金额"></c-input-currency>
          </el-form-item>
        </c-col>
      </c-col>
    </c-col>

  </div>
</template>
<script>
import event from "../event";

export default {
  inject: ["root"],
  props: ["model", "codes"],
  mixins: [event],
  data() {
    return {
      trnName: "dbezxb",
      currentDanlstIndex: -1,
      dataForm: {
        clinam: "",
        ineref: "",
        ineamt: "0.00",
        cur: "",
        cusref: "",
        cusamt: "0.00"
      },
      dialogVisibleFlg: false,
      opeType: "",
      formLabelWidth: "150px"
    };
  },
  methods: {
    addRowDanlst() {
      let newRow = {
        clinam: "",
        ineref: "",
        ineamt: "0.00",
        cur: "",
        cusref: "",
        cusamt: "0.00"
      };
      let start = 0;
      if (this.model.recpan.danlst) {
        start = this.model.recpan.danlst.length;
      }
      this.model.recpan.danlst.splice(start, 0, newRow);

      this.currentDanlstIndex = this.model.recpan.danlst.length - 1;
    },
    removeRowDanlst() {
      console.log("选中的他行下标", this.currentDanlstIndex);
      if (this.currentDanlstIndex === -1) {
        this.$notify.error({
          title: "错误",
          message: "请选择一条数据删除!"
        });
        return;
      }
      this.model.recpan.danlst.splice(this.currentDanlstIndex, 1);
      this.currentDanlstIndex = -1;
      //this.processSetpan();
      this.refreshLnelstAndCuslst();
    },
    tableDanlstRowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },
    handleDanlstRowClick(row) {
      this.currentDanlstIndex = row.index;
    },
    handleEdit(row) {
      this.dialogVisibleFlg = true;
      this.opeTyp = "ADD";
      if (row) {
        this.opeTyp = "EDIT";
        Object.assign(this.dataForm, row);
      }
    },
    handleAddNewDan() {
      if (this.opeTyp == "ADD") {
        this.addRowDanlst();
      }
      this.dialogVisibleFlg = false;
      this.opeTyp = "";
      this.model.recpan.danlst.splice(
        this.currentDanlstIndex,
        1,
        this.dataForm
      );
      this.dataForm = {
        clinam: "",
        ineref: "",
        ineamt: "0.00",
        cur: "",
        cusref: "",
        cusamt: "0.00"
      };
      this.refreshLnelstAndCuslst();
    },

    beforeUpload(file) {
      if (!this.model.recgrp.rec.rcvtotcur) {
        this.$notify.error({ title: "错误", message: "请输入先应收账款币种" });
        return false;
      }
      console.log(file, "--文件");
      this.file2XLSX(file).then(res => {
        let newData = res[0].sheet.map(item => ({
          clinam: item.hasOwnProperty("进口企业名称")
            ? item["进口企业名称"]
            : "",
          ineref: item.hasOwnProperty("发票号") ? item["发票号"] : "",
          ineamt: item.hasOwnProperty("发票金额")
            ? item["发票金额"].toString()
            : "",
          cusref: item.hasOwnProperty("报关单编号") ? item["报关单编号"] : "",
          cur: this.model.recgrp.rec.rcvtotcur,
          cusamt: item.hasOwnProperty("报关单占用金额")
            ? item["报关单占用金额"].toString()
            : ""
        }));
        this.model.recpan.danlst = this.model.recpan.danlst.concat(newData);
      });
      return false;
    }
  },
  computed: {
    // test() {
    //   this.model.recgrp.lnelst
    //   this.model.recgrp.rec.inenum = 0;
    // }
  },
  created: function() {}
};
</script>
<style>
</style>