<template>
  <div class="videos pagePadding">
    <div
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <ex-table
        v-loading="loading"
        :table="table"
        :data="tableData"
        :columns="columns"
        :page="pageInfo"
        :size="size"
        @page-curr-change="handlePageChange"
        @page-size-change="handleSizeChange"
        @screen-reset="
          pageInfo.curr = 1;
          searchList();
        "
        @screen-submit="
          pageInfo.curr = 1;
          searchList();
        "
      >
        <template #table-header="{}">
          <el-row  style="width:100%">
            <el-col style="width: 150px">
              <el-select
                :size="searchSize"
                v-model="parmValue.status"
                placeholder="视频状态"
              >
                <el-option
                  v-for="item in statusList"
                  :key="'status' + item.id"
                  :label="item.label"
                  :value="item.id"
                />
              </el-select>
            </el-col>

            <el-col :span="15" style="width: 400px;padding:0 0 0 10px">
              <el-input
                :size="searchSize"
                placeholder="请输入内容"
                v-model="input"
                class="input-with-select"
              >
                <el-select
                  v-model="select"
                  slot="prepend"
                  placeholder="请选择"
                  style="width: 120px"
                >
                  <el-option label="视频名称" value="1"></el-option>
                  <el-option label="视频链接" value="2"></el-option>
                  <el-option label="视频编号" value="3"></el-option>
                </el-select>
                <el-button
                  slot="append"
                  @click.native="searchList"
                  icon="el-icon-search"
                >
                </el-button>
              </el-input>
            </el-col>
            <el-col
              :span="3"
              style="width: 66px; float: left; margin-left: 10px"
            >
              <el-button type="warning" :size="searchSize" @click="restSearch">
                重置
              </el-button>
            </el-col>
            <el-col
              :span="3"
              v-if="powers.some((item) => item == '003')"
              style="width: 70px; float: right"
            >
              <el-button type="primary" :size="searchSize" @click="restSearch">
                搜索
              </el-button>
            </el-col>
            <el-col
              :span="2"
              v-if="powers.some((item) => item == '003')"
              style="width: 70px; float: right; margin-right: 10px"
            >
              <el-button
                v-if="powers.some((item) => item == '003')"
                type="primary"
                :size="searchSize"
                @click="openModal('add', '003', {})"
              >
                添加
              </el-button>
            </el-col>
          </el-row>
        </template>
        <template #operation="{ scope }">
          <el-tooltip
            v-if="powers.some((item) => item == '007')"
            class="item"
            effect="dark"
            content="详情"
            placement="top"
          >
            <i
              class="el-icon-view tb-icon"
              @click="openModal(scope.row.id, '007', scope.row)"
            ></i>
          </el-tooltip>
          <el-tooltip
            v-if="powers.some((item) => item == '005')"
            class="item"
            effect="dark"
            content="修改"
            placement="top"
          >
            <i
              class="el-icon-edit tb-icon"
              @click="openModal(scope.row.id, '005', scope.row)"
            ></i>
          </el-tooltip>
          <el-tooltip
            v-if="
              powers.some((item) => item == '004') && scope.row.status === '1'
            "
            class="item"
            effect="dark"
            content="禁用"
            placement="top"
            @click="openModal(scope.row.id, '004', scope.row)"
          >
            <i
              class="tb-icon el-icon-error"
              @click="selectPop(scope.row.status, scope.row.id)"
            ></i>
          </el-tooltip>
          <el-tooltip
            v-if="
              powers.some((item) => item == '004') && scope.row.status !== '1'
            "
            class="item"
            effect="dark"
            content="启用"
            placement="top"
            @click="openModal(scope.row.id, '004', scope.row)"
          >
            <i
              class="tb-icon el-icon-success"
              @click="selectPop(scope.row.status, scope.row.id)"
            ></i>
          </el-tooltip>
          <!-- <el-tooltip
            v-if="powers.some((item) => item == '006')"
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            @click="openModal(scope.row.id, '006', scope.row)"
          >
            <i
              class="tb-icon el-icon-delete-solid"
              @click="deletePop(scope.row.id)"
            ></i>
          </el-tooltip> -->
        </template>
        <template #status="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="
              scope.row.status == '0'
                ? 'danger'
                : scope.row.status == '1'
                ? 'success'
                : ''
            "
            v-text="
              (statusList.find((item) => item.status == scope.row.status) || {})
                .label || '--'
            "
          >
          </el-tag>
        </template>
      </ex-table>
      <add-edit
        :id="modelId"
        :sitem="sitem"
        :show-model="showModel"
        :is-detail="isDetail"
        @refresh="searchList"
        @cancel="showModel = false"
      />
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
<script>
import mixinPage from "@/mixins/elPaginationHandle";
import asyncRequest from "@/apis/service/mobile/videos";
import ExTable from "@/components/ExTableNew.vue";
import addEdit from "./addEdit";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
export default {
  name: "videos",
  components: {
    addEdit,
    ExTable,
  },
  mixins: [mixinPage, resToken],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "videos"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      sitem: null,
      select: "1",
      input: "",
      statusList: [
        { status: "1", label: "启用" },
        { status: "0", label: "禁用" },
      ],
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        page: 1, // 页码数
        size: 15, // 页容量
        status: "", //视频状态
        video_sn: "", //视频编号
        video_name: "", // 视频名称
        video_url: "", // 视频地址
      },
      tableData: [],
      // 表格 - 数据
      table: {
        stripe: true, //是否是斑马条纹边框
        border: true, //是否显示纵向边框
        // _defaultHeader_: ["setcol"]
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        {
          prop: "id",
          label: "ID",
          width: "70px",
          fixed: "left",
        },
        {
          prop: "video_name",
          label: "视频名称",
          width: "250px",
        },
        {
          prop: "remark",
          label: "视频描述",
        },
        {
          prop: "status",
          label: "视频状态",
          _slot_: "status",
          width: "100px",
        },
        {
          prop: "addtime",
          label: "创建时间",
          sortable: true,
          width: "140px",
        },
        {
          prop: "",
          label: "操作",
          fixed: "right",
          _noset_: true,
          width: "150px",
          _slot_: "operation",
        },
      ],
    };
  },
  mounted() {
    this.searchList(); //异步调用接口，请求数据
  },
  methods: {
    selectPop(staus, id) {
      this.$confirm("此操作将切换视频状态, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.changeBtnStatus(staus, id);
      });
    },
    msgPop() {
      this.$alert(res.msg, {
        confirmButtonText: "确定",
        callback: (action) => {
          this.$message({
            type: "info",
            message: `action: ${action}`,
          });
        },
      });
    },
    deletePop(id) {
      this.$alert("请确认要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.deleteBtnProject(id);
      });
    },
    //-重置-输入框数据
    restSearch() {
      this.select = "1";
      this.input = "";
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        video_name: "", // 视频名称
        video_url: "", // 视频链接
        video_sn: "", //视频编号
        status: "", //视频状态
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      this.searchList(); //请求接口数据
    },
    // 打开弹窗，添加-详情-修改-启/禁用-删除按钮
    openModal(id, isDetail, sitem) {
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
    },
    //
    async changeBtnStatus(status, id) {
      this.loading = false; //开启loading加载
      let changeValue = {
        id: "",
        status: "",
      };
      changeValue.status = status === "1" ? "0" : "1";
      changeValue.id = id;
      var res = await asyncRequest.change(changeValue); //请求更新视频接口/admin/Change
      if (res && res.code === 0 && res.data) {
        //正常响应的操作
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout(); //登出
      } else {
        this.msgPop(); //警告弹窗，提示接口返回信息
      }
      this.loading = false; //停止loading
      this.searchList();
    },
    async deleteBtnProject(id) {
      this.loading = false;
      let deleteValue = {
        id: "",
      };
      deleteValue.id = id; //入参
      var res = await asyncRequest.delete(deleteValue); //调用/admin/Delete接口，
      if (res && res.code === 0) {
        //正常响应后的操作
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.msgPop();
      }
      this.loading = false;
      this.searchList();
    },
    async searchList() {
      this.loading = true; //开启加载loading
      this.parmValue.video_name = this.select === "1" ? this.input : ""; //把用户输入的name赋到video_name上
      this.parmValue.video_url = this.select === "2" ? this.input : ""; //赋url
      this.parmValue.video_sn = this.select === "3" ? this.input : ""; //赋video_sn
      const res = await asyncRequest.list(this.parmValue); //调用视频列表接口/admin/video
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.video; //正常响应把响应数据存到tableData中。
        this.pageInfo.total = Number(res.data.count); //存入总数据量
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout(); //超时登出
      } else {
        this.tableData = []; //如果服务器未正常响应且不是登出状态，初始化数据
        this.pageInfo.total = 0;
      }
      this.loading = false; //关闭loading
    },
  },
};
</script>
<style lang="scss" scoped>
.el-select .el-input {
  width: 130px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
  width: 100px;
}
</style>