<template>

  <div class="body-layer">
    <div class="body-head">
      <h3>視頻列表</h3>
      <div class="table-ctrl-right">
        <!-- <el-input placeholder="關鍵詞" suffix-icon="el-icon-search" class="search-scene" v-model="search.state.searchKey" style="width: auto" /> -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="oper.readyInsert"
          >添加視頻</el-button
        >
      </div>
    </div>
    <el-table
      ref="multipleTable"
      :data="dataList.state"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column label="視頻名稱" prop="videoName"></el-table-column>
      <el-table-column label="視頻鏈接" prop="videoUrl"></el-table-column>
      <el-table-column label="顯示狀態" prop="showStatus" v-slot:default="{ row }">
        <el-switch
          @click="clickChange(row)"
          :loading="loading"
          v-model="row.showStatus"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-table-column>
      <el-table-column label="創建時間" prop="createTime"></el-table-column>
      <el-table-column label="操作" v-slot:default="{ row }" >
        <span class="oper-span" @click="oper.readyUpdate(row)" >編輯</span>
        <span class="oper-span" @click="dataList.delete(row)" style="color: var(--primaryColor)">刪除</span>
      </el-table-column>
    </el-table>

    <com-pagination
      @size-change="pag.sizeChange"
      @current-change="pag.currentChange"
      :current-page="pag.state.currPage"
      :page-size="pag.state.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pag.state.total"/>
  </div>
  
  <com-dialog
    :title="(oper.state.id ? '修改' : '添加') + '視頻'"
    v-model:show="oper.state.show"
    @submit="submit"
  >
    <el-form ref="form" :model="oper.state" label-width="100px" class="shop-from">
      <!-- <el-form-item label="視頻類型：" class="mandatory">
        <el-select v-model="oper.state.region" placeholder="視頻類型：">
          <el-option label="區域一" value="shanghai"></el-option>
          <el-option label="區域二" value="beijing"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="視頻名稱：" class="mandatory">
        <el-input  maxlength="15" show-word-limit :modelValue="oper.state.videoName" @update:modelValue="val => oper.state.videoName = val.trim()"></el-input>
      </el-form-item>
     
      <el-form-item label="上傳視頻：" class="mandatory">
        <com-upload @changeVideo="changeVideo" @close="()=>oper.state.videoUrl=''" @update:modelValue="val => oper.state.videoUrl = val" :video="oper.state.videoUrl"/>
        <!-- <com-company :modelValue="oper.state.videoUrl" @update:modelValue="val => oper.state.deptId = val.trim()" hideAll /> -->
      </el-form-item>
      <!-- <el-form-item label="物理地址：" class="mandatory">
        <el-input :modelValue="oper.state.childName" @update:modelValue="val => oper.state.childName = val.trim()"></el-input>
      </el-form-item>
      <el-form-item label="S/N：" class="mandatory">
        <el-input :modelValue="oper.state.snCode" @update:modelValue="val => oper.state.snCode = val.trim()"></el-input>
      </el-form-item> -->
  
    </el-form>
  </com-dialog>

</template>

<script>
import axios from 'axios'
import { ref, watch } from "vue";
import getTableState from "@/state/tableRef";
import auth from "@/state/viewAuth";
import comDialog from "@/components/dialog";
import comUpload from "@/components/upload-video";
import comPagination from "@/components/pagination";
import { dateFormat } from '@/util'
import {
  getVideoList,
  insertVideo,
  updateVideo,
  deleteVideo,

  getCategoryList,
  getDeptList,
  getSceneList,

  uploadFile
} from '@/request/config'



export default {
  name: 'shop',
  setup() {
    const state = getTableState({
      updateUrl: updateVideo,
      insertUrl: insertVideo,
      getUrl: getVideoList,
      delUrl: deleteVideo,
      searchAttr: {  },
      operAttr: {
        videoName: '',
        videoUrl:'',
        showStatus: 0
      },
      throttleTime: 0,
      pagAttr: {totalNum: 'total',fixBooStatus: 'showStatus'}
    });
    const headList = ref([{ name: "視頻管理", value: '0' }]);
    const time = ref(null)
    const loading = ref(false)
    

    watch(time, () => {
      if (time.value) {
        state.search.value.state.startTime = dateFormat(new Date(time.value[0]), 'yyyy-MM-dd hh:mm:ss')
        state.search.value.state.endTime = dateFormat(new Date(time.value[1]), 'yyyy-MM-dd 23:59:59')
      } else {
        state.search.value.state.startTime = state.search.value.state.endTime = ''
      }
    })
    watch(
      [() => state.search.value.state.startTime, () => state.search.value.state.endTime],
      () => {
        if (!state.search.value.state.startTime || !state.search.value.state.endTime) {
          time.value = null
        }
      }
    )
    
    watch(
      () => state.pag.value.state.total,
      () => {
        headList.value[0].name = '開場視頻（' + state.pag.value.state.total +'）'
      }
    )


    console.log( {...state});

    return { ...state, loading, headList, time, auth,getCategoryList,getDeptList,getSceneList};
  },
  methods: {

    async clickChange(row){
        this.oper.state = {...row, showStatus: Number(row.showStatus)}
        this.oper.update()
    },
    
   
    async changeVideo(file) {
      let res = await axios.post(uploadFile, {
        file
      })
      this.oper.state.videoUrl = res.data
    },
   
    submit() {
      if (!this.oper.state.videoName.trim()) {
        return this.$alert('視頻名稱不能為空！', '提示')
      } else if (!this.oper.state.videoUrl.trim()) {
        return this.$alert('視頻不能為空！', '提示')
      }

      this.oper.state.id ? this.oper.update() : this.oper.insert()

      this.$message({
        type: 'success',
        message: '操作成功'
      });
    }
  },
  components: {
    "com-dialog": comDialog,
    "com-pagination": comPagination,
    "com-upload": comUpload,
  },
};
</script>

<style lang="scss" scoped>
.table-ctrl-right {
  .search-scene {
    margin: 0 20px 0 26px;
  }
  i {
    margin-left: 20px;
    font-size: 1.7rem;
    vertical-align: middle;
    cursor: pointer;

    &.active {
      color: var(--primaryColor);
    }
  }
}

.shop-from {
  width: 320px;
  margin: 0 auto;
}
</style>
