<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="销售公司" prop="companyId">-->
<!--        <el-select filterable v-model="queryParams.companyId" clearable placeholder="请选择公司名" size="small">-->
<!--          <el-option-->
<!--            v-for="item in companys"-->
<!--            :key="item.companyId"-->
<!--            :label="item.companyName"-->
<!--            :value="item.companyId"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="公司名" prop="companyId">
        <select-tree
          v-model="selectedCompanyList"
          :raw-data="deptList"
          placeholder="请选择销售"
          :parentSelectable="true"
          :multiple="true"
          component-width="300px"
          :max-display-tags="3"
          :check-strictly="false"
          :return-leaf-only="false"
        ></select-tree>
      </el-form-item>
      <el-form-item label="规则编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入规则名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="类型 " prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small"  >
          <el-option
            :key="1"
            :label="'个微'"
            :value="1"
          />
          <el-option
            :key="2"
            :label="'企微'"
            :value="2"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="规则状态 " prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择规则状态" clearable size="small" >
          <el-option
            v-for="dict in sysSopStatus"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入规则名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" border :data="sopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="规则编号" align="center" prop="id" />
      <el-table-column label="规则名称" align="center" prop="name" />
      <el-table-column label="规则状态" align="center" prop="status" width="150">
        <template slot-scope="scope">
          <dict-tag :options="sysSopStatus" :value="scope.row.status"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="类别" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.type==1">个微</el-tag>
          <el-tag type="primary" v-else-if="scope.row.type==2">企微</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发送方式" align="center" prop="sendType">
        <template slot-scope="scope">
          <dict-tag :options="sysQwSopType" :value="scope.row.sendType"/>
        </template>
      </el-table-column>

      <el-table-column label="模板" align="center" prop="tempId" />


      <el-table-column label="新客户自动创建sop" align="center" prop="isAutoSop" width="140">
        <template slot-scope="scope">
          <span v-if="scope.row.sendType!=1 && scope.row.isAutoSop == '1'" style="margin-left: 10px;color: #13ce66">已开启</span>
          <span v-if="scope.row.sendType!=1 && scope.row.isAutoSop == '2'" style="margin-left: 10px;color: #ff4949">已关闭</span>
          <span v-if="scope.row.sendType==1" style="margin-left: 10px;color: rgb(250,114,3)">企微接口无法设置</span>
        </template>
      </el-table-column>
      <el-table-column label="过期时间" align="center" prop="expiryTime" >
        <template slot-scope="scope">
          {{scope.row.expiryTime}} 小时
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="修改规则状态时间" align="center" prop="stopTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  width="180px" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status==2||scope.row.status==0 || scope.row.status == 3 || scope.row.status == 4 "
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="selectSchedule(scope.row)"
          >查看营期</el-button>
          <el-button
            v-if="scope.row.status==2 || scope.row.status==0 || scope.row.status == 3 || scope.row.status == 4  "
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleScheduleDetail(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-drawer :title="sopLogsDialog.title" :visible.sync="sopLogsDialog.open" size="70%" style="font-weight: bolder">
      <sopLogsDetails ref="sopLogsDetails" :rowDetailFrom="sopLogsDialog.sopLogsForm"></sopLogsDetails>
    </el-drawer>

  </div>
</template>

<script>
import { listSop, exportSop, } from "@/api/qw/sop";

import sopLogsDetails from '@/views/qw/sopLogs/sopLogsList.vue'
import { getCompanyList } from '@/api/company/company'
import SelectTree from '@/components/TreeSelect/index.vue'
import { getDeptData } from '@/api/system/employeeStats'
export default {
  name: "Sop",
    components: { SelectTree, sopLogsDetails},
  data() {
    return {

      selectedCompanyList: [],
      deptList: [],
      //模板查询
      tempListLoading:false,
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      //选中的map
      selectionMap:{},
      myQwCompanyList:[],
      //销售员工列表
      companyUserLists:[],
      companys: [],
      courseList:[],
      // videoList:[],
      tags:null,
      excludeTags:null,
      // 非单个禁用
      single: true,
      setting:[],
      tagList:[],
      tempList:[],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企微sop表格数据
      sopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      companyUserList:[],
      // 状态字典
      statusOptions: [],
      //sop状态
      sysSopStatus: [],

      autoSopOpen:{
        title:'',
        open:false,
        id:null,
        isAutoSop:null,
      },
      outTimeOpen:{
        title:'',
        open:false,
        id:null,
        expiryTime:null,
      },

      //企微SOP发送类型
      sysQwSopType: [],

      //SOP课程观看状态
      sysFsSopWatchStatus:[],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        name: null,
        status: null,
        sendType:null,
        type: null,
        qwUserIds: null,
        setting: null,
        createBy: null,
        corpId: null,
        createTime: null,
        companyId: null
      },
      sopLogsDialog:{
        title:'',
        open:false,
        sopLogsForm:[],
      },
      // 表单参数
      form: {
        autoSopTime:{ autoSopType:2,autoStartTime:'00:00',autoEndTime:'24:00',autoSopSend:2},
      },
      userSelectList:[],
      listUser:{
        title:"",
        open:false
      },
      // 表单校验
      rules: {
        name:[ { required: true, message: "名称不能为空", trigger: "submit" }],
        type:[ { required: true, message: "不能为空", trigger: "submit" }],
        sendType:[ { required: true, message: "不能为空", trigger: "submit" }],
        startTime:[ { required: true, message: "开始时间不能为空", trigger: "submit" }],
        tempId:[ { required: true, message: "模板不能为空", trigger: "submit" }],
      },
      autoSopTimeRules:{
        autoSopType:[ { required: true, message: "选项不能为空", trigger: "submit" }],
        autoStartTime:[ { required: true, message: "起始时间不能为空", trigger: "submit" }],
        autoEndTime:[ { required: true, message: "结束时间不能为空", trigger: "submit" }],
      }
    };
  },
  created() {

    this.getDicts("sys_sop_status").then(response => {
      this.sysSopStatus = response.data;
    });


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

    this.getDicts("sys_qw_sop_type").then(response => {
      this.sysQwSopType = response.data;
    });

    getCompanyList().then(response => {
      this.companys = response.data;
    });

    getDeptData().then(response => {
      this.deptList = response.data;
    })

    this.getList();

  },

  methods: {

    /** 查询企微sop列表 */
    getList() {
      this.loading = true;

      if(this.selectedCompanyList != null && this.selectedCompanyList.length > 0) {
        this.queryParams.userIds = this.selectedCompanyList;
      }else {
        this.queryParams.userIds = [];
      }
      listSop(this.queryParams).then(response => {

        this.sopList = response.data.list;
        this.total = response.data.total;
        this.loading = false;

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        status: 1,
        sendType:2,
        type: 2,
        filterType:2,
        qwUserIds: null,
        corpId: null,
        setting: null,
        createBy: null,
        createTime: null,
        isAutoSop:null,
        autoSopTime:{ autoSopType:2,autoStartTime:'00:00',autoEndTime:'24:00',autoSopSend:2},
      };
      this.resetForm("form");
      this.tags = null;
      this.excludeTags = null;
    },
    /** 搜索按钮操作 */
    handleQuery() {

      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.selectedCompanyList=[];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.selectionMap=selection;
      this.single = selection.length!==1
      this.multiple = !selection.length
    },


    /**
    * 查看SOP任务内营期
    */
    selectSchedule(row){
      const data ={
        id : row.id,
        name :row.name,
        tempId :row.tempId
      }
      // 使用 params 传递参数
      this.$router.push({
        path: `/qw/sopUserLogs/sopUserLogsSchedule/${data.id}`,
        query: { name: data.name,
          tempId: data.tempId, } // 如果需要传递更多参数，可以使用 query
      });
    },


    /**
    * 查看营期内详情
    */
    handleScheduleDetail(row){
        this.sopLogsDialog.title='规则执行详情';
        this.sopLogsDialog.open=true;
        this.sopLogsDialog.sopLogsForm=row;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企微sop数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportSop(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style scoped>
.custom-input ::v-deep  .el-input__inner {
  height: 20px;
  padding: 0 4px;
  text-align:center;
  display: block;
}
.custom-input ::v-deep  .el-input__icon {
  line-height: 20px;
}
</style>
