<template>
  <div class="app-container">
    <div ref="topContainerRef">
        <TableSearch
            :queryParams="queryParams"
            :showSearch="showSearch"
            label-width="90px"
            queryRef="queryRef"
            @refresh="resetQuery"
            @search="handleQuery"
        >
            <template #one>
                <el-col :lg="6" :md="12" :sm="24" :xl="6" :xs="24" :xxl="6">
                    <el-form-item label="手机名称" prop="phoneName">
                        <el-input
                        v-model="queryParams.phoneName"
                        placeholder="请输入手机名称"
                        clearable
                        style="width: 240px"
                        @keyup.enter="handleQuery"
                        />
                    </el-form-item>
                </el-col>
                <el-col :lg="6" :md="12" :sm="24" :xl="6" :xs="24" :xxl="6">
                    <el-form-item label="手机型号" prop="phoneModel">
                        <el-input
                        v-model="queryParams.phoneModel"
                        placeholder="请输入手机型号"
                        clearable
                        style="width: 240px"
                        @keyup.enter="handleQuery"
                        />
                    </el-form-item>
                </el-col>
                <el-col :lg="6" :md="12" :sm="24" :xl="6" :xs="24" :xxl="6">
                    <el-form-item label="厂商" prop="manufacturer">
                        <el-input
                        v-model="queryParams.manufacturer"
                        placeholder="请输入厂商"
                        clearable
                        style="width: 240px"
                        @keyup.enter="handleQuery"
                        />
                    </el-form-item>
                </el-col>
            </template>
        </TableSearch>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                type="success"
                plain
                icon="Edit"
                :disabled="single"
                @click="handleUpdate"
                >修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                type="danger"
                plain
                icon="Delete"
                :disabled="multiple"
                @click="handleDelete"
                >删除</el-button>
            </el-col>

        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </div>

    <div :style="containerHeight">
      <!-- 表格数据 -->
      <el-table height="100%" v-loading="loading" :data="tableDataList" @selection-change="handleSelectionChange">
         <el-table-column type="selection" width="55" align="center" />
         <el-table-column label="手机名称" prop="phoneName" :show-overflow-tooltip="true" width="180"/>
         <el-table-column label="手机型号" prop="phoneModel" />
         <el-table-column label="系统版本" prop="osVersion"  />
         <el-table-column label="操作系统" prop="osSystem" width="100" />
         <el-table-column label="厂商" align="center" prop="manufacturer" />
         <el-table-column label="额外参数" align="center" prop="specifications" :show-overflow-tooltip="true" width="180"/>
         <el-table-column label="状态" align="center" width="100">
            <template #default="scope">
               <el-switch
                  v-model="scope.row.status"
                  active-value="0"
                  inactive-value="1"
                  @change="handleStatusChange(scope.row)"
               ></el-switch>
            </template>
         </el-table-column>
         <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" width="180"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-tooltip content="修改" placement="top">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
              </el-tooltip>
            </template>
         </el-table-column>
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />

    </div>

      <!-- 添加或修改配置对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
         <el-form ref="phoneRef" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="手机名称" prop="phoneName">
               <el-input v-model="form.phoneName" placeholder="请输入手机名称" />
            </el-form-item>
            <el-form-item label="手机型号" prop="phoneModel">
               <el-input v-model="form.phoneModel" placeholder="请输入手机型号" />
            </el-form-item>
            <el-form-item label="操作系统" prop="osSystem">
                <el-select v-model="form.osSystem">
                    <el-option  label="Android" value="Android" />
                    <el-option  label="iOS" value="iOS" />
               </el-select>
            </el-form-item>
            <el-form-item label="系统版本" prop="osVersion">
               <el-input v-model="form.osVersion" placeholder="请输入系统版本" />
            </el-form-item>
            <el-form-item label="厂商" prop="manufacturer">
               <el-input v-model="form.manufacturer" />
            </el-form-item>
            <el-form-item label="规格参数" prop="specifications">
               <el-input v-model="form.specifications" />
            </el-form-item>
            <el-form-item label="备注">
               <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
      </el-dialog>

   </div>
</template>

<script setup name="Role">
import TableSearch from "@/components/TableSearch/index.vue";
import { useContainerHeight } from "@/hooks/tableHeight";
import { addPhone, getPhoneList, updatePhone, deletePhone } from "@/api/intelligenceTest/mobilePhone.js"
import { ElMessage, ElMessageBox } from 'element-plus'

const topContainerRef = ref();
const containerHeight = useContainerHeight(topContainerRef);

const tableDataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    phoneName: undefined,
    phoneModel: undefined,
    manufacturer: undefined
  },
  rules: {
    phoneName: [{ required: true, message: "手机名称不能为空", trigger: "blur" }],
    phoneModel: [{ required: true, message: "手机型号不能为空", trigger: "blur" }],
    manufacturer: [{ required: true, message: "厂商不能为空", trigger: "blur" }],
    osSystem: [{ required: true, message: "操作系统不能为空", trigger: "blur" }],
    osVersion: [{ required: true, message: "系统版本不能为空", trigger: "blur" }]
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
const getList = async () =>  {
  loading.value = true;
  const res = await getPhoneList(queryParams.value)
  if (res.code === 200) {
    tableDataList.value = res.rows;
    total.value = res.total;
  }
  loading.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(row) {
    const deleteIds = row.id || ids.value;
    ElMessageBox.confirm(`确定要删除编号为${deleteIds}的手机吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
    }).then(() => {
        deletePhone(deleteIds).then(() => {
        ElMessage.success("删除成功");
        getList();
        });
     });
}


/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 状态修改 */
function handleStatusChange(row) {
  let text = row.status === "0" ? "启用" : "停用";
  ElMessageBox.confirm('确认要"' + text + '""' + row.phoneName + '"手机吗?', "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    updatePhone({
        ...row,
    }).then(() => {
      ElMessage.success(text + "成功");
      getList();
    });
  }).catch(() => {
    row.status = row.status === "0" ? "1" : "0";
  })
}

const reset = () => {
    phoneRef.value.resetFields()
};

const phoneRef = ref()
const isNew = ref(true)

/** 添加 */
function handleAdd() {
  open.value = true;
  isNew.value = true
  title.value = "添加";
}

/** 修改 */
function handleUpdate(row) {
  form.value = { ...row };
  open.value = true;
  isNew.value = false
  title.value = "修改";
}



/** 提交按钮 */
function submitForm() {
  phoneRef.value.validate((valid) => {
    if (valid) {
      if (isNew.value) {
        addPhone(form.value).then(() => {
          ElMessage.success("添加成功");
          getList();
          open.value = false;
          reset();
        });
      } else {
        deleteVehicle(form.value).then(() => {
          ElMessage.success("修改成功");
          getList();
          open.value = false;
          reset();
        });
      }
    }
  }); 
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

getList();
</script>
