<template>
  <!-- 全景课堂 -->
  <div class="ssysKcbglManage" v-loading="loading">
    <div class="searchDiv flex">
      <div class="flex">
        <el-button size="default" type="success" @click="handleAddOrEdit()">新建</el-button>
      </div>
      <div class="flex">
        <el-input v-model="keyword" size="default" placeholder="请输入搜索的关键词" :suffix-icon="Search" />
        <el-button size="default" type="primary" @click="initData" style="margin-left: 10px;">搜索</el-button>
      </div>
    </div>

    <div class="searchContent">
      <div class="tableList">
        <div class="tableDiv">
          <el-table ref="multipleTableRef" :data="tableData" height="100%" style="width: 100%">
            <el-table-column prop="ksk_name" width="" label="课表名称" show-overflow-tooltip></el-table-column>
            <el-table-column width="" label="状态" show-overflow-tooltip>
              <template #default="{row}">
                <span :style="{color: row.ksk_status_option_k == 2 ? '#FF0000' : 'normal'}">{{row.ksk_status}}</span>
              </template>
            </el-table-column>
            <el-table-column width="" label="课程表" show-overflow-tooltip>
              <template #default="{row}">
                <el-image v-if="row.ksk_file!=null&&row.ksk_file!=''" style="width: 38px; height: 38px;display: block;" :src="getFullUrl(row.ksk_file)" :preview-teleported="true" :preview-src-list="[getFullUrl(row.ksk_file)]" />
              </template>
            </el-table-column>
            <el-table-column label="操作" width="">
              <template #default="{row}">
                <el-button text size="default" type="primary" @click="handleAddOrEdit(row.ksk_id)">编辑</el-button>
                <span class="split">|</span>
                <el-button text size="default" type="primary" @click="handleRemove(row.ksk_id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="paginationDiv">
          <el-pagination background layout="total, prev, pager, next" :total="total" @current-change="handleCurrentChange" :page-size="limit" :currentPage="currentPage"></el-pagination>
        </div>
      </div>
    </div>

    <el-dialog width="500px" v-model="dialogVisible" :title="dialogTitle" custom-class="commonDialog" :show-close="false" :close-on-click-modal="false">
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="80px" class="demo-ruleForm">
        <el-form-item label="课表名称" prop="ksk_name">
          <el-input v-model="ruleForm.ksk_name" />
        </el-form-item>
        <el-form-item label="状态" prop="ksk_status">
          <el-switch v-model="ruleForm.ksk_status" active-text="启用" active-value="1" inactive-text="禁用" inactive-value="2" />
        </el-form-item>
        <el-form-item label="图片" prop="ksk_file">
          <file-upload-progress v-if="dialogVisible" v-model:part="ruleForm.ksk_file" @update:part="handleUpdate" :limit="1" accept=".png,.jpg,.jpeg">
            <template #default>
              <div class="flex_center w-65px h-65px border border-dashed border-hex-E4E6E8 bg-hex-F9F9F9 text-hex-9D9AB9">
                <i-akar-icons:plus class="text-xl" />
              </div>
            </template>
            <template #tip>
              <div class="mt-2">支持格式：jpg,png,jpeg</div>
            </template>
          </file-upload-progress>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose(2)">取消</el-button>
          <el-button type="primary" @click="commitData(ruleFormRef)">提交</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup >
import { reactive, ref } from "vue";
import { Search } from "@element-plus/icons-vue";
import { getFullUrl } from "~/utils/helper";
import { kcbList, kcbAdd, kcbDetail, kcbEdit, kcbDelete } from "./api";

const loading = ref(false);

const ruleFormRef = ref(null);
const ruleForm = reactive({
  ksk_name: "",
  ksk_status: "",
  ksk_file: "",
  ksk_ext: ""
});
const rules = reactive({
  ksk_name: [{ required: true, message: "请输入课表名称", trigger: "blur" }],
  ksk_file: [{ required: true, message: "请上传课表", trigger: "submit" }],
  ksk_status: [{ required: true }]
});

const tableData = ref([]);
const total = ref(0);
const limit = ref(10);
const currentPage = ref(1);
function handleCurrentChange(val) {
  currentPage.value = val;
  initData();
}

const keyword = ref("");
initData();
async function initData() {
  let obj = {
    limit: limit.value,
    page: currentPage.value,
    keyword: keyword.value,
  };
  loading.value = true;
  let data = await kcbList(obj);
  total.value = parseInt(data.total_rows);
  tableData.value = data.page_data;
  loading.value = false;
}

const dialogVisible = ref(false);
const dialogTitle = ref("");
const currentId = ref("");
const handleAddOrEdit = (id) => {
  dialogVisible.value = true;
  if (id) {
    currentId.value = id;
    dialogTitle.value = "编辑";
    initDetailInfo();
  } else {
    dialogTitle.value = "添加";
  }
};

const initDetailInfo = async () => {
  let data = await kcbDetail({ ksk_id: currentId.value });
  for (let key in ruleForm) {
    ruleForm[key] = data.one_info[key];
  }
  ruleForm.ksk_status = data.one_info.ksk_status_option_k;
}

const handleClose = (type) => {
  dialogVisible.value = false;
  currentId.value = "";
  resetForm(ruleFormRef.value);
  if (type == 1) {
    initData();
  }
}

const handleUpdate = (data) => {
  ruleForm.ksk_ext = data.substr(data.lastIndexOf(".") + 1);
}

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};

const commitData = (formEl) => {
  if (!formEl) return;

  formEl.validate(async (valid, fields) => {
    if (valid) {
      loading.value = true;
      let data = {
        kzkt_ssyskc_kcb: ruleForm,
      };
      if (currentId.value == "") {
        await kcbAdd(data);
      } else {
        data = Object.assign({ ksk_id: currentId.value }, data);
        await kcbEdit(data);
      }
      loading.value = false;
      ElMessage.success("保存成功！");
      handleClose(1);
    } else {
      console.log("error submit!", fields);
    }
  });
}

const handleRemove = async (ksk_id) => {
  ElMessageBox.confirm("是否确定删除当前数据？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    buttonSize: "default",
  })
    .then(async () => {
      loading.value = true;
      let data = await kcbDelete({ ksk_id });
      if (data) {
        ElMessage.success("删除成功！");
        loading.value = false;
        initData();
      }
    })
    .catch(() => { });
};
</script>

<style lang="scss" scoped>
@import "~/styles/dialog.scss";
.ssysKcbglManage {
  height: calc(100vh - 50px - 2rem);
  .flex {
    display: flex;
    align-items: center;
  }
  .searchDiv {
    // height: 62px;
    background: #fff;
    // border-radius: 6px;
    // box-shadow: 0 0 5px rgba($color: #000000, $alpha: 0.1);
    // padding: 0 20px;
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid #e3e3e3;
    ::v-deep .el-button {
      height: 32px;
    }
  }
  .searchContent {
    margin-top: 12px;
    height: calc(100% - 53px - 12px);
    background: #fff;
    border-radius: 6px;
    // box-shadow: 0 0 5px rgba($color: #000000, $alpha: 0.1);
    // padding: 0 20px;
    .tableList {
      height: 100%;
      // height: calc(100% - 52px - 10px);
      position: relative;
      .tableDiv {
        // background: #ddd;
        height: calc(100% - 42px);
        .el-button {
          padding: 0;
        }
        .split {
          color: #00a3ff;
          margin: 0 10px;
        }
      }
      .paginationDiv {
        position: absolute;
        bottom: -5px;
        padding: 3px;
        width: 100%;
        display: -webkit-box;
        -webkit-box-pack: end;
      }
    }
  }
}
</style>