<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="账户ID" prop="accountId">
        <el-input
          v-model="queryParams.accountId"
          placeholder="请输入账户ID"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="快手ID" prop="ksId">
        <el-input
          v-model="queryParams.ksId"
          placeholder="请输入快手ID"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="账户名称" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          placeholder="请输入账户名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>

      <el-form-item label="认领状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="选择认领状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in queryParamsList.statusList"
            :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-button
          icon="el-icon-refresh"
          size="mini"
          :loading="syncAccountsLoading"
          @click="syncAccounts"
          >同步账户</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="typeList"
      @selection-change="handleSelectionChange"
      ref="multipleTable"
    >
      <el-table-column
        label="账户ID"
        align="center"
        prop="accountId"
        width="200px"
        fixed="left"
      />
      <el-table-column label="账户名称" align="center" prop="accountName" width="200px" />
      <el-table-column label="媒体" align="center" prop="type">
        <template slot-scope="scope">
          <div v-if="scope.row.type">
            {{ scope.row.type | mediaName }}
          </div>
          <div v-else>-</div>
        </template>
      </el-table-column>
      <el-table-column
        label="快手昵称(快手ID)"
        align="center"
        prop="ksName"
        width="200px"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.ksName">{{ scope.row.ksName }}({{ scope.row.ksId }})</div>
          <div v-else>-</div>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="accountCreateTime"
        width="200px"
      />
      <el-table-column label="产品名称" align="center" prop="productName" width="200px" />
      <el-table-column label="运营" align="center" prop="operatorName"> </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :disabled="scope.row.operatorName != '-'"
            @click="getAccount(scope.row)"
            >认领</el-button
          >
          <el-button
            size="mini"
            type="text"
            :disabled="
              $store.getters.roles[0] == 'admin' ? false : userId != scope.row.operatorId
            "
            @click="cancelAccount(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"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="媒体类型" prop="mediaCode">
          <el-radio-group v-model="form.mediaCode">
            <el-radio-button label="JN">磁力金牛</el-radio-button>
            <el-radio-button label="QC" disabled>千川</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="账户类型" prop="accountType">
          <el-radio-group v-model="form.accountType">
            <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="projectId">
          <el-select
            ref="optionAdvertiser"
            v-model="form.projectId"
            placeholder="选择项目"
            clearable
            style="width: 240px"
            filterable
          >
            <el-option
              v-for="item in formList.projectList"
              :key="item.id"
              :label="item.projectName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </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 {
  getAccountList,
  getMyProjects,
  getUnboundAccountList,
  claimAccount,
  deleteClaimAccount,
  getAccountByProjectId,
  syncAccounts,
} from "@/api/accountManagement/accountManagement";
import { listUser } from "@/api/system/user";
import { ifError } from "assert";
export default {
  name: "AccountClaim",
  data() {
    return {
      dialogTableVisible: false,
      syncAccountsLoading: false,
      logPage: {
        pageNum: 1,
        pageSize: 10,
      },
      logData: [],
      logTotal: 0,
      logLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      accountId: undefined,
      // 编辑所用id
      updateId: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分配销售
      openAllocation: false,
      // 选择的销售id
      saleId: undefined,
      // 获取用户id
      userId: undefined,
      // 销售列表
      saleList: [],
      // 分配销售确定loading
      confirmLoadingSale: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accountId: undefined,
        ksId: undefined,
        accountName: undefined,
        status: undefined,
      },
      //查询参数所用下拉数据
      queryParamsList: {
        statusList: [
          { value: 1, label: "认领" },
          { value: 2, label: "未认领" },
        ],
      },
      // 表单参数
      form: {
        ocpxActionTypeList: [],
      },
      //上传报表下拉数据
      formList: {
        personList: [], //新增下拉人员列表
        projectList: [], //项目列表
      },
      fileList: [],
      //获取销售线索文件流
      dateFile: null,
      //销售线索上传loading
      confirmLoading: false,
      // 表单校验
      rules: {
        projectId: [{ required: true, message: "项目必选", trigger: "change" }],
      },
    };
  },
  created() {
    this.userId = this.$store.getters.userId;
  },
  filters: {
    ocpxActionTypeName(status) {
      const statusMap = {
        72: "涨粉",
        395: "订单支付",
        192: "直播推广 ROI",
      };
      return statusMap[status];
    },
    mediaName(status) {
      const statusMap = {
        JN: "磁力金牛",
      };
      return statusMap[status];
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.getList();
    });
  },
  methods: {
    syncAccounts() {
      this.syncAccountsLoading = true;
      syncAccounts()
        .then((res) => {
          this.syncAccountsLoading = false;
          this.$message.success("同步成功");
          this.resetQuery();
        })
        .catch(() => {
          this.syncAccountsLoading = false;
        });
    },
    listUser() {
      listUser({ pageNum: 1, pageSize: 10000 }).then((res) => {
        this.formList.personList = res.rows;
      });
    },
    getProject() {
      getMyProjects({ pageNum: 1, pageSize: 10000, userId: this.userId }).then((res) => {
        this.formList.projectList = res.rows;
      });
    },
    /** 复选框禁用 */
    selectable(row, index) {
      return true;
    },

    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      getUnboundAccountList({ ...this.queryParams, userId: this.userId }).then(
        (response) => {
          this.typeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.confirmLoading = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.dateFile = null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1;
      // this.handleQuery();
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
    },

    getAccount(item) {
      //   getAccountByProjectId({ projectId: item.id }).then((res) => {});
      this.open = true;
      this.title = "账户绑定";
      this.getProject();
      this.accountId = item.accountId;
      this.reset();
      this.$nextTick(() => {
        this.$set(this.form, "mediaCode", "JN");
        this.$set(this.form, "accountType", 1);
      });
    },

    cancelAccount(item) {
      this.$modal
        .confirm(`请确认是否取消认领？`)
        .then(() => {
          var params = {};
          params.accountId = item.accountId;
          params.userId = this.$store.getters.userId;
          deleteClaimAccount(params)
            .then((res) => {
              this.$modal.msgSuccess("取消认领成功");
              this.resetQuery();
            })
            .catch(() => {
              this.$message.error("失败");
              this.loading = false;
            });
        })
        .catch(() => {});
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.confirmLoading = true;
          var params = { ...this.form };
          params.accountId = this.accountId;
          params.userId = this.userId;
          //   params.advertiserId = 2;

          claimAccount({ ...params }).then((res) => {
            this.open = false;
            this.confirmLoading = false;
            this.handleQuery();
          });
        }
      });
    },
  },
};
</script>
