<!--
 * @Description: 新增/编辑弹框
 * @Author: Sugar.
 * @Date: 2023-11-24 13:55:00
 * @LastEditors: Sugar.
 * @LastEditTime: 2023-11-24 13:55:00
 * @FilePath: \cattle_webui\src\views\team\ticketMr\dialog\dumplingEdit.vue
 * @Copyright: Copyright (c) 2016~2023 by Sugar., All Rights Reserved.
-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="700px"
    append-to-body
    :close-on-click-modal="false"
    @close="cancel"
  >
    <div class="dialog" style="padding: 0">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >添加</el-button>
        </el-col>
      </el-row>
      <el-table ref="tables" v-loading="tabLoading" :data="dataList" border>
        <el-table-column label="序号" align="center" type="index" width="50"></el-table-column>
        <el-table-column label="团队名称" align="center" prop="name" />
        <el-table-column label="团队类型" align="center" prop="type">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.team_type" :value="scope.row.type"/>
          </template>
        </el-table-column>
        <el-table-column label="负责人" align="center" prop="contact" />
        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(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"
      />
    </div>

    <el-dialog
      title="团队选择"
      :visible.sync="selectionOpen"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
      @close="selectionCancel"
    >
      <div class="dialog" style="padding: 0">
        <el-table ref="tables" v-loading="selectionTabLoading" :data="selectionDataList" border @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" type="index" width="50"></el-table-column>
          <el-table-column label="团队名称" align="center" prop="name" />
          <el-table-column label="团队类型" align="center" prop="type">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.team_type" :value="scope.row.type"/>
            </template>
          </el-table-column>
          <el-table-column label="负责人" align="center" prop="contact" />
        </el-table>
        <pagination
          v-show="selectionTabTotal>0"
          :total="selectionTabTotal"
          :page.sync="selectQueryParams.pageNum"
          :limit.sync="selectQueryParams.pageSize"
          @pagination="getSelectList"
        />
      </div>
      <span slot="footer" class="dialog-footer">
      <el-button @click="selectionCancel">取消</el-button>
      <el-button
        type="primary"
        @click="submitForm"
        v-loading.fullscreen.lock="loading"
        element-loading-text="提交中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <span v-if="loading">提交中...</span>
        <span v-else>确认</span>
      </el-button>
    </span>
    </el-dialog>

    <span slot="footer" class="dialog-footer">
      <el-button  type="primary"  @click="cancel">确认</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { bindTeam, queryBindTeam, unBindTeam } from '@/api/team/ticketMr'
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";
import { updateStatus } from '@/api/team/teamMr'
export default {
  name: "dumplingEdit",
  components: {
    Editor,
  },
  props: {
    dict: {
      type: Object,
      default: () => [],
    },
  },
  data() {
    return {
      title: "团队绑定",
      model: "EDIT",
      open: false,
      loading: false,
      tabLoading: false,
      selectionTabLoading: false,
      dataList: [],
      selectionDataList: [],
      total: 0,
      selectionTabTotal: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: 1,
      },
      selectQueryParams: {
        pageNum: 1,
        pageSize: 10,
        type: 2,
      },
      selectList: [],
      selectionOpen: false,
      onwObj: {},
    };
  },
  methods: {
    /**
     * 打开弹框
     * @date 2023-11-22
     * @param {any} obj
     * @returns {any}
     */
    openDialog(title, obj) {
      this.open = true;
      this.title = "团队绑定";
      this.onwObj = obj;
      this.getList(obj);
    },
    handleAdd() {
      this.getSelectList();
      this.selectionOpen = true
    },
    /** 查询列表 */
    getList(obj) {
      this.tabLoading = true;
      this.queryParams.configId = this.onwObj.id
      queryBindTeam(this.queryParams)
        .then(response => {
            this.dataList = response.data.rows;
            this.total = response.data.total;
            this.tabLoading = false;
          }
        ).catch(() => {
        this.tabLoading = false;
      });
    },
    /** 查询列表 */
    getSelectList(obj) {
      this.selectionTabLoading = true;
      queryBindTeam(this.selectQueryParams)
        .then(response => {
            this.selectionDataList = response.data.rows;
            this.selectionTabTotal = response.data.total;
            this.selectionTabLoading = false;
          }
        ).catch(() => {
        this.selectionTabLoading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectList = selection;
    },
    /**
     * 保存
     * @date 2023-11-22
     * @returns {any}
     */
    async submitForm() {
      try {
        if(this.selectList.length <= 0) {
          this.$message.error("请勾选团队!");
          return false
        }
        let postList = [];
        this.selectList.forEach(item => {
          postList.push({
            "teamId": item.id,
            "configId": this.onwObj.id
          })
        })
        this.loading = true;
        const { code } = await bindTeam(postList);
        if (code === 200) {
          this.$message.success("操作成功!");
          this.getList(this.onwObj);
          this.selectionOpen = false;
        }
      } catch (error) {
      } finally {
        this.loading = false;
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认移除数据团队名称为"' + row.name + '"的数据项？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        unBindTeam({ teamId: row.id, configId: this.onwObj.id }).then((res) => {
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList(this.onwObj);
          }
        });
      }).catch(() => {});
    },
    /**
     * 重置
     * @date 2023-11-22
     * @returns {any}
     */
    reset() {

    },
    /**
     * 关闭弹框
     * @date 2023-11-22
     * @returns {any}
     */
    cancel() {
      this.open = false;
    },
    selectionCancel() {
      this.selectionOpen = false;
    }
  },
};
</script>

<style lang="scss" scoped>
.dialog {
  padding: 0 30px;
  max-height: 65vh;
  overflow-y: auto;
}
.dialog {
  padding: 0 30px;
  .upload-btn {
    width: 100px;
    height: 100px;
    background-color: #fbfdff;
    border: dashed 1px #c0ccda;
    border-radius: 5px;
    i {
      font-size: 30px;
      margin-top: 20px;
    }
    &-text {
      margin-top: -10px;
    }
  }
  .avatar {
    cursor: pointer;
  }
}
</style>
