<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="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </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-table
      v-loading="loading"
      :data="typeList"
      @selection-change="handleSelectionChange"
      ref="multipleTable"
    >
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="广告主名称" align="center" prop="advertiserName" />
      <el-table-column label="媒体" align="center" prop="mediaCode">
        <template slot-scope="scope">
          <div v-if="scope.row.mediaCode">
            {{ scope.row.mediaCode | mediaName }}
          </div>
          <div v-else>-</div>
        </template>
      </el-table-column>
      <el-table-column label="销售" align="center" prop="saleName" />
      <el-table-column label="运营" align="center" prop="operatorName" />
      <el-table-column label="设计" align="center" prop="designResponsibleName" />
      <el-table-column
        label="转化目标"
        align="center"
        prop="ocpxActionType"
        width="200px"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.ocpxActionType">
            <span
              v-for="(item, index) in JSON.parse(scope.row.ocpxActionType)"
              :key="index"
            >
              {{ item.ocpxName + "(" + (item.maxValue / 1000).toFixed(2) + ")" }}
              <br />
            </span>
          </div>
          <div v-else>-</div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="300px"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="getAccount(scope.row, 1)"
            >查看账户</el-button
          >
          <el-button size="mini" type="text" @click="getAccount(scope.row, 2)"
            >查看人员</el-button
          >
          <el-button size="mini" type="text" @click="getAccount(scope.row, 3)"
            >新增人员</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="查看账户" :visible.sync="lookAccount" width="800px" append-to-body>
      <el-table
        v-loading="lookAccountLoading"
        :data="accountData.lookAccountList"
        ref="lookAccount"
      >
        <el-table-column label="账户ID" align="center" prop="accountId" width="200px" />
        <el-table-column
          label="账户名称"
          align="center"
          prop="accountName"
          width="200px"
        />
        <el-table-column label="所属人" align="center" prop="operatorName">
        </el-table-column>
        <el-table-column label="绑定日期" align="center" prop="createTime">
        </el-table-column>
      </el-table>

      <pagination
        v-show="accountData.total > 0"
        :total="accountData.total"
        :page.sync="accountData.pageNum"
        :limit.sync="accountData.pageSize"
        @pagination="changeAccount"
      />
    </el-dialog>

    <el-dialog title="查看人员" :visible.sync="lookPerson" width="800px" append-to-body>
      <el-tag
        v-for="tag in personList"
        :key="tag.userId"
        closable
        @close="handleClose(tag)"
        style="margin: 0 10px 10px 10px"
      >
        {{ tag.userName + " | " + (tag.roleName ? tag.roleName : "-") }}
      </el-tag>
    </el-dialog>

    <!-- 添加或修改参数配置对话框 -->
    <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="userIds">
          <el-select
            ref="optionOperator"
            v-model="form.userIds"
            placeholder="选择人员"
            multiple
            clearable
            filterable
            style="width: 240px"
          >
            <el-option
              v-for="item in formList.personList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            />
          </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 {
  getMyProjects,
  getAccountByProjectId,
  queryProjectUsers,
  deleteProjectUsers,
  addProjectUsers,
} from "@/api/accountManagement/accountManagement";
import { listUser } from "@/api/system/user";
export default {
  name: "MyProject",
  data() {
    return {
      dialogTableVisible: false,
      logPage: {
        pageNum: 1,
        pageSize: 10,
      },
      logData: [],
      logTotal: 0,
      logLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      projectId: undefined,
      projectName: undefined,
      // 编辑所用id
      updateId: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      lookAccount: false,
      lookAccountLoading: false,

      lookPerson: false,
      personList: [],
      accountData: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        lookAccountList: [],
      },
      // 分配销售
      openAllocation: false,
      // 选择的销售id
      saleId: undefined,
      // 获取用户id
      userId: undefined,
      // 销售列表
      saleList: [],
      // 分配销售确定loading
      confirmLoadingSale: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: undefined,
      },
      //查询参数所用下拉数据
      queryParamsList: {},
      // 表单参数
      form: {
        ocpxActionTypeList: [],
      },
      //上传报表下拉数据
      formList: {
        personList: [], //新增下拉人员列表
        ocpxActionList: [], //优化目标列表
        advertiserList: [], //广告主列表
      },
      fileList: [],
      //获取销售线索文件流
      dateFile: null,
      //销售线索上传loading
      confirmLoading: false,
      // 表单校验
      rules: {
        userIds: [{ 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: {
    listUser() {
      listUser({ pageNum: 1, pageSize: 10000 }).then((res) => {
        this.formList.personList = res.rows;
      });
    },
    /** 复选框禁用 */
    selectable(row, index) {
      return true;
    },

    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      getMyProjects({ ...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, type) {
      this.projectId = item.id;
      this.projectName = item.projectName;
      if (type == 1) {
        this.lookAccount = true;
        this.accountData.lookAccountList = [];
        this.lookAccountLoading = true;
        getAccountByProjectId({
          projectId: item.id,
          pageNum: this.accountData.pageNum,
          pageSize: 10,
        }).then((res) => {
          this.accountData.lookAccountList = res.rows;
          this.accountData.total = res.total;
          this.lookAccountLoading = false;
        });
      } else if (type == 2) {
        this.lookPerson = true;
        this.getProjectUsers();
      } else if (type == 3) {
        this.open = true;
        this.title = "新增人员";
        this.listUser();
        this.reset();
        this.$nextTick(() => {
          this.$refs["form"].clearValidate("userIds");
        });
      }
    },
    getProjectUsers() {
      queryProjectUsers({
        projectId: this.projectId,
      }).then((res) => {
        this.personList = res.result;
      });
    },
    handleClose(item) {
      console.log(item);
      deleteProjectUsers({ userId: item.userId, projectId: item.projectId }).then(
        (res) => {
          this.$message.success("删除成功");
          this.getProjectUsers();
        }
      );
    },
    changeAccount() {
      this.accountData.lookAccountList = [];
      this.lookAccountLoading = true;
      getAccountByProjectId({
        projectId: this.projectId,
        pageNum: this.accountData.pageNum,
        pageSize: 10,
      }).then((res) => {
        this.accountData.lookAccountList = res.rows;
        this.accountData.total = res.total;
        this.lookAccountLoading = false;
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.confirmLoading = true;
          var params = {};
          params.projectId = this.projectId;
          params.projectName = this.projectName;
          params.userIds = JSON.stringify(this.form.userIds);

          addProjectUsers(params)
            .then((res) => {
              this.open = false;
              this.confirmLoading = false;
              this.resetQuery();
            })
            .catch((err) => {
              this.confirmLoading = false;
            });
        }
      });
    },
  },
};
</script>
