<style scoped lang="scss">
::v-deep a {
  color: #409eff;
  display: block;
}
p {
  text-align: left;
  margin: 0;
}
</style>
<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="userId">
        <el-select
          filterable
          v-model="queryParams.userId"
          placeholder="人员名称"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in queryParamsList.personList"
            :key="item.userId"
            :label="item.nickMame"
            :value="item.userId"
          >
            <div style="display: flex">
              <span
                style="
                  width: 10px;
                  height: 10px;
                  border-radius: 50%;
                  background-color: green;
                  position: relative;
                  top: 12px;
                  margin-right: 5px;
                "
                :style="
                  item.status == '0'
                    ? 'backgroundColor:green'
                    : 'backgroundColor:red'
                "
                >{{ item.value }}</span
              ><span>{{ item.nickMame }}</span>
            </div>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="itemTitle">
        <el-input
          v-model="queryParams.itemTitle"
          placeholder="请输入商品名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品Id" prop="itemId">
        <el-input
          v-model.number="queryParams.itemId"
          placeholder="请输入商品Id"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认领状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择认领状态"
          clearable
          style="width: 240px"
        >
          <el-option label="已认领" :value="2" />
          <el-option label="未认领" :value="1" />
        </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>
    <p
      style="
        width: 100%;
        height: 40px;
        background: rgb(241, 247, 254);
        border: 1px solid rgb(227, 237, 250);
        line-height: 40px;
        padding: 0 30px;
        font-size: 14px;
        display: flex;
        margin-bottom:15px
      "
    >
      <span>
        商品总数：<a style="display: inline-block" @click="search()">{{
          itemSummary.itemTotal
        }}</a></span
      >
      <span style="margin: 0 20px"
        >已认领商品数：<a style="display: inline-block" @click="search('2')">{{
          itemSummary.claimCount
        }}</a></span
      >
      <span>
        未认领商品数：<a style="display: inline-block" @click="search('1')">{{
          itemSummary.unClaimCount
        }}</a></span
      >
    </p>
    <!-- this.itemSummary = {
          itemTotal: res.itemTotal,
          claimCount: res.claimCount,
          unClaimCount: res.unClaimCount,
        }; -->
    <el-table
      v-loading="loading"
      :data="typeList"
      @selection-change="handleSelectionChange"
      ref="multipleTable"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column
        label="商品图片"
        align="center"
        prop="imageUrl"
        width="150"
      >
        <template slot-scope="scope">
          <img
            :src="scope.row.imageUrl"
            alt=""
            style="width: 100px; height: auto"
          />
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="itemTitle">
      </el-table-column> -->
      <el-table-column
        label="商品名称"
        align="center"
        prop="itemTitle"
        width="300"
      >
        <template slot-scope="scope">
          <div style="display: flex; width: 100%">
            <div style="width: 40%">
              <img
                :src="scope.row.imageUrl"
                alt=""
                style="width: 70px; height: auto"
              />
            </div>
            <div style="width: 60%">
              <a
                style="
                  text-overflow: ellipsis;
                  white-space: nowrap;
                  overflow: hidden;
                  width: 100%;
                "
                :title="scope.row.itemTitle"
              >
                {{ scope.row.itemTitle }}
              </a>
              <p>商品Id： {{ scope.row.itemId }}</p>
              <p>
                佣金率：
                {{
                  scope.row.itemCommissionRate
                    ? scope.row.itemCommissionRate / 10 + "%"
                    : "-"
                }}
              </p>
            </div>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="商品Id" align="center" prop="itemId" /> -->
      <el-table-column label="价格" align="center" prop="reservePrice">
        <template slot-scope="scope">
          {{ (scope.row.reservePrice / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column
        label="服务费率"
        align="center"
        prop="regimentalPromotionRate"
      >
        <template slot-scope="scope">
          {{ scope.row.regimentalPromotionRate / 10 + "%" }}
        </template>
      </el-table-column>

      <el-table-column label="认领人员" align="center" prop="userName">
        <template slot-scope="scope">
          {{ !!scope.row.userName ? scope.row.userName : "-" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" prop="action">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            :disabled="!!scope.row.userName"
            >认领</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            :disabled="scope.row.disabled"
            >解绑</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="dialogFormVisible"
      width="400px"
    >
      <el-form :model="form">
        <el-form-item label="认领人员">
          <el-select
            filterable
            v-model="form.userId"
            placeholder="人员名称"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="item in queryParamsList.personList"
              :key="item.userId"
              :label="item.nickMame"
              :value="item.userId + '@' + item.nickMame"
            >
              <div style="display: flex">
                <span
                  style="
                    width: 10px;
                    height: 10px;
                    border-radius: 50%;
                    background-color: green;
                    position: relative;
                    top: 12px;
                    margin-right: 5px;
                  "
                  :style="
                    item.status == '0'
                      ? 'backgroundColor:green'
                      : 'backgroundColor:red'
                  "
                  >{{ item.value }}</span
                ><span>{{ item.nickMame }}</span>
              </div>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogFormVisible = false;
            form.userId = undefined;
          "
          >取 消</el-button
        >
        <el-button type="primary" @click="okBd" :loading="logLoading"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
  refreshCache,
} from "@/api/system/dict/type";
import {
  getBindUserItemList,
  getUnbindUser,
  getBindUser,
  getSupplyChainUserList,
  getSupplyChainCourtshipList,
  getItemBindSummary,
} from "@/api/supplyChain/supplyChain";
export default {
  name: "Claim",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      dialogFormVisible: false,
      itemId: undefined,
      logPage: {
        pageNum: 1,
        pageSize: 10,
      },
      logData: [],
      logTotal: 0,
      logLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分配销售
      openAllocation: false,
      // 选择的销售id
      saleId: undefined,
      // 获取用户id
      userId: undefined,
      remark: [],
      name: undefined,
      // 销售列表
      saleList: [],
      // 分配销售确定loading
      confirmLoadingSale: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemId: undefined,
        itemTitle: undefined,
        userId: undefined,
        status: undefined,
        orderStartDate: undefined,
        orderEndDate: undefined,
      },
      uploadDate: [new Date(), new Date()],
      //查询参数所用下拉数据
      queryParamsList: {
        personList: [],
      },
      form: {
        userId: "",
      },
      itemSummary: {
        itemTotal: 0,
        claimCount: 0,
        unClaimCount: 0,
      },
    };
  },
  created() {
    this.userId = this.$store.getters.userId;
    this.remark = this.$store.getters.roles;
    this.name = this.$store.getters.name;
    let date = new Date();
    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}`, `${y}-${MM}-${dd}`];
  },
  mounted() {
    this.$nextTick(() => {
      this.getPersonList();
      this.getItemBindSummary();
      this.getList();
    });
  },
  computed: {
    isDisabled() {
      if (
        this.remark.includes("admin") ||
        this.remark.includes("supplyChainAdmin")
      ) {
        return false;
      } else {
        return true;
      }
    },
  },
  methods: {
    search(type) {
      if (type) {
        this.queryParams.status = +type;
      } else {
        this.queryParams.status = undefined;
      }

      this.handleQuery();
    },
    getItemBindSummary() {
      getItemBindSummary().then((res) => {
        this.itemSummary = {
          itemTotal: res.itemTotal,
          claimCount: res.claimCount,
          unClaimCount: res.unClaimCount,
        };
      });
    },
    handleDelete(item) {
      this.$confirm("确定解绑吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          getUnbindUser({
            itemId: item.itemId,
            userId: item.userId,
            nikeName: item.userName,
          }).then((res) => {
            if (res.code == 0) {
              this.$message.success(res.message);
              this.getList();
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {});
    },

    handleUpdate(item) {
      if (this.remark.includes("admin")) {
        this.dialogFormVisible = true;

        this.itemId = item.itemId;
      } else {
        getBindUser({
          itemId: item.itemId,
          userId: this.userId,
          nikeName: this.name,
        }).then((res) => {
          this.logLoading = false;
          if (res.code == 0) {
            this.dialogFormVisible = false;
            this.$message.success(res.message);
            this.getList();
          } else {
            this.$message.error(res.message);
          }
        });
      }
    },
    okBd() {
      if (!!this.form.userId) {
        this.logLoading = true;
        getBindUser({
          itemId: this.itemId,
          userId: this.form.userId.split("@")[0],
          nikeName: this.form.userId.split("@")[1],
        }).then((res) => {
          this.logLoading = false;
          if (res.code == 0) {
            this.dialogFormVisible = false;
            this.$message.success(res.message);
            this.getList();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.error("请选择人员");
      }
    },
    getPersonList() {
      getSupplyChainCourtshipList().then((response) => {
        this.queryParamsList.personList = response.rows;
      });
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      getBindUserItemList(
        this.addDateRange(
          this.queryParams,
          this.uploadDate,
          "orderStartDate",
          "orderEndDate"
        )
      ).then((response) => {
        if (response.rows.length == 0 && response.total > 0) {
          this.queryParams.pageNum = this.queryParams.pageNum - 1;
          this.getList();
        } else {
          this.typeList = response.rows.map((item) => {
            return {
              ...item,
              disabled: this.isDisabled
                ? item.userId != this.userId
                : !item.userId
                ? true
                : false,
            };
          });
          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 = [];
      let date = new Date();
      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}`, `${y}-${MM}-${dd}`];
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
    },
  },
};
</script>
