<template>
  <div>
    <div class="sty">
      <el-page-header @back="goBack"> </el-page-header>
    </div>
    <br />
    <span>基本信息</span>
    <el-divider></el-divider>
    <!-- 表头 -->
    <div>
      <el-form
        ref="searchForm"
        :model="searchForm"
        label-width="100px"
        size="small"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="单据类型" prop="">
              <el-input v-model="searchForm.billType" disabled></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="单据来源" prop="">
              <el-select
                class="selectStyle"
                v-model="searchForm.source"
                placeholder="请选择"
              >
                <el-option value="工程押金"> </el-option>
                <el-option value="保证金"> </el-option>
                <el-option value="工程价差"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="业务日期" prop="">
              <el-date-picker
                disabled
                class="selectStyle"
                type="datetime"
                placeholder="系统自动生成"
                default-time="23:59:59"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="到期日" prop="">
              <el-date-picker
                class="selectStyle"
                v-model="searchForm.endTime"
                type="datetime"
                placeholder="选择日期时间"
                default-time="23:59:59"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="总金额" prop="">
              <el-input
                disabled
                v-model="searchForm.totalAmount"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="单据状态" prop="">
              <el-select
                class="selectStyle"
                v-model="searchForm.examineStatus"
                placeholder="请选择"
              >
                <el-option value="SAVE" label="暂存"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="备注" prop="">
              <el-input
                v-model="searchForm.remark"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <span>明细</span>
    <el-divider></el-divider>
    <!-- 列表 -->
    <div class="mymain-container">
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
          :row-class-name="rouClassNameFn"
          @row-click="onRowClick"
        >
          <el-table-column align="center" label="序号">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="*经销商名称"
            prop="customerName"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-select
                @change="changeCustomerFn($event, scope.$index)"
                v-model="scope.row.customerId"
                placeholder="请选择"
                filterable
              >
                <el-option
                  v-for="(item, i) in customerList"
                  :key="i"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="费用项目编码"
            prop="projectCode"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.projectCode"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="*项目费用名称"
            prop="projectName"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.projectName"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="*费用承担部门"
            prop="departmentId"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.departmentId"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="现金钱包"
            prop="walletId"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-select
                filterable
                v-model="scope.row.walletId"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in scope.row.walletList"
                  :key="item.customerWalletId"
                  :label="item.customerWalletName"
                  :value="item.customerWalletId"
                >
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="*发票类型"
            prop="invoiceType"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.invoiceType"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="不含税金额"
            prop="afterTaxAmount"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input
                :min="0"
                oninput="if(value<0)value=0"
                v-model="scope.row.afterTaxAmount"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="税额"
            prop="tax"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input
                :min="0"
                oninput="if(value<0)value=0"
                v-model="scope.row.tax"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="总金额"
            prop="tax"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input
                :min="0"
                oninput="if(value<0)value=0"
                v-model="scope.row.totalAmount"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="备注"
            prop="remark"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark"></el-input>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div class="fl">
        <el-button type="primary" size="small" @click="addFn"
          >提交审核</el-button
        >
        <el-button type="primary" size="small" @click="resetFn">重置</el-button>
      </div>
      <div class="fr">
        <el-button type="primary" size="small" @click="addRowFn"
          >添加行</el-button
        >
        <el-button type="primary" size="small" @click="delRowFn"
          >删除行</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import {
  getCustomerList,
  getFinanceCustomerList,
  getFinanceOtherReceAdd,
  getFinanceOtherReceApply,
  getFinanceOtherReceList,
} from "@/api/finance/receivable_list";
export default {
  data() {
    return {
      searchForm: {
        source: "",

        billType: "其他应收单",
        examineStatus: "",
      },
      dataList: [],
      customerList: [], //经销商数据
      listLoading: false, // 列表加载loading
      walletList: [], //现金钱包数据
      delIndex: null,
    };
  },
  created() {
    this.getCustomerList();
  },
  methods: {
    //重置
    resetFn() {
      this.searchForm = {};
    },
    rouClassNameFn({ row, rowIndex }) {
      //把每一行的索引放进row
      row.index = rowIndex;
    },
    onRowClick(row, event, column) {
      this.delIndex = row.index;
    },
    //删除行
    delRowFn() {
      this.dataList.splice(this.delIndex, 1);
    },

    //新增
    async addFn() {
      if (this.dataList.length == 0) {
        this.$message.error("请添加数据");
        return;
      }
      let aaa = this.dataList.find((v) => {
        if (v.afterTaxAmount == "") {
          this.$message.error("请输入不含税金额");
          return v;
        }
        if (v.customerId == "") {
          this.$message.error("请选择经销商");
          return v;
        }
        if (!v.walletId) {
          this.$message.error("请选择现金钱包");
          return v;
        }
        if (v.tax == "") {
          this.$message.error("请输入税额");
          return v;
        }
        if (v.totalAmount == "") {
          this.$message.error("请输入总金额");
          return v;
        }
      });
      console.log(aaa);
      if (aaa != undefined) {
        return;
      }

      this.dataList.forEach((v) => {
        let res = this.customerList.filter((i) => i.id == v.customerId);
        console.log(res);
        v.customerName = res[0].name;
        v.customerNumber = res[0].number;
        let res2 = v.walletList.filter((j) => j.customerWalletId == v.walletId);
        v.customerWalletId = res2[0].customerWalletId;
        v.walletId = res2[0].walletRebateId;
      });

      await getFinanceOtherReceAdd({
        items: this.dataList,
        ...this.searchForm,
      });
      const res = await getFinanceOtherReceList({ pageSize: 1, pageNum: 1 });
      await getFinanceOtherReceApply({ id: res.data.records[0].id });
      this.$message.success("提审成功");
      this.$emit("updateList");
      this.$parent.showPage = 1;
      this.resetFn();
    },
    //选择经销商名称事件
    async changeCustomerFn(v, index) {
      console.log(v, 322311);
      const res = await getFinanceCustomerList({
        customerId: v,
        type: "COMMONLY",
      });
      this.$set(this.dataList[index], "walletList", res.data);
      console.log(this.dataList, "kkk");
      // this.walletList = res.data;
    },
    //获取经销商数据
    async getCustomerList() {
      const res = await getCustomerList({ pageNum: 1, pageSize: -1 });
      this.customerList = res.data.records;
    },
    //添加行数据
    addRowFn() {
      this.dataList.push({
        customerId: "",
        projectCode: "",
        projectName: "",
        departmentId: "",
        walletId: "",
        invoiceType: "",
        afterTaxAmount: "",
        tax: "",
        totalAmount: "",
      });
    },
    goBack() {
      this.$parent.showPage = 1;
    },
  },
};
</script>

<style lang="scss" scoped>
.selectStyle {
  width: 100%;
}
.mar {
  margin-top: 20px;
}
</style>
