<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="page.data">
      <el-form-item class="margin-left">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addOrUpdate('添加账号')"
          >添加账号</el-button
        >
      </el-form-item>
      <el-form-item class="margin-left input">
        <el-select
          v-model="page.data.username"
          placeholder="请选择账号"
          filterable
          clearable
        >
          <el-option
            v-for="item in option"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="margin-left input">
        <el-input
          v-model="page.data.nickname"
          placeholder="请输入姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item class="margin-left input">
        <el-input
          v-model="page.data.phone"
          placeholder="请输入手机号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item class="margin-left input">
        <el-input
          v-model="page.data.address"
          placeholder="请输入地址"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item class="margin-left">
        <el-button type="primary" @click="fetchData" icon="el-icon-search"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 主体表格 -->
    <el-table
      :data="tableData"
      v-loading="listLoading"
      border
      fit
      highlight-current-row
      class="table"
    >
      <el-table-column align="center" width="80" label="序号">
        <template slot-scope="scope">
          {{ indexMethod(scope.$index) }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="username" label="账号名称">
      </el-table-column>
      <el-table-column align="center" prop="nickname" label="管理员姓名">
      </el-table-column>
      <el-table-column align="center" prop="phone" label="管理员手机号">
      </el-table-column>
      <el-table-column label="详细地址" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.address | matchNull }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="createTime"
        label="创建时间"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column align="center" label="操作区" width="210">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="small"
            @click="addOrUpdate('编辑账号', row)"
            >编辑</el-button
          >
          <el-button type="danger" size="small" @click="handleDelete(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页栏-->
    <pagination
      :total="total"
      :page.sync="page.pageNumber"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />
    <!-- 添加/编辑账号 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="800px"
      :close-on-click-modal="false"
    >
      <el-form
        :model="addOrUpdateForm"
        :rules="rules"
        ref="formRef"
        label-position="left"
        label-width="120px"
      >
        <div class="flex">
          <el-form-item style="flex: 0.5" label="账号名称" prop="username">
            <el-input
              v-model="addOrUpdateForm.username"
              placeholder="请输入账号名称"
            ></el-input>
          </el-form-item>

          <el-form-item
            style="margin-left: 20px; flex: 0.5"
            label="账号密码"
            prop="password"
          >
            <el-input
              v-model="addOrUpdateForm.password"
              placeholder="请输入账号密码"
            ></el-input>
          </el-form-item>
        </div>

        <div class="flex">
          <el-form-item style="flex: 0.5" label="管理员姓名" prop="nickname">
            <el-input
              placeholder="请输入管理员姓名"
              v-model.trim="addOrUpdateForm.nickname"
            />
          </el-form-item>
          <el-form-item
            label="管理员手机号"
            style="margin-left: 20px; flex: 0.5"
            prop="phone"
          >
            <el-input
              placeholder="请输入管理员手机号"
              v-model.trim="addOrUpdateForm.phone"
              maxlength="11"
              show-word-limit
            />
          </el-form-item>
        </div>
        <!-- <div class="flex">
          <el-form-item label="省" prop="provinceId" label-width="120px">
            <el-select
              filterable
              v-model="addOrUpdateForm.provinceId"
              @change="chooseProvince"
              placeholder="请选择省份"
            >
              <el-option
                v-for="item in province_list"
                :key="item.areaCodeId"
                :label="item.name"
                :value="item.areaCodeId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="市"
            prop="cityId"
            label-width="50px"
            style="margin-left: 10px"
          >
            <el-select
              filterable
              v-model="addOrUpdateForm.cityId"
              @change="chooseCity"
              placeholder="请选择城市"
            >
              <el-option
                v-for="item in city_list"
                :key="item.areaCodeId"
                :label="item.name"
                :value="item.areaCodeId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="区/县"
            prop="areaId"
            label-width="80px"
            style="margin-left: 10px"
          >
            <el-select
              filterable
              v-model="addOrUpdateForm.areaId"
              @change="chooseArea"
              placeholder="请选择区/县"
            >
              <el-option
                v-for="item in area_list"
                :key="item.areaCodeId"
                :label="item.name"
                :value="item.areaCodeId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </div> -->
        <el-form-item label="详细地址" prop="address">
          <el-input
            v-model="addOrUpdateForm.address"
            placeholder="请输入公司详细地址"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import {
  getProvince,
  getCity,
  getArea,
  doGetAdmins,
  doEditAdmin,
  doDeleteAdmin,
  getUserlist,
} from "@/api/user";
import { checkRole } from "@/utils/checkRole";
export default {
  components: { Pagination },
  filters: {
    matchNull(str) {
      if (!str) {
        return "未填写";
      } else {
        return str;
      }
    },
  },
  data() {
    return {
      listLoading: false,
      submitLoading: false,
      dialogTitle: "",
      dialogVisible: false,
      total: 0,
      option: [],
      // 分页参数
      page: {
        data: {
          username: "",
          nickname: "",
          phone: "",
          address: "",
        },
        pageNumber: 1,
        pageSize: 10,
      },
      tableData: [],
      isEdit: false, //判断是否是编辑
      // 添加编辑账号
      addOrUpdateForm: {
        username: "", //账号名称
        password: "", //账号密码
        nickname: "", //管理员姓名
        // provinceId: null, //省
        // cityId: null, //市
        // areaId: null, //区
        phone: "", //手机号
        address: "", //详细地址
      },
      //省市县列表
      province_list: [],
      city_list: [],
      area_list: [],
      // 规则
      rules: {
        username: [
          { required: true, message: "请输入账号名称", trigger: "blur" },
        ],
        // password: [
        //   { required: true, message: "请输入账号密码", trigger: "blur" },
        // ],
        // provinceId: [{ required: true, message: "请选择省", trigger: "blur" }],
        // cityId: [{ required: true, message: "请选择城市", trigger: "blur" }],
        // areaId: [{ required: true, message: "请选择地区", trigger: "blur" }],
        // companyAddress: [
        //   { required: true, message: "请输入公司详细地址", trigger: "blur" },
        // ],
        nickname: [
          { required: true, message: "请输入管理员姓名", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入管理员手机号", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.fetchData();
    this.getUserList();
  },
  methods: {
    // 查询权限
    checkRole,
    // 获取列表数据
    fetchData() {
      this.listLoading = true;
      doGetAdmins(this.page).then((res) => {
        // console.log(res, 11111);
        this.tableData = res.data.records;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },
    getUserList() {
      getUserlist().then((res) => {
        this.option = res.data;
      });
    },
    // 添加编辑账号
    addOrUpdate(dialogTitle, edit) {
      // this.cloneFn();
      this.dialogVisible = true;
      this.dialogTitle = dialogTitle;
      this.$nextTick(() => {
        this.$refs.formRef.resetFields();
        if (edit) {
          // 修改
          // console.log(edit);
          // this.getProvince();
          // this.getCity(edit.provinceId);
          // this.getArea(edit.cityId);
          this.addOrUpdateForm = JSON.parse(JSON.stringify(edit));
          this.isEdit = true;
        } else {
          // 添加
          // this.getProvince();
          this.isEdit = false;
        }
      });
    },
    // 确认按钮
    submit() {
      this.$refs.formRef.validate(async (pass) => {
        if (!pass) {
          return false;
        }
        await doEditAdmin({
          ...this.addOrUpdateForm,
          id: this.isEdit ? this.addOrUpdateForm.id : undefined,
        });
        this.$message.success(`${this.dialogTitle}成功`);
        this.submitLoading = false;
        this.dialogVisible = false;
        this.fetchData();
      });
    },
    // 删除账号
    handleDelete(id) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(() => {
        doDeleteAdmin(id).then((res) => {
          this.fetchData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    // 省
    // getProvince() {
    //   getProvince().then((res) => {
    //     this.province_list = res.data;
    //   });
    // },
    // // 市
    // getCity(e) {
    //   // console.log(e);
    //   getCity({ areaCodeId: e }).then((res) => {
    //     this.city_list = res.data;
    //   });
    // },
    // // 区
    // getArea(e) {
    //   getArea({ areaCodeId: e }).then((res) => {
    //     // console.log(res);
    //     this.area_list = res.data;
    //   });
    // },
    // // 选择省
    // chooseProvince(e) {
    //   this.addOrUpdateForm.cityId = null;
    //   this.city_list = [];
    //   this.addOrUpdateForm.areaId = null;
    //   //this.$set(this.addOrUpdateForm,'areaId','')
    //   this.area_list = [];
    //   // console.log(e);
    //   if (e) {
    //     this.getCity(e);
    //   }
    // },
    // // 选择市
    // chooseCity(e) {
    //   console.log(e);
    //   this.addOrUpdateForm.areaId = null;
    //   this.area_list = [];
    //   if (e) {
    //     this.getArea(e);
    //   }
    // },
    // // 选择区/县
    // chooseArea(e) {
    //   console.log(e);
    //   // console.log(this.addOrUpdateForm.areaId);
    // },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNumber - 1) * this.page.pageSize + index + 1;
    },
  },
};
</script>

<style scoped>
.margin-left {
  margin: 10px 0 0 10px;
}
.input {
  width: 150px;
}
.flex {
  width: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
</style>
