<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="项目标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入项目标题"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="项目类型" prop="itemType">
        <el-select v-model="queryParams.itemType" style="width: 240px">
          <el-option
            v-for="dict in jc_item_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="后台关闭" prop="isClose">
        <el-select v-model="queryParams.isClose" style="width: 240px">
          <el-option
            v-for="dict in sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <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="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handlerDel"
          >删除</el-button
        >
      </el-col>
    </el-row>
    <!-- v-loading="loading" -->
    <el-table :data="roleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目标题" prop="title" align="center" />
      <el-table-column label="项目类型" prop="itemType" align="center">
        <template #default="scope">
          <dict-tag :options="jc_item_type" :value="scope.row.itemType" />
        </template>
      </el-table-column>
      <el-table-column label="后台关闭" prop="isClose" align="center">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.isClose" />
        </template>
      </el-table-column>
      <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="handlerUpdate(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handlerDel(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="getListData"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" width="800px">
      <el-form
        ref="ruleFormRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目类型" prop="itemType">
              <el-select v-model="form.itemType">
                <el-option
                  v-for="dict in jc_item_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入项目标题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="技术点" prop="technology">
              <el-select v-model="form.technology" multiple collapse-tags>
                <el-option
                  v-for="dict in jc_technology"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="后台关闭" prop="isClose">
              <el-select v-model="form.isClose">
                <el-option
                  v-for="dict in sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item
          label="项目集合(例如：xxx项目的APP/pc/大屏端)"
          class="block-form"
          label-width="300px"
          prop="itemJson"
        >
          <FormTable v-model="form.itemJson"></FormTable>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDataScope">提 交</el-button>
          <el-button @click="cancelDataScope">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import FormTable from "./table.vue";
import { getList, add, del, updata, getInfo } from "@/api/jichuang/model.js";
const { proxy } = getCurrentInstance();
const { jc_technology, jc_item_type, sys_yes_no } = proxy.useDict(
  "jc_technology",
  "jc_item_type",
  "sys_yes_no"
);
const showSearch = ref(true);

const multiple = ref(true);
const loading = ref(true);
const roleList = ref([]);
const ids = ref([]);
const title = ref("");
const open = ref(false);
const total = ref(0);
const data = reactive({
  form: {
    id: null,
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    itemType: [
      {
        required: true,
        message: "请选择项目类型",
        trigger: "change",
      },
    ],
    title: [
      {
        required: true,
        message: "请输入项目标题",
        trigger: "blur",
      },
    ],
    technology: [
      {
        required: true,
        message: "请选择技术点",
        trigger: "change",
      },
    ],
    isClose: [
      {
        required: true,
        message: "请选择后台是否关闭",
        trigger: "change",
      },
    ],
    itemJson: [
      {
        required: true,
        message: "请输入项目集合",
        trigger: "change",
      },
    ],
  },
});
const { queryParams, form, rules } = toRefs(data);

onMounted(() => {
  getListData();
});
// 新增
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "新增案例";
};
//点击修改
const handlerUpdate = async (row) => {
  reset();
  //获取详情
  const info = await getInfo(row.id);
  title.value = "修改案例";
  info.data.technology = info.data.technology.split(",");
  info.data.itemJson = JSON.parse(info.data.itemJson);
  form.value = info.data;
  open.value = true;
};
//点击删除
const handlerDel = (row) => {
  const idlist = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除编号为"' + idlist + '"的数据项?')
    .then(function () {
      return del(idlist);
    })
    .then(() => {
      getListData();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};
//关闭
const cancelDataScope = () => {
  open.value = false;
};

const getListData = async () => {
  loading.value = true;
  const response = await getList(queryParams.value);
  roleList.value = response.rows;
  total.value = response.total;
  loading.value = false;
};

//提交
const submitDataScope = async () => {
  proxy.$refs["ruleFormRef"].validate(async (valid) => {
    form.value.technology = form.value.technology.toString();
    form.value.itemJson = JSON.stringify(form.value.itemJson);
    if (valid) {
      if (form.value.id) {
        await updata(form.value);
        try {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getListData();
        } catch {
          form.value.technology = form.value.technology.split(",");
          form.value.itemJson = JSON.parse(form.value.itemJson);
        }
      } else {
        await add(form.value);
        try {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getListData();
        } catch {
          form.value.technology = form.value.technology.split(",");
          form.value.itemJson = JSON.parse(form.value.itemJson);
        }
      }
    }
  });
};

function handleQuery() {
  queryParams.value.pageNum = 1;
  getListData();
}
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
function reset() {
  form.value = {
    itemJson: [],
    technology: [],
  };
  proxy.resetForm("ruleFormRef");
}

function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  multiple.value = !selection.length;
}
</script>
<style scoped lang="scss">
.block-form {
  display: flex;
  flex-direction: column;
}
</style>
