<template>
  <div class="app-container">
    <span>返利单</span>
    <el-divider></el-divider>
    <!-- 表头 -->
    <div>
      <el-form
        ref="searchForm"
        :rules="rules"
        :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 disabled placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="返利日期" prop="theTime">
              <el-date-picker
                class="selectStyle"
                v-model="searchForm.theTime"
                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="remark">
              <el-input
                v-model="searchForm.remark"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="制单人" prop="">
              <el-input
                disabled
                v-model="searchForm.createBy"
                placeholder="请输入"
              ></el-input>
            </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.createTime"
                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-row>
      </el-form>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div class="fr">
        <el-button type="primary" size="small">批量导入</el-button>
      </div>
    </div>
    <!-- 列表 -->
    <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="序号"
            type="index"
            width="100"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="经销商编码"
            prop="customerNumber"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input disabled v-model="scope.row.customerNumber"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="经销商名称"
            prop="customerName"
            min-width="200"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-select
                @change="changeCustomerFn($event, scope.$index, scope.row)"
                v-model="scope.row.customerId"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in customerData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="返利类型"
            prop="customerWalletId"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.customerWalletId"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in scope.row.walletList"
                  :key="item.customerWalletId"
                  :label="item.name"
                  :value="item.customerWalletId"
                >
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="返利总金额"
            prop="amount"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model.number="scope.row.amount"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="暂扣返利"
            prop="withholdAmount"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model.number="scope.row.withholdAmount"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="政策文件流水号"
            prop="policyFileNo"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.policyFileNo"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="政策文号"
            prop="policyDocNo"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.policyDocNo"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="政策年份"
            prop="policyYear"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.policyYear"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="政策月份"
            prop="policyMonth"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.policyMonth"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="政策归属部门"
            prop="policyOrg"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.policyOrg"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="客户区域"
            prop="customerArea"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.customerArea"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="客户属性"
            prop="customerAttr"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.customerAttr"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="建立实际归属客户"
            prop="rewardActualCustomers"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.rewardActualCustomers"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="备注1"
            prop="remark1"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark1"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="备注2"
            prop="remark2"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark2"></el-input>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <br />

    <!-- 按钮 -->
    <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="submitFn"
          >提交审批</el-button
        >
        <el-button type="primary" size="small" @click="cancelFn"
          >重置</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 { mapGetters } from "vuex";
import {
  getCustomerList,
  getWalletCustomerList,
  getRebateOrderAdd,
  getRebateOrderApply,
  getRebateOrderList,
} from "@/api/finance/rebate_form";
export default {
  data() {
    return {
      dataList: [],
      listLoading: false, // 列表加载loading
      customerData: [],
      searchForm: {
        theTime: "",

        remark: "",
        createBy: "",
      },
      delIndex: null,
      rules: {
        theTime: [
          {
            required: true,
            message: "请选择返利日期",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["name"]),
  },
  created() {
    this.getCustomerData();
    this.searchForm.createBy = this.name;
  },
  methods: {
    //重置
    cancelFn() {
      this.searchForm = {
        theTime: "",
        createTime: "",
        remark: "",
        createBy: "",
      };
    },
    rouClassNameFn({ row, rowIndex }) {
      //把每一行的索引放进row
      row.index = rowIndex;
    },
    onRowClick(row, event, column) {
      this.delIndex = row.index;
    },
    //删除行
    delRowFn() {
      this.dataList.splice(this.delIndex, 1);
    },
    //提交审批
    async submitFn() {
      let res = await getRebateOrderList({ pageSize: -1, pageNum: 1 });
      console.log(res);
      let index = res.data.records.length - 1;
      let arr = res.data.records;
      await getRebateOrderApply({ id: arr[index].rebateOrderId });
      this.$message.success("提交审批成功");
    },
    //新增
    async addFn() {
      let arr;
      await this.$refs.searchForm.validate();
      this.dataList.forEach((v) => {
        arr = v.walletList.filter(
          (i) => i.customerWalletId == v.customerWalletId
        );
        console.log(arr);
        v.walletName = arr[0].name;
      });
      console.log(this.dataList);
      await getRebateOrderAdd({ ...this.searchForm, items: this.dataList });
      this.$message.success("保存成功");
      this.$router.push("/finance/rebate_list");
    },
    //选择经销商名称事件
    async changeCustomerFn(v, index, value) {
      value.customerWalletId = "";

      const res = await getWalletCustomerList({
        customerId: v,
        type: "REBATE",
      });
      const res2 = this.customerData.filter((i) => i.id == v);
      console.log(res2, 8888);

      this.$set(this.dataList[index], "customerNumber", res2[0].number);
      this.$set(this.dataList[index], "customerName", res2[0].name);

      this.$set(this.dataList[index], "walletList", res.data);
    },
    //获取经销商数据
    async getCustomerData() {
      const res = await getCustomerList({ pageSize: -1, pageNum: 1 });
      this.customerData = res.data.records;
    },
    //添加行
    addRowFn() {
      this.dataList.push({
        customerNumber: "",
        customerName: "",
        customerWalletId: "",
        amount: null,
        withholdAmount: null,
        policyFileNo: "",
        policyDocNo: "",
        policyYear: "",
        policyMonth: "",
        policyOrg: "",
        customerArea: "",
        customerAttr: "",
        rewardActualCustomers: "",
        remark1: "",
        remark2: "",
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.selectStyle {
  width: 100%;
}
</style>
