<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <!--     <el-form-item label="结算状态" prop="settlementStatus">
             <el-select
               v-model="queryParams.settlementStatus"
               placeholder="请选择结算状态"
               clearable
               style="width: 240px"
             >
               <el-option label="未达到结算条件" value="1"/>
               <el-option label="待结算" value="2"/>
               <el-option label="已结算" value="3"/>
             </el-select>
           </el-form-item>-->

      <el-form-item label="商品ID" prop="itemId">
        <el-input
          placeholder="请输入商品ID"
          style="width: 240px"
          v-model="queryParams.itemId"
        >
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          plain
          size="mini"
          @click="upload"
          style="margin-bottom: 15px"
        >导出
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="typeList" ref="multipleTable">

      <el-table-column label="商品归属人" align="center" prop="itemCreateName" width="150"></el-table-column>
      <el-table-column label="达人归属人" align="center" prop="promoterCreateName" width="150"></el-table-column>
      <el-table-column label="商品名称" align="center" prop="itemName" width="200"></el-table-column>
      <el-table-column label="商品ID" align="center" prop="itemIds" width="200"></el-table-column>
      <el-table-column label="达人名称" align="center" prop="promoterName" width="150"></el-table-column>
      <el-table-column label="达人ID" align="center" prop="promoterId" width="150"></el-table-column>
      <el-table-column label="有效单数" align="center" prop="validOrderNum" width="150"></el-table-column>
      <el-table-column label="达人激励金额" align="center" prop="anchorAmount" width="150"></el-table-column>
      <el-table-column label="推荐人激励" align="center" prop="referenceAmount" width="150"></el-table-column>
      <el-table-column width="200" label="推荐人信息" align="center" prop="promoterInfo">
        <template slot-scope="scope">
          <el-input
            type="textarea"
            v-model="scope.row.promoterInfo"
            placeholder="请输入内容"
          >
          </el-input>
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remarks" width="150"></el-table-column>
      <el-table-column width="150" label="结算状态" align="center" prop="settlementStatusStr">
      </el-table-column>
      <el-table-column label="操作" align="center" prop="action" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" :disabled="scope.row.settlementStatus == 2" @click="removeRule(scope.row)"
          >付款
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :pageSizes="[10, 20, 30, 50]"
    />

    <!-- 添加直播 -->
  </div>
</template>

<script>
  import {downFilePost} from "@/api/supplyChain/supplyChain";
  import {
    detailList,
    updateFinancialReviewDetails,
  } from "@/api/goodsManagement/goods";

  export default {
    name: "Rule-Config",
    components: {},
    data() {
      return {
        consigneeEdit: false,

        // 遮罩层
        loading: true,
        // 选中数组
        ids: null,
        okIds: null,
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 字典表格数据
        typeList: [],
        //上传文件类型
        fileType: [".doc", ".xls", ".xlsx", ".ppt", ".pdf", ".csv"],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        openFp: false,
        formFp: {},
        // 是否可编辑
        edit: false,
        // 分配销售
        openAllocation: false,
        // 选择的销售id
        saleId: undefined,
        // 销售列表
        saleList: [],
        // 分配销售确定loading
        confirmLoadingSale: false,
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          settlementStatus: undefined
        },
        uploadDate: new Date(),
        //页面所有的下拉数据
        queryParamsList: {},
        // 表单参数
        form: {},
        formBroadcast: {
          ruleDetails: [],
        },
        //上传报表下拉数据
        formList: {},
        fileList: [],
        accept: "",
        //获取销售线索文件流
        dateFile: new FormData(),
        //销售线索上传loading
        confirmLoading: false,
        // 表单校验
        rules: {},
        rulesBroadcast: {
          ruleName: {
            required: true,
            message: "规则名称必填",
            trigger: "blur",
          },
          ruleType: {
            required: true,
            message: "规则类型必选",
            trigger: "change",
          },
        },
        // 分配销售展示列表
        ksInfo: null,
        userId: null,
      };
    },
    created() {
      this.userId = this.$store.getters.userId;

      // let start = new Date();
      // start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
      // let date = new Date(start);
      // let y = date.getFullYear();
      // let MM = date.getMonth() + 1;
      // MM = MM < 10 ? "0" + MM : MM;
      // let dd = date.getDate();
      // dd = dd < 10 ? "0" + dd : dd;
      // this.uploadDate = `${y}-${MM}-${dd}`;

      if (this.$route && this.$route.query.statDate && this.$route.query.promoterId) {
        this.getList();
      }
    },
    filters: {},
    methods: {
      removeRule(item) {
        this.$modal
          .confirm(`请确认是否结算此数据？`)
          .then(() => {
            var params = {};
            params.id = +item.id;
            params.status = 2;
            params.promoterId = item.promoterId;
            params.promoterInfo = item.promoterInfo;
            updateFinancialReviewDetails(params)
              .then((res) => {
                this.getList();
                this.$modal.msgSuccess("结算成功");
              })
              .catch(() => {
              });
          })
          .catch(() => {
          });
      },

      /** 查询字典类型列表 */
      getList() {
        this.loading = true;
        this.typeList = [];
        detailList({
          statDate: this.$route.query.statDate,
          promoterId: this.$route.query.promoterId,
          ...this.queryParams,
        })
          .then((response) => {
            this.loading = false;
            this.typeList = response.rows;
            this.total = response.total;
          })
          .catch((err) => {
            console.log(err);
          });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.confirmLoading = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {};
        this.dateFile = null;
        this.resetForm("formBroadcast");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        let start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
        let date = new Date(start);
        let y = date.getFullYear();
        let MM = date.getMonth() + 1;
        MM = MM < 10 ? "0" + MM : MM;
        let dd = date.getDate();
        dd = dd < 10 ? "0" + dd : dd;
        this.uploadDate = `${y}-${MM}-${dd}`;
        this.resetForm("queryForm");
        this.handleQuery();
      },
      upload() {
        downFilePost("/settlement/exportPromoterSettlementDetail", {
          statDate: this.$route.query.statDate,
          promoterId: this.$route.query.promoterId,
        }).then((res) => {
          let blob = new Blob([res], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          });
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "激励结算详情.xlsx"; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        });
      },

    },
  };
</script>
<style lang="scss" scoped>
  .entire-line {
    width: 100%;
  }

  ::v-deep .entire-line .el-form-item__content {
    width: 60%;
  }

  ::v-deep .el-form-item__label {
    white-space: nowrap;
  }
</style>
<style scoped lang="scss">
  ::v-deep a {
    color: #409eff;
    display: block;
    text-align: left;
  }

  .item-name-calss p {
    margin: 0;
    text-align: left;
  }

  .tableBox {
    width: 100%;
  }
</style>
