<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="销售" prop="nickName">
        <el-select v-model="queryParams.companyUserId" remote placeholder="请选择" filterable clearable  style="width: 100%;" @keyup.enter.native="handleQuery">
          <el-option
            v-for="dict in companyUserList"
            :key="`${dict.nickName} - ${dict.userName}`"
            :label="`${dict.nickName} - ${dict.userName}`"
            :value="dict.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类别" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类别" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="sop编号" prop="sopId">
        <el-input
          v-model="queryParams.sopId"
          placeholder="请输入sopid"
          clearable
          size="small"
          @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>

  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
	<el-tabs type="card" v-model="actName" @tab-click="handleClickX">
	  <el-tab-pane v-for="(item,index) in statusOptions" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
	</el-tabs>
    <el-table border v-loading="loading" :data="QwWorkTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="客户昵称" align="center" prop="name" />-->
<!--      <el-table-column label="企微账号" align="center" prop="qwUserName" />-->
      <el-table-column label="销售名称" align="center" prop="companyUserName" />
      <el-table-column label="状态" align="center" prop="status">
		 <template slot-scope="scope">
		   <dict-tag :options="statusOptions" :value="scope.row.status"/>
		 </template>
      </el-table-column>
      <el-table-column label="类别" align="center" prop="typeText">
        <template slot-scope="scope">
          <dict-tag :options="typeOptions" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="分值" align="center" prop="score">
        <template slot-scope="scope">
          <span :style="getScoreStyle(scope.row.score)">{{ scope.row.score }}</span>
        </template>
      </el-table-column>
      <el-table-column label="sop编号" align="center" prop="sopId" />
      <el-table-column label="公司id" align="center" prop="companyName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status==0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备注" prop="remark">
          <el-input  v-model="form.remark" placeholder="请输入备注"  type="textarea" :rows="3"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQwWorkTask, getQwWorkTask, delQwWorkTask, addQwWorkTask, updateQwWorkTask, exportQwWorkTask } from "@/api/qw/QwWorkTask";
import {getMyQwUserList, getMyQwCompanyList, handleInputAuthAppKey, updateUser} from "@/api/qw/user";
import {getUserList} from "@/api/company/companyUser";
export default {
  name: "QwWorkTask",
  data() {
    return {
	  actName:"0",
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
	  myQwUserList:[],
      companyUserList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企微任务看板表格数据
      QwWorkTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态 0 待处理 1 已处理 3 过期字典
      statusOptions: [],
      // 类别 1先导 2 课程 3 大小转 4 转人工字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        extId: null,
        status: 0,
        qwUserId: null,
        type: null,
        title: null,
        score: null,
        sopId: null,
        companyId: null,
        companyUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {

    this.getDicts("sys_qw_work_task_status").then(response => {
      this.statusOptions = response.data;
    });

    this.getDicts("sys_qw_work_task_type").then(response => {
      this.typeOptions = response.data;
    });

    getUserList().then(res=>{
      if(res.code === 200) {
        this.companyUserList = res.data
      }
    })
    this.handleGetMyQwUserList();

    this.getList();
  },
  methods: {
	getScoreStyle(score) {
	let backgroundColor = '';
	  if (score >= 15) {
		backgroundColor = '#ff4d4f';    // 红色
	  } else if (score >= 9) {
		backgroundColor = '#ff7d45';    // 橘红
	  } else if (score >= 4) {
		backgroundColor = '#ffec3d';    // 黄色
	  } else {
		backgroundColor = '#ffffff';    // 白色
	  }
	  return {
		'background-color': backgroundColor,
		'padding': '5px 10px',
		'border-radius': '4px'
	  };
	},
    /** 查询企微任务看板列表 */
    getList() {
      this.loading = true;
      listQwWorkTask(this.queryParams).then(response => {
        this.QwWorkTaskList = response.rows;
        this.total = response.total;
      }).finally(() => {
        this.loading = false;
      });
    },
	handleClickX(tab, event) {
	  this.queryParams.status=tab.name;
	  this.handleQuery();
	},
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        extId: null,
        qwUserId: null,
        status: 0,
        type: null,
        title: null,
        remark: null,
        score: null,
        sopId: null,
        companyId: null,
        companyUserId: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
	handleGetMyQwUserList(){

	  getMyQwUserList().then(response => {
	    this.myQwUserList = response.data;
	    if(this.myQwUserList!=null){
	      this.queryParams.qwUserId=this.myQwUserList[0].dictValue
	      this.queryParams.corpId=this.myQwUserList[0].corpId
	      this.getList();
	    }
	  });

	},
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企微任务看板";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
        this.form = row;
        this.open = true;
        this.title = "处理任务";

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQwWorkTask(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQwWorkTask(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除企微任务看板编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delQwWorkTask(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企微任务看板数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportQwWorkTask(queryParams);
        }).then(response => {
          this.download(response.msg);

        }).catch(() => {})
        .finally(()=>{
          this.exportLoading = false;
        })
    }
  }
};
</script>
