<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="身体素质名称" prop="physicalQualityName">
        <el-input
          v-model="queryParams.physicalQualityName"
          placeholder="请输入身体素质名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运动程度开始值" prop="degreeStartFraction">
        <el-input
          v-model="queryParams.degreeStartFraction"
          placeholder="请输入运动程度开始值"
          clearable
          oninput="value=value.replace(/[^\d]/g,'')"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运动程度结束值" prop="degreeEndFraction">
        <el-input
          v-model="queryParams.degreeEndFraction"
          placeholder="请输入运动程度结束值"
          clearable
          oninput="value=value.replace(/[^\d]/g,'')"
          @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>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:motionPrescriptionLibrary:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:motionPrescriptionLibrary:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:motionPrescriptionLibrary:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:motionPrescriptionLibrary:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="motionPrescriptionLibraryList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="身体素质名称" align="center" prop="physicalQualityName" />
      <el-table-column label="运动程度开始值" align="center" prop="degreeStartFraction" />
      <el-table-column label="运动程度结束值" align="center" prop="degreeEndFraction" />
      <el-table-column label="运动类型" align="center" prop="motionType" />
      <el-table-column label="运动频率" align="center" prop="motionFrequency" />
      <el-table-column label="运动强度" align="center" prop="motionStrength" />
      <el-table-column label="运动时间" align="center" prop="motionTime" />
      <el-table-column label="运动量" align="center" prop="motionCapacity" />
      <el-table-column label="进阶时间" align="center" prop="advancedTime" />
      <el-table-column label="进阶方案" align="center" prop="advancedScheme" show-overflow-tooltip />
      <el-table-column label="处方视频" align="center" prop="prescriptionVideoUrl">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-if="scope.row.prescriptionVideoFlag == 'YES'"
            @click="uploadhandle(scope.row)"
            v-hasPermi="[
              'system:motionPrescriptionLibrary:getPrescriptionVideoInfo',
            ]"
          >查看</el-button>
          <div v-else>无</div>
        </template>
      </el-table-column>
      <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-upload2"
            @click="uploadopentrue(scope.row)"
          >上传视频</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:motionPrescriptionLibrary:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:motionPrescriptionLibrary:remove']"
          >删除</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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" :inline="true">
        <el-form-item label="身体素质名称" prop="physicalQualityName">
          <el-input
            v-model="form.physicalQualityName"
            placeholder="请输入身体素质名称"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="运动程度开始值" prop="degreeStartFraction">
          <el-input
            v-model="form.degreeStartFraction"
            placeholder="请输入运动程度开始值"
            style="width: 250px"
            oninput="value=value.replace(/[^\d]/g,'')"
          />
        </el-form-item>
        <el-form-item label="运动程度结束值" prop="degreeEndFraction">
          <el-input
            v-model="form.degreeEndFraction"
            placeholder="请输入运动程度结束值"
            style="width: 250px"
            oninput="value=value.replace(/[^\d]/g,'')"
          />
        </el-form-item>
        <el-form-item label="运动频率" prop="motionFrequency">
          <el-input v-model="form.motionFrequency" placeholder="请输入运动频率" style="width: 250px" />
        </el-form-item>
        <el-form-item label="运动强度" prop="motionStrength">
          <el-input v-model="form.motionStrength" placeholder="请输入运动强度" style="width: 250px" />
        </el-form-item>
        <el-form-item label="运动时间" prop="motionTime">
          <el-input v-model="form.motionTime" placeholder="请输入运动时间" style="width: 250px" />
        </el-form-item>
        <el-form-item label="运动量" prop="motionCapacity">
          <el-input v-model="form.motionCapacity" placeholder="请输入运动量" style="width: 250px" />
        </el-form-item>
        <el-form-item label="进阶时间" prop="advancedTime">
          <el-input v-model="form.advancedTime" placeholder="请输入进阶时间" style="width: 250px" />
        </el-form-item>
        <el-form-item label="进阶方案" prop="advancedScheme">
          <el-input
            v-model="form.advancedScheme"
            type="textarea"
            placeholder="请输入内容"
            style="width: 500px"
            :rows="7"
          />
        </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>
    <!-- //上传视频 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <!-- <video
        v-if="videourl"
        :src="baseurl+videourl"
        style="width:350px;height:200px"
        controls="controls"
      ></video>-->
      <el-upload
        ref="upload"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :data="upload.data"
        :file-list="fileList"
        :on-change="handleChange"
        :before-remove="handleFileremove"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看视频 -->
    <el-dialog title="查看" :visible.sync="lookvideoshow" width="1200px" append-to-body>
      <div v-if="lookvideolist.length > 0">
        <video
          v-for="(item, index) in lookvideolist"
          :key="index"
          :src="item"
          style="
            width: 350px;
            height: 200px;
            display: inline-block;
            margin: 0 30px 30px 0;
          "
          controls="controls"
        ></video>
      </div>
      <div v-else>
        <el-empty description="暂无视频"></el-empty>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="lookvideoshow = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listMotionPrescriptionLibrary,
  getMotionPrescriptionLibrary,
  delMotionPrescriptionLibrary,
  addMotionPrescriptionLibrary,
  updateMotionPrescriptionLibrary,
  getPrescriptionVideoInfo,
  confirmUploadVideo,
  deletePrescriptionVideo,
  prescriptionVideoUpload
} from "@/api/system/motionPrescriptionLibrary";
import { getToken } from "@/utils/auth";
export default {
  name: "MotionPrescriptionLibrary",
  data() {
    return {
      baseurl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运动处方库数据表格数据
      motionPrescriptionLibraryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //查看视频
      lookvideoshow: false,
      //查看视频数组
      lookvideolist: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        physicalQualityName: null,
        degreeStartFraction: null,
        degreeEndFraction: null,
        motionType: null,
        motionFrequency: null,
        motionStrength: null,
        motionTime: null,
        motionCapacity: null,
        advancedTime: null,
        advancedScheme: null,
        prescriptionVideoUrl: null
      },
      // 表单参数
      form: {},
      //上传视频
      // 用户导入参数
      videourl: [],
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "上传视频",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        data: {
          motionPrescriptionId: null
        },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API +
          "/system/motionPrescriptionLibrary/uploadMotionPrescriptionVideo"
      },
      // 表单校验
      rules: {
        physicalQualityName: [
          {
            required: true,
            message: "请输入身体素质名称",
            trigger: "blur"
          }
        ],
        degreeStartFraction: [
          {
            required: true,
            message: "请输入运动程度开始值",
            trigger: "blur"
          }
        ],
        degreeEndFraction: [
          {
            required: true,
            message: "请输入运动程度结束值",
            trigger: "blur"
          }
        ],
        motionType: [
          {
            required: true,
            message: "请输入运动类型",
            trigger: "blur"
          }
        ],
        motionFrequency: [
          {
            required: true,
            message: "请输入运动频率",
            trigger: "blur"
          }
        ],
        motionStrength: [
          {
            required: true,
            message: "请输入运动强度",
            trigger: "blur"
          }
        ],
        motionTime: [
          {
            required: true,
            message: "请输入运动时间",
            trigger: "blur"
          }
        ],

        motionCapacity: [
          {
            required: true,
            message: "请输入运动量",
            trigger: "blur"
          }
        ],
        advancedTime: [
          {
            required: true,
            message: "请输入进阶时间",
            trigger: "blur"
          }
        ],
        advancedScheme: [
          {
            required: true,
            message: "请输入进阶方案",
            trigger: "blur"
          }
        ]
      },
      fileList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询运动处方库数据列表 */
    getList() {
      this.loading = true;
      listMotionPrescriptionLibrary(this.queryParams).then(response => {
        this.motionPrescriptionLibraryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        physicalQualityName: null,
        degreeStartFraction: null,
        degreeEndFraction: null,
        motionType: null,
        motionFrequency: null,
        motionStrength: null,
        motionTime: null,
        motionCapacity: null,
        advancedTime: null,
        advancedScheme: null,
        prescriptionVideoUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      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;
      getMotionPrescriptionLibrary(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运动处方数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMotionPrescriptionLibrary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMotionPrescriptionLibrary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function() {
          return delMotionPrescriptionLibrary(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/motionPrescriptionLibrary/export",
        {
          ...this.queryParams
        },
        `motionPrescriptionLibrary_${new Date().getTime()}.xlsx`
      );
    },
    uploadhandle(e) {
      this.lookvideolist = [];
      getPrescriptionVideoInfo(e.id).then(res => {
        res.data.forEach(e => {
          e.prescriptionVideoUrl = this.baseurl + e.prescriptionVideoUrl;
          this.lookvideolist.push(e.prescriptionVideoUrl);
        });
        this.lookvideoshow = true;
      });
    },
    //打开视频上传窗口
    uploadopentrue(e) {
      this.videourl = [];
      this.fileList = [];
      this.upload.data.motionPrescriptionId = e.id;
      prescriptionVideoUpload(e.id).then(res => {
        if (
          res.data.prescriptionVideoUrlList &&
          res.data.prescriptionVideoUrlList.length > 0
        ) {
          res.data.prescriptionVideoUrlList.forEach(el => {
            var obj = {
              name: el.prescriptionVideoName,
              response: el
            };
            this.fileList.push(obj);
          });
        }
        this.upload.open = true;
      });
    },
    //文件上传
    handleChange(file, fileList) {},
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    //删除文件
    handleFileremove(file, fileList) {
      // deletePrescriptionVideo(file.response.prescriptionVideoUrl).then(res => {
      //   this.$modal.msgSuccess("删除视频成功");
      // });
      this.fileList = this.fileList.filter(e => e != file);
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.fileList = fileList.slice();
      this.$refs.upload.clearFiles();
      // this.$alert(
      //   "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
      //     response.msg +
      //     "</div>",
      //   "上传结果",
      //   { dangerouslyUseHTMLString: true }
      // );
    },
    // 提交上传文件
    submitFileForm() {
      this.fileList.forEach(e => {
        this.videourl.push(e.response);
      });
      var obj = {
        motionPrescriptionId: this.upload.data.motionPrescriptionId,
        prescriptionVideoUrlList: this.videourl
      };
      confirmUploadVideo(obj).then(res => {
        this.upload.open = false;
        this.$modal.msgSuccess("上传视频成功");
        this.getList();
      });
    }
  }
};
</script>
<style scoped>
</style>
