<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="姓名">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入姓名"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员类型">
        <el-select
          v-model="queryParams.role"
          placeholder="人员类型"
          clearable
          style="width: 200px"
        >
          <el-option label="维修人员" value="supporter" />
          <el-option label="客户角色" value="customer" />
        </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"
          v-hasPermi="['system:notice:add']"
          >新增</el-button
        >
      </el-col>

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

    <el-table v-loading="loading" :data="noticeList">
      <el-table-column label="序号" align="center" type="index" width="80" />
      <el-table-column label="手机号" align="center" prop="phonenumber">
      </el-table-column>
      <el-table-column label="姓名" :show-overflow-tooltip="true">
        <template #default="scope">
          {{ scope.row.nickName }}
        </template>
      </el-table-column>
      <el-table-column label="区域" :show-overflow-tooltip="true">
        <template #default="scope">
          {{ scope.row.provinceName }}--{{ scope.row.cityName }}--{{
            scope.row.areaName
          }}
        </template>
      </el-table-column>

      <el-table-column label="人员类型" align="center" prop="role" width="100">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.role == 'customer'"
            >客户角色</el-tag
          >
          <el-tag type="success" v-else>维修人员</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建者" align="center" prop="createBy" width="100" /> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="100"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:notice:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:notice:remove']"
            >删除</el-button
          >
        </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"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-form :model="form" :rules="rules" ref="userRef" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户姓名" prop="nickName">
              <el-input
                v-model="form.nickName"
                placeholder="请输入用户昵称"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input
                v-model="form.phonenumber"
                placeholder="请输入手机号码"
                maxlength="11"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="区域" prop="phonenumber">
              <el-cascader
                ref="myCascader"
                v-model="addArea"
                :options="options"
                style="width: 100%"
                @change="handleChange"
                :props="cityProps"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="用户性别">
              <el-select v-model="form.sex" placeholder="请选择">
                <el-option
                  v-for="dict in sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-form-item label="角色">
            <el-select v-model="form.role" placeholder="请选择">
              <el-option
                v-for="item in roleOptions"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="邮箱">
              <el-input
                v-model="form.email"
                placeholder="请输入邮箱"
                maxlength="50"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in sys_normal_disable"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.role=='supporter'">
          <el-col :span="12">
            <el-form-item label="身份证号" prop="identityNumber">
              <el-input
                v-model="form.identityNumber"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行卡号" prop="bankCardNumber">
              <el-input
                v-model="form.bankCardNumber"
                placeholder="请输入"
                maxlength="11"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户行" prop="bankName">
              <el-input
                v-model="form.bankName"
                placeholder="请输入"
                maxlength="11"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </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="Notice">
import { wxuserList, update, remove, add } from "@/api/aftersales/afteruser";
import cityData from "./city.json";
console.log();
const { proxy } = getCurrentInstance();
const { sys_user_sex, sys_normal_disable } = proxy.useDict(
  "sys_user_sex",
  "sys_normal_disable"
);

const noticeList = 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 options = ref(cityData);
const cityProps = ref({
  value: "code",
  label: "name",
  children: "children",
});
const roleOptions = ref([
  {
    roleId: "customer",
    roleName: "客户角色",
  },
  {
    roleId: "supporter",
    roleName: "维修人员",
  },
]);
const addArea = ref();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    noticeTitle: undefined,
    createBy: undefined,
    status: undefined,
  },
  rules: {
    noticeTitle: [
      { required: true, message: "公告标题不能为空", trigger: "blur" },
    ],
    noticeType: [
      { required: true, message: "公告类型不能为空", trigger: "change" },
    ],
  },
});

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

/** 查询公告列表 */
function getList() {
  loading.value = true;
  wxuserList(queryParams.value).then((response) => {
    noticeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    userId: undefined,

    status: "0",
  };
  proxy.resetForm("userRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
function handleChange(value, e) {
  console.log();
  let pathLabels = proxy.$refs.myCascader.getCheckedNodes()[0].pathLabels;
  console.log(value, e);
  form.value.provinceCode = value[0] + "0000";
  form.value.cityCode = value[1] + "00";
  form.value.areaCode = value[2];
  form.value.provinceName = pathLabels[0];
  form.value.cityName = pathLabels[1];
  form.value.areaName = pathLabels[2];
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  addArea.value = [];
  title.value = "添加用户";
}
/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  console.log(row);
  form.value = Object.assign({}, row);
  open.value = true;

  title.value = row.nickName;
  addArea.value = [
    row.provinceCode.toString().substr(0, 2),
    row.cityCode.toString().substr(0, 4),
    row.areaCode.toString(),
  ];
  console.log(addArea.value);
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["userRef"].validate((valid) => {
    if (valid) {
      if (form.value.userId != undefined) {
        update(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        add(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const userId = row.userId;
  proxy.$modal
    .confirm('是否确认用户为"' + row.nickName + '"的数据项？')
    .then(function () {
      return remove(userId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
