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

import AscForm from "./components/Form.vue";

import { getBranchList, updateBranch, deleteBranch } from "@/api/branch.js";

const state = reactive({
  page: 1,
  pageSize: 10,
});

const { page, pageSize } = toRefs(state);
const total = ref(0);
const loading = ref(false);

const showFormDraw = ref(false);

const tableData = ref([]);

onMounted(() => {
  getTableData();
});

const getTableData = async () => {
  loading.value = true;
  let res = await getBranchList(state);
  if (res.code === 200) {
    tableData.value = res.data;
  }
  loading.value = false;
};

const handleAdd = () => {
  editid.value = 0;
  showFormDraw.value = true;
};

const handleEdit = (id) => {
  editid.value = id;
  showFormDraw.value = true;
};

const handleDelete = (id) => {
  ElMessageBox.confirm('確認刪除此紀錄?')
    .then(async () => {
      let res = await deleteBranch(id);
      if (res.code === 200) {
        ElMessage({
          message: '刪除成功.',
          type: 'success',
        });
        getTableData();

      }else{
        ElMessage({
          message: '刪除失敗.',
          type: 'error',
        });
      }
    })
    .catch(() => {
    })
};

// Pagination
const handleSizeChange = (val) => {
  state.pageSize = val;
  getTableData();
};

const handleCurrentChange = (val) => {
  state.page = val;
  getTableData();
};

const handlePrevClick = (val) => {
  state.page = val;
  getTableData();
};

const handleNextClick = (val) => {
  state.page = val;
  getTableData();
};


// 編輯排序
const editOrderId = ref(0);

const handleEditItem = (id) => {
  editOrderId.value = id;
}

const handleUpdateItem = async (row) => {

  let res = await updateBranch(row);
  if (res.code === 200) {
    ElMessage({
      message: '修改成功.',
      type: 'success',
    });
    getTableData();
  } else {
    ElMessage({
      message: '修改失敗.',
      type: 'error',
    });
  }
  editOrderId.value = 0;
}

const handleCloseEditItem = () => {
  editOrderId.value = 0;
}

// Drawer

const editid = ref(0);

const handleCloseFormDraw = (val) => {

  showFormDraw.value = false;
  if(val){
    getTableData();
  }
};
</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="handleAdd"
        >
          新增據點
        </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="name" label="據點名稱" />
        <el-table-column prop="address" label="據點地址" />
        <el-table-column prop="tel" label="電話" />
        <el-table-column prop="fax" label="傳真" />
        <el-table-column prop="status" label="是否有效" />
        <el-table-column prop="sort" label="排序" width="80">
          <template #default="scope">
            <div @dblclick="handleEditItem(scope.row.id)" v-if="scope.row.id !== editOrderId">
              {{ scope.row.sort_order }}
            </div>
            <el-input v-model="scope.row.sort_order" :style="{ width: '50px' }" 
            @keyup.enter="handleUpdateItem(scope.row)" 
            @blur="handleCloseEditItem()"
            :autoFocus="true"
            v-focus="scope.row.id === editOrderId"
            v-else />
          </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="handleEdit(scope.row.id)">修改</el-dropdown-item>
                  <el-dropdown-item :icon="Delete" @click="handleDelete(scope.row.id)">刪除</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="showFormDraw" 
      :destroy-on-close="true" 
      title="公司據點" 
      direction="rtl" 
      size="50%">
      <asc-form :editid="editid" @close="handleCloseFormDraw"/>
    </el-drawer>
  </div>
</template>

<style lang="less" scoped>


</style>
