<template>
  <!-- 面包屑 -->
  <el-breadcrumb>
    <el-breadcrumb-item>专业优质服务管理</el-breadcrumb-item>
    <el-breadcrumb-item to="/professional_service/list">专业优质服务列表</el-breadcrumb-item>
  </el-breadcrumb>
  <!-- 搜索 -->
  <el-form inline :model="params">


    <el-form-item :label-width="labelWidth" label="语言类型" prop="i18n_lang_type">
      <el-select style="width:150px" v-model="params.i18n_lang_type_guid" clearable placeholder="请选择">
        <el-option v-for="item in i18n_lang_type" :key="item.i18n_lang_type_guid" :label="item.i18n_lang_type_code"
          :value="item.i18n_lang_type_guid"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="标题">
      <el-input v-model='params.professional_service_title' placeholder='请输入标题'></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="tableRef.reload()" icon="ElIconSearch">
        搜索
      </el-button>
    </el-form-item>
  </el-form>
  <el-space style="margin-bottom: 10px;">
    <!-- 添加专业优质服务 -->
    <el-col :span="1">
      <el-button type="primary" @click="addProfessionalServiceDialogVisible = true"> 添加 </el-button>
    </el-col>



    <!-- 下拉操作 -->
    <el-dropdown v-if="selectionData.length">
      <el-button type="primary">
        批量操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
      </el-button>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item @click="handleDelete(selectionData)">
            批量删除
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </el-space>
  <!-- 数据表格 -->
  <DataTable ref="tableRef" style="width: 100%" :onSelectionChange="data => (selectionData = data)" :column="column"
    :params="params" :request="params => getProfessionalServiceList(params)">

    <!-- 排序 -->
    <template #professional_service_sort='scope'>
      <el-input-number :disabled='loading' v-model='scope.row.professional_service_sort' :min='1'
        controls-position='right' @change='handleEditOrder(scope.row)'></el-input-number>
    </template>

    <!-- 图标 -->
    <template #professional_service_icon_name='scope'>
      <svg-icon v-if="scope.row.professional_service_icon_name" :name="scope.row.professional_service_icon_name" />
    </template>

    <template #i18n_lang_type_code="scope">
      <el-tag type="primary">{{ scope.row.i18n_lang_type_code }}</el-tag>
    </template>

    <template #chaoz="scope">
      <el-space>
        <el-button size="small" @click="handleUpdate(scope.row)">
          编辑
        </el-button>
        <el-dropdown @command="handleCommand">
          <el-button type="primary" size="small">
            更多<el-icon class="el-icon--right"><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item :command="{ type: 'detail', row: scope.row }">
                详情
              </el-dropdown-item>
              <el-dropdown-item :command="{ type: 'delete', row: scope.row }">
                删除
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-space>
    </template>
  </DataTable>

  <!-- 添加专业优质服务 -->
  <AddProfessionalServiceDialog v-model="addProfessionalServiceDialogVisible" :done="() => tableRef.reload()">
  </AddProfessionalServiceDialog>
  <!-- 编辑专业优质服务 -->
  <EditProfessionalServiceDialog v-model="EditProfessionalServiceDialogVisible" :data="EditProfessionalServiceDialogRow"
    :done="() => tableRef.reload()"></EditProfessionalServiceDialog>
  <!-- 专业优质服务详情 -->
  <DetailProfessionalServiceDialog v-model="DetailProfessionalServiceDialogVisible"
    :data="DetailProfessionalServiceDialogRow"></DetailProfessionalServiceDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive, watch } from 'vue';
import { useLoginStore } from "~/store";
import { getProfessionalServiceList, editProfessionalService, deleteProfessionalService } from '~/service/professional_service';
import { getI18nLangTypeList } from '~/service/i18n_lang_type';
import AddProfessionalServiceDialog from './components/AddProfessionalServiceDialog.vue';
import EditProfessionalServiceDialog from './components/EditProfessionalServiceDialog.vue';
import DetailProfessionalServiceDialog from './components/DetailProfessionalServiceDialog.vue';
import SvgIcon from '~/components/SvgIcon/index.vue'

const tableRef = ref();
const selectionData = ref([]);
const store = useLoginStore();

const addProfessionalServiceDialogVisible = ref(false);
const EditProfessionalServiceDialogVisible = ref(false);
const EditProfessionalServiceDialogRow = ref({});
const DetailProfessionalServiceDialogVisible = ref(false);
const DetailProfessionalServiceDialogRow = ref({});

const headers = {
  Accept: "application/json",
  ...store.headers,
};

// 查询参数
const params = reactive({
  professional_service_title: "",
  i18n_lang_type_guid: ""
});
const column = [

  {
    fixed: true,
    type: 'selection'
  },
  {
    prop: "i18n_lang_type_code",
    label: '语言类型code',
    width: '150'
  },
  {
    prop: "professional_service_icon_name",
    label: '图标',
    width: '150'
  },
  {
    prop: "professional_service_title",
    label: '标题',
    width: '150'
  },
  {
    prop: "professional_service_describe",
    label: '描述',
  },
  {
    prop: "professional_service_link",
    label: '跳转链接',
    width: '250'
  },
  {
    prop: "professional_service_sort",
    label: '排序',
  },
  {
    label: '操作',
    prop: 'chaoz',
    width: '250',
    fixed: 'right'
  }
];


const handleCommand = ({ type, row }) => {
  switch (type) {
    case "detail":
      handleDetail(row);
      break;
    case 'delete':
      handleDelete([row]);
      break;
  }
};

// 删除数据
const handleDelete = data => {
  ElMessageBox.confirm(`您确定要删除该专业优质服务吗？`).then(async () => {
    const res = await deleteProfessionalService({
      professional_service_guid: data.map(v => v.professional_service_guid).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditProfessionalServiceDialogVisible.value = true
  EditProfessionalServiceDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailProfessionalServiceDialogVisible.value = true
  DetailProfessionalServiceDialogRow.value = row
}



//排序
const loading = ref(false)
async function handleEditOrder(data) {
  loading.value = true
  const { code } = await editProfessionalService(data);
  if (code == 0) {
    loading.value = false
    tableRef.value.reload()
  }
}

const i18n_lang_type = ref();
async function geti18n_lang_typeList() {
  getI18nLangTypeList({ limit: 9999 }).then((res) => {
    if (res.code == 0) {
      i18n_lang_type.value = res.data
    }
  })
}
geti18n_lang_typeList()


</script>
