<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['course:sop:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="sopList">
      <el-table-column label="规则名称" align="center" prop="name" />
      <el-table-column label="课节名称" align="center" prop="courseTitle" />
      <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="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['course:sop:edit']"
          >执行详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="chooseCourseSop(scope.row)"
            v-hasPermi="['course:sop:edit']"
          >选择此课节SOP</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="60%" style="font-weight: bolder" append-to-body>
      <sop-logs-details ref="sopLogsDetails"></sop-logs-details>
    </el-drawer>

    <!-- 设置视频的过期时间   -->
    <el-dialog :title="setExpireTimeDialog.title" :visible.sync="setExpireTimeDialog.open" width="500px" append-to-body>
      <el-form ref="setExpireTimeDialog" :model="setExpireTimeDialog" label-width="150px">
        <el-form-item label="过期时间（天）">
          <el-row>
            <el-input-number  v-model="setExpireTimeDialog.days"  :min="0" :max="100" ></el-input-number>
          </el-row>
          <el-row>
            <span class="tip">填写0时，默认为系统配置的默认时间</span>
          </el-row>
        </el-form-item>
        <div class="line"></div>
        <div style="float:right;margin-right:20px">
          <el-button type="primary" @click="onSubmit">确定</el-button>
        </div>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { listSop, getSop, delSop, addSop, updateSop, exportSop } from "@/api/course/sop";
import  Material  from "@/views/course/Material/index.vue";
import sopLogsDetails from '@/views/course/sopLogs/sopLogsList.vue'
import userCourseVideo from '@/views/course/sop/userCourseVideo.vue';
export default {
  name: "courseSop",
  components: {sopLogsDetails,userCourseVideo,Material},
  data() {
    return {
      imageArr:[],
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      setting:[],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企微sop表格数据
      sopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      //SOP课程观看状态
      sysFsSopWatchStatus:[],
      //课程列表
      userCourseList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        status: null,
        type: 1,
        qwUserIds: null,
        createBy: null,
        createTime: null
      },
      //查询课程
      courseParams:{
        isShow: "1",
        courseName:null,
      },
      //课程小节
      chooseCourseDialog:{
        title:'',
        open:false,
      },
      //SOP 日志
      sopLogsDialog:{
        title:'',
        open:false,
      },
      //设置过期时间的
      setExpireTimeDialog:{
        title:'请设置短链的过期时间',
        open:false,
        days:0,
        data:null,
      },
      // 表单参数
      form: {},
      listUser:{
        title:"",
        open:false
      },
      // 表单校验
      rules: {
        name:[ { required: true, message: "名称不能为空", trigger: "submit" }],
        status:[ { required: true, message: "不能为空", trigger: "submit" }],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_company_status").then(response => {
      this.statusOptions = response.data;
    });

    this.getDicts("sys_fs_sop_watch_status").then(response => {
      this.sysFsSopWatchStatus = response.data;
    });

  },
  watch:{

  },
  methods: {

    //选择课节SOP
    chooseCourseSop(row){

      this.setExpireTimeDialog.data=row;
      this.setExpireTimeDialog.open=true;
      // this.$emit('bindCourseSop',row)
    },

    onSubmit(){
      this.setExpireTimeDialog.open=false;
      this.$emit('bindCourseSop',this.setExpireTimeDialog.data,this.setExpireTimeDialog.days)
    },
    /** 查询企微sop列表 */
    getList() {
      this.loading = true;
      listSop(this.queryParams).then(response => {
        this.sopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        status: 1,
        sopType:1,
        type: null,
        qwUserIds: null,
        setting: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /**
     * 查看详情
     */
    handleDetail(row){
      this.sopLogsDialog.title='规则执行详情';
      this.sopLogsDialog.open=true;

      setTimeout(() => {
        this.$refs.sopLogsDetails.selectSopLogsDetails(row.id)
      }, 200);

    },


    /** 导出按钮操作 */
    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 /deep/ .el-input__inner {
  height: 20px;
  padding: 0 4px;
  text-align:center;

}
.custom-input /deep/ .el-input__icon {
  line-height: 20px;
}
</style>
