<script setup>
import { ref, reactive } from "vue";
import { ArrowDown, Plus, Edit, Delete, UploadFilled, Search } from "@element-plus/icons-vue";

const tableData = ref([
  {
    id: 1,
    title: "塗料與油墨",
    img:"https://fakeimg.pl/150x80/dddddd/",
    sort: "1",
  },
  {
    id: 2,
    title: "特用(工業)化學",
    img:"https://fakeimg.pl/150x80/dddddd/",
    sort: "3",
  },
  {
    id: 3,
    title: "塗料與油墨",
    img:"https://fakeimg.pl/150x80/dddddd/",
    sort: "2",
  },
  {
    id: 4,
    title: "特用(工業)化學",
    img:"https://fakeimg.pl/150x80/dddddd/",
    sort: "5",
  },
  {
    id: 5,
    title: "塗料與油墨",
    img:"https://fakeimg.pl/150x80/dddddd/",
    sort: "4",
  },
  {
    id: 6,
    title: "特用(工業)化學",
    img:"https://fakeimg.pl/150x80/dddddd/",
    sort: "6",
  },
]);

const initform = reactive({
  title: "",
  titleS: "",
});

const form = reactive({
  title: "Activate Your Life",
  titleS: "持續開拓產品和專有技術，獨特化學品製造商",
});


const searchText = ref('');

const showAddNewsDrawer = ref(false);
const showEditNewsDrawer = ref(false);

const loading = ref(false);

const deleteRow = (index) => {
  tableData.value.splice(index, 1)
}

</script>

<template>
  <div class="main">
    <!-- Breadcrumb: Start -->
    <div class="breadcrumb-section">
      <div>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item> 經銷系統 </el-breadcrumb-item>
          <el-breadcrumb-item><a href="/">產品列表</a></el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div>
        <el-button
          type="primary"
          :icon="Plus"
          @click="showAddNewsDrawer = true"
        >
          新增輪播
        </el-button>
      </div>
    </div>
    <!-- Breadcrumb: End -->    
    
    <el-card :body-style="{ padding: '0px' }">
      <el-table :data="tableData" style="width: 100%">  
        <el-table-column prop="id" label="#" width="50" />
        <el-table-column prop="title" label="圖片標題" />
        <el-table-column prop="img" label="代表圖片">
          <template #default="scope">
            <el-image style="width: 150px; height: 80px" :src="tableData[scope.$index].img" fit="cover" />
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" width="80">
          <template #default="scope">
            <el-input v-model="tableData[scope.$index].sort" :style="{ width: '40px' }"/>
          </template>
        </el-table-column>
        <el-table-column label="其他項目" width="105" fixed="right">
          <template #default="scope">            
            <el-dropdown>
              <el-button type="danger">
                操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item :icon="Edit" @click="showEditNewsDrawer = true">修改</el-dropdown-item>
                  <el-dropdown-item :icon="Delete" @click="deleteRow(scope.$index)">刪除</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>

  <div>
    <el-drawer
      ref="drawerRef"
      v-model="showAddNewsDrawer"
      title="新增輪播"
      direction="rtl"
      size="550"
    >
      <el-card>
        <el-form :model="initform" label-width="120px">          
          <el-form-item label="圖片標題">
            <el-input v-model="initform.title" />
          </el-form-item>
          <el-form-item label="代表圖片">
            <el-upload
              class="upload-demo"
              action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              list-type="picture"
            >
              <el-button type="primary">上傳圖片</el-button>
              <template #tip>
                <small class="el-upload__tip"> ※ 請上傳500kb以內jpg檔案。</small>
              </template>
            </el-upload>
          </el-form-item>
        </el-form>        
      </el-card>
      <div class="demo-drawer__footer" style="text-align: right">
        <el-button @click="showAddNewsDrawer = false">取消</el-button>
        <el-button type="primary" :loading="loading" @click="onClick">{{
          loading ? "Submitting ..." : "確認"
        }}</el-button>
      </div>
    </el-drawer>
  </div>

  <div>
    <el-drawer
      ref="drawerRef"
      v-model="showEditNewsDrawer"
      title="修改輪播"
      direction="rtl"
      size="550"
    >
      <el-card>
        <el-form :model="form" label-width="120px">          
          <el-form-item label="圖片標題">
            <el-input v-model="form.title" />
          </el-form-item>
          <el-form-item label="代表圖片">
            <el-upload
              class="upload-demo"
              action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              list-type="picture"
            >
              <el-button type="primary">上傳圖片</el-button>
              <template #tip>
                <small class="el-upload__tip">
                  請上傳500kb以內jpg檔案。
                </small>
              </template>
            </el-upload>
          </el-form-item>
        </el-form>
        
      </el-card>
      <div class="demo-drawer__footer" style="text-align: right">
        <el-button @click="showEditNewsDrawer = false">取消</el-button>
        <el-button type="primary" :loading="loading" @click="onClick">{{
          loading ? "Submitting ..." : "確認"
        }}</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<style lang="less" scoped>


</style>
