<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="companyName">
        <el-input
          placeholder="请输入公司名称"
          v-model="queryParams.companyName"
          style="width: 240px"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="广告主名称" prop="advertiserName">
        <el-input
          placeholder="请输入广告主名称"
          v-model="queryParams.advertiserName"
          style="width: 240px"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          placeholder="请输入广告主名称"
          v-model="queryParams.productName"
          style="width: 240px"
        >
        </el-input>
      </el-form-item>

      <el-form-item label="投放类型" prop="releaseType">
        <el-select
          v-model="queryParams.releaseType"
          placeholder="选择投放类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in queryParamsList.releaseTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="返点类型" prop="rebateType">
        <el-select
          v-model="queryParams.rebateType"
          placeholder="请选择返点类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in queryParamsList.rebateTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="销售" prop="saleName">
        <el-select
          v-model="queryParams.saleName"
          placeholder="请选择销售"
          clearable
          filterable
          style="width: 240px"
        >
          <el-option
            v-for="item in queryParamsList.saleLeaderList"
            :key="item.value"
            :label="item.label"
            :value="item.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          style="width: 240px"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="saleName">
        <el-select
          v-model="queryParams.saleName"
          placeholder="请选择销售"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in queryParamsList.saleLeaderList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </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-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-document-add"
          size="mini"
          @click="handleAdd"
          >新增政策</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="typeList"
      ref="multipleTable"
      style="width: 100%"
    >
      <el-table-column
        label="公司名称"
        align="center"
        prop="companyName"
        fixed
        width="150"
      >
      </el-table-column>
      <el-table-column
        label="广告主名称"
        align="center"
        prop="advertiserName"
        fixed
        width="150"
      >
      </el-table-column>
      <el-table-column
        label="产品名称"
        align="center"
        prop="productName"
        width="150"
      >
      </el-table-column>
      <el-table-column
        label="投放类型"
        align="center"
        prop="releaseType"
        width="150"
      >
        <template slot-scope="scope">
          <div v-if="!!scope.row.releaseType">
            <span v-for="(item, index) in scope.row.releaseType.split(',')">
              {{ item | releaseTypeChinese }}<br />
            </span>
          </div>
          <div v-else>-</div>
        </template>
      </el-table-column>
      <el-table-column
        label="销售"
        align="center"
        prop="saleName"
        width="150"
      />
      <el-table-column
        label="状态"
        align="center"
        prop="approvedStatus"
        width="150"
      >
        <template slot-scope="scope">
          <div>
            {{
              scope.row.approvedStatus == 0
                ? "草稿"
                : scope.row.approvedStatus == 1
                ? "待审核"
                : scope.row.approvedStatus == 2
                ? "审核中"
                : scope.row.approvedStatus == 3
                ? "审核通过"
                : scope.row.approvedStatus == 4
                ? "审核拒绝"
                : "其他"
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="是否预付"
        align="center"
        prop="advancePay"
        width="150"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.advancePay == 0 ? "否" : "是" }}
          </div>
        </template>
      </el-table-column>

      <el-table-column
        label="账期"
        align="center"
        prop="accountingPeriod"
        width="150"
      >
      </el-table-column>
      <el-table-column
        label="返点类型"
        align="center"
        prop="rebateType"
        width="150"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.rebateType | rebateTypeChinese }}
          </div>
        </template>
      </el-table-column>

      <el-table-column
        label="返点比例"
        align="center"
        prop="rebateRate"
        width="150"
      >
      </el-table-column>
      <el-table-column
        label="操盘结算类型"
        align="center"
        prop="operateSettlementType"
        width="150"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.operateSettlementType == "1" ? "服务费" : "货品佣金" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="运营方式"
        align="center"
        prop="promotionType"
        width="150"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.promotionType == "0" ? "自运营" : "客户运营" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="政策日期"
        align="center"
        prop="policyStartDate"
        width="150"
      >
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remarks"
        width="200"
        showOverflowTooltip
      >
      </el-table-column>
      <!-- 、、操作、审核理由 -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAllocationOne(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handlePersonVisible(scope.row)"
            >查看人员</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="审核理由"
        align="center"
        prop="remarks"
        width="200"
        showOverflowTooltip
      >
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="900px"
      append-to-body
      :before-close="cancel"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        :inline="true"
      >
        <div style="width: 100%; display: flex">
          <div style="flex: 1">
            <el-form-item
              label="公司名称"
              prop="companyName"
              style="width: 100%"
            >
              <el-select
                v-model="form.companyName"
                placeholder="选择投放类型"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="item in queryParamsList.policyCustomerList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item
              label="广告主名称"
              prop="advertiserName"
              style="width: 100%"
            >
              <el-input
                disabled
                placeholder="请输入广告主名称"
                v-model="form.advertiserName"
                style="width: 240px"
              >
              </el-input>
            </el-form-item>
            <el-form-item
              label="产品名称"
              prop="productName"
              style="width: 100%"
            >
              <el-input
                placeholder="请输入产品名称"
                v-model="form.productName"
                style="width: 240px"
              >
              </el-input>
            </el-form-item>
            <el-form-item
              label="投放类型"
              prop="releaseType"
              style="width: 100%"
            >
              <el-select
                v-model="form.releaseType"
                placeholder="选择投放类型"
                clearable
                multiple
                style="width: 240px"
              >
                <el-option
                  v-for="item in queryParamsList.releaseTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="销售" prop="saleId" style="width: 100%">
              <el-select
                v-model="form.saleId"
                placeholder="请选择销售"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="item in queryParamsList.saleLeaderList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item
              label="我司主体"
              prop="companySubjectId"
              style="width: 100%"
            >
              <el-select
                v-model="form.companySubjectId"
                placeholder="请选择我司主体"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="item in queryParamsList.companySubjectInfoList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item
              label="运营方式"
              prop="promotionType"
              style="width: 100%"
            >
              <el-radio-group v-model="form.promotionType">
                <el-radio-button label="0">自运营</el-radio-button>
                <el-radio-button label="1">客户运营</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </div>
          <div style="flex: 1">
            <el-form-item
              label="是否预付"
              prop="advancePay"
              v-if="form.releaseType.includes('1')"
            >
              <el-radio-group v-model="form.advancePay">
                <el-radio-button label="0">否</el-radio-button>
                <el-radio-button label="1">是</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item
              label="返点类型"
              prop="rebateType"
              v-if="form.releaseType.includes('1')"
            >
              <el-select
                v-model="form.rebateType"
                placeholder="请选择返点类型"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="item in queryParamsList.rebateTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item
              label="返点比例"
              prop="rebateRate"
              v-if="form.releaseType.includes('1')"
            >
              <el-input-number
                v-model="form.rebateRate"
                :precision="2"
                :step="0.01"
                :min="0"
              ></el-input-number>
            </el-form-item>
            <el-form-item
              label="账期"
              prop="accountingPeriod"
              v-if="form.releaseType.includes('1')"
            >
              <el-input-number
                v-model="form.accountingPeriod"
                :precision="0"
                :step="1"
                :min="1"
              ></el-input-number>
            </el-form-item>
            <el-form-item
              label="操盘结算类型"
              prop="operateSettlementType"
              v-if="form.releaseType.includes('2')"
            >
              <el-radio-group v-model="form.operateSettlementType">
                <el-radio-button label="1">服务费</el-radio-button>
                <el-radio-button label="2">货品佣金</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="起止时间" prop="createTime">
              <el-date-picker
                v-model="form.createTime"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                style="width: 240px"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="备注" prop="remarks">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入内容"
                v-model="form.remarks"
                maxlength="500"
                show-word-limit
                style="width: 240px"
              >
              </el-input>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="confirmLoading"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { refreshCache } from "@/api/system/dict/type";
import vueUpload from "../trader/components/vueUpload";
import FileUpload from "../trader/components/upload";

import { getQueryRolesList } from "@/api/trader/trader";
import {
  getSelectPolicyList,
  getSelectCompanySubjectInfoList,
  addSaleFinancePolicyInfo,
  getSelectPolicyCustomerList,
} from "@/api/policy/policy";

export default {
  name: "account-list",
  components: {
    vueUpload,
    FileUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否为编辑
      edit: false,
      // 分配销售
      openAllocation: false,
      // 选择的销售id
      saleId: undefined,
      // 销售列表
      saleList: [],
      // 分配销售确定loading
      confirmLoadingSale: false,
      openFp: false,
      personVisible: false,
      personData: null,
      personList: [],
      formFp: {},
      fileType: [".xls", ".xlsx", ".csv"],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: undefined,
        advertiserName: undefined,
        productName: undefined,
        releaseType: undefined,
        rebateType: undefined,
        saleName: undefined,
        createTime: undefined,
      },
      //页面所有的下拉数据
      queryParamsList: {
        ksList: [], //快手昵称下拉
        accountList: [], //账户列表下拉
        releaseTypeList: [
          { value: "1", label: "磁力金牛-投流" },
          { value: "2", label: "磁力金牛-操盘" },
        ], //账户类型下拉
        rebateTypeList: [
          { value: "0", label: "返现" },
          { value: "1", label: "返货" },
          { value: "2", label: "返现+返货" },
        ],
        saleLeaderList: [], //销售列表
        allPersonList: [], //所有人
        companySubjectInfoList: [],
        policyCustomerList: [],
      },
      // 表单参数
      form: {
        promotionType: "1",
        advancePay: "1",
        operateSettlementType: "1",
        releaseType: ["1"],
      },
      fileList: [],
      //获取销售线索文件流
      dateFile: null,
      //销售线索上传loading
      confirmLoading: false,
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 30,
            message: "公司名称最多30个字,最少1个字",
            trigger: "blur",
          },
        ],
        advertiserName: [
          { required: true, message: "广告主名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 30,
            message: "广告主名称最多30个字,最少1个字",
            trigger: "blur",
          },
        ],
        productName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 30,
            message: "产品名称最多30个字,最少1个字",
            trigger: "blur",
          },
        ],
        releaseType: [
          { required: true, message: "投放类型不能为空", trigger: "change" },
        ],
        saleId: [
          { required: true, message: "投放类型不能为空", trigger: "change" },
        ],
        companySubjectId: [
          { required: true, message: "我司主体不能为空", trigger: "change" },
        ],
        advancePay: [{ required: true, message: "", trigger: "change" }],
        promotionType: [{ required: true, message: "", trigger: "change" }],
        rebateType: [
          { required: true, message: "返点类型不能为空", trigger: "change" },
        ],
        accountingPeriod: [
          { required: true, message: "账期不能为空", trigger: "blur" },
        ],
        rebateRate: [
          { required: true, message: "返点比例不能为空", trigger: "blur" },
        ],
        operateSettlementType: [
          {
            required: true,
            message: "操盘结算类型不能为空",
            trigger: "change",
          },
        ],
        createTime: [
          {
            required: true,
            message: "起止时间不能为空",
            trigger: "change",
          },
        ],
      },

      // 分配销售展示列表
      ksInfo: null,
      userId: null,
    };
  },
  created() {
    this.userId = this.$store.getters.userId;
    this.getList();
    this.getOperatorList();
    this.getSelectCompanySubjectInfoList();
    this.getSelectPolicyCustomerList();
  },
  watch: {
    "form.companyName": function (n, o) {
      if (n) {
        this.$set(this.form, "advertiserName", n.split("@")[1]);
      } else {
        this.$set(this.form, "advertiserName", undefined);
      }
    },
  },
  filters: {
    rebateTypeChinese(status) {
      const statusMap = {
        0: "返现",
        1: "返货",
        2: "返现+返货",
      };
      return statusMap[status];
    },
    releaseTypeChinese(status) {
      const statusMap = {
        1: "磁力金牛-投流",
        2: "磁力金牛-操盘",
      };
      return statusMap[status];
    },
  },
  methods: {
    getSelectPolicyCustomerList() {
      getSelectPolicyCustomerList()
        .then((res) => {
          this.queryParamsList.policyCustomerList = res.data.map((item) => {
            return {
              value:
                item.ksId + "@" + item.advertiserName + "@" + item.companyName,
              label: item.companyName,
            };
          });
        })
        .catch(() => {});
    },
    // 获取公司主体列表
    getSelectCompanySubjectInfoList() {
      getSelectCompanySubjectInfoList()
        .then((res) => {
          this.queryParamsList.companySubjectInfoList = res.data.map((item) => {
            return {
              value: item.id,
              label: item.companySubjectName,
            };
          });
        })
        .catch(() => {});
    },
    // 获取销售列表
    getOperatorList() {
      getQueryRolesList({ type: 2 })
        .then((res) => {
          this.queryParamsList.saleLeaderList = res.data.map((item) => {
            return {
              value: item.userId,
              label: item.nickName,
            };
          });
        })
        .catch((error) => {});
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      var params = { ...this.queryParams, createUserId: this.userId };
      if (params.createTime) {
        params.createTimeStart = params.createTime[0];
        params.createTimeEnd = params.createTime[1];
      }

      getSelectPolicyList(params).then((response) => {
        this.typeList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.ids = null;
      this.open = true;
      this.edit = false;
      this.title = "新增政策";
    },
    /** 新增政策确定按钮操作 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(this.form);
          this.confirmLoading = true;
          let formData = { ...this.form };
          formData.policyStartDate = formData.createTime[0];
          formData.policyEndDate = formData.createTime[1];
          formData.createUserId = this.userId;
          formData.releaseType = formData.releaseType.join();
          formData.advertiserName = this.form.companyName.split("@")[1];
          formData.ksId = +this.form.companyName.split("@")[0];
          formData.companyName = this.form.companyName.split("@")[2];
          if (!this.ids) {
          } else {
            formData.id = this.ids;
          }
          let { createTime, ...params } = formData;
          addSaleFinancePolicyInfo(params)
            .then((res) => {
              this.confirmLoading = false;
              this.$message.success(res.msg);
              this.ids = null;
              this.cancel();
              this.handleQuery();
            })
            .catch((err) => {
              this.confirmLoading = false;
            });
        }
      });
    },
    /** 新增政策取消按钮操作 */
    cancel() {
      this.form = {
        promotionType: "1",
        advancePay: "1",
        operateSettlementType: "1",
        releaseType: ["1"],
      };
      this.resetForm("form");
      this.open = false;
    },
    handleDelete(row) {},
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/dict/type/export",
        {
          ...this.queryParams,
        },
        `type_${new Date().getTime()}.xlsx`
      );
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.entire-line {
  width: 100%;
  ::v-deep .el-form-item__content {
    width: 60%;
  }
}
::v-deep .el-form-item__label {
  white-space: nowrap;
}
::v-deep .el-input-number__decrease {
  display: none;
}
::v-deep .el-input-number__increase {
  display: none;
}
::v-deep .el-textarea__inner {
  padding: 5px 15px 40px 15px;
}
::v-deep .el-textarea .el-input__count {
  color: #909399;
  background: #ffffff;
  position: absolute;
  font-size: 12px;
  bottom: 1px;
  right: 20px;
  width: 90%;
  text-align: right;
}
</style>
