<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="categoryName"></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-input  maxlength="50" show-word-limit :modelValue="oper.state.categoryName" @update:modelValue="val => oper.state.categoryName = val.trim()"></el-input>
      </el-form-item>
  
    </el-form>
  </com-dialog>

</template>

<script>
import { ref, watch } from "vue";
import getTableState from "@/state/tableRef";
import auth from "@/state/viewAuth";
import comDialog from "@/components/dialog";
import comPagination from "@/components/pagination";
import { dateFormat } from '@/util'
import {
  getShopCategoryList,
  insertCategory,
  updateCategory,
  deleteCategory,
} from '@/request/config'



export default {
  name: 'shop',
  setup() {
    const state = getTableState({
      updateUrl: updateCategory,
      insertUrl: insertCategory,
      getUrl: getShopCategoryList,
      delUrl: deleteCategory,
      searchAttr: {  },
      operAttr: {
        categoryName: ''
      },
      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};
  },
  methods: {
   
  
   
    submit() {
      if (!this.oper.state.categoryName.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,
  },
};
</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>
