<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
      <el-form-item label="省" prop="provinceId">
        <el-select v-model="queryParams.provinceId" placeholder="请选择省" @change="getCityData" filterable style="width: 200px;">
          <el-option v-for="(item,index) in provinceDataList" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="市" prop="cityId">
        <el-select v-model="queryParams.cityId" placeholder="请选择市" @change="getDistrictData" filterable style="width: 200px;">
          <el-option v-for="(item, index) in cityDataList" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="区" prop="districtId">
        <el-select v-model="queryParams.districtId" placeholder="请选择区" filterable style="width: 200px;">
          <el-option v-for="(item, index) in districtDataList" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="登录名称" prop="userName">
        <el-input v-model="queryParams.userName" clearable style="width: 200px;" placeholder="请输入登录名称"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" filterable style="width: 200px;">
          <el-option label="启用" value="0"></el-option>
          <el-option label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <h3 class="toolbar">
      <span class="title">列表</span>
      <div class="tool">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </div>
    </h3>
    <el-table v-loading="loading" :data="list" border>
      <el-table-column label="账户" prop="nickName" align="center" />
      <el-table-column label="登录名称" prop="userName" align="center"></el-table-column>
      <el-table-column label="手机号" prop="phonenumber" align="center" width="150" />
      <el-table-column label="负责地区" prop="remark" align="center"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160"></el-table-column>
      <el-table-column label="状态" align="center" key="status">
        <template slot-scope="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" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          <el-button type="text" @click="handleResetPwd(scope.row)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
    <add-tax-user ref="addTax" v-if="addTaxVisible" @refreshData="getList()"></add-tax-user>
  </div>
</template>

<script>
  import {getProvinceDataApi,getRegionChildrenApi} from "@/api/common/common"
  import {entStateOptions, getLabel, authStateOptions} from '@/utils/dataFormat'
  import {listTaxAdminApi,deleteTaxApi} from '@/api/admin/tax/tax'
  import { changeUserStatus } from "@/api/system/user";
  import AddTaxUser from './components/AddTaxUser'
  import { resetUserPwd } from "@/api/system/user";
  export default {
    components: {
      AddTaxUser
    },
    data () {
      return {
        loading: false,
        addTaxVisible: false,
        type: '',
        provinceDataList: [],
        cityDataList: [],
        districtDataList: [],
        entStateOptions: entStateOptions,
        authStateOptions: authStateOptions,
        // 模板状态
        // statusOptions: accessStatus,
        dateRange: [],
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          provinceId: undefined,
          cityId: undefined,
          districtId: undefined,
          userName: undefined,
          status: undefined
        },
        list: [],
        total: 0,
        open: false,
        openInfo: false,
      }
    },
    created() {
      this.getList()
      this.getProvinceData()
    },
    methods: {
      //选择省份
      getCityData() {
        this.queryParams.cityId = ''
        this.queryParams.districtId = ''
        getRegionChildrenApi(this.queryParams.provinceId).then(res => {
          if(res.code == 200) {
            this.cityDataList = res.data
          }
        })
      },
      //选择市
      getDistrictData() {
        this.queryParams.districtId = ''
        getRegionChildrenApi(this.queryParams.cityId).then(res => {
          if(res.code == 200) {
            this.districtDataList = res.data
          }
        })
      },
      getProvinceData() {
        getProvinceDataApi().then(res => {
          if(res.code == 200) {
            this.provinceDataList = res.data
          }
        })
      },
      getList() {
        this.loading = true;
        listTaxAdminApi(this.queryParams).then(response => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      handleAdd() {
        this.type = 'add'
        this.addTaxVisible = true
        this.$nextTick(() => {
          this.$refs.addTax.init(this.type)
        })
      },
      handleUpdate(row) {
        this.type = 'update'
        this.addTaxVisible = true
        this.$nextTick(() => {
          this.$refs.addTax.init(this.type, row.userId)
        })
      },
      // 用户状态修改
      handleStatusChange(row) {
        let text = row.status === "0" ? "启用" : "停用";
        this.$modal.confirm('确认要"' + text + '""' + row.userName + '"用户吗？').then(function() {
          return changeUserStatus(row.userId, row.status);
        }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function() {
          row.status = row.status === "0" ? "1" : "0";
        });
      },
      /** 重置密码按钮操作 */
      handleResetPwd(row) {
        this.$prompt('请输入"' + row.userName + '"的新密码', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          closeOnClickModal: false,
          inputPattern: /^.{5,20}$/,
          inputErrorMessage: "用户密码长度必须介于 5 和 20 之间"
        }).then(({ value }) => {
            resetUserPwd(row.userId, value).then(response => {
              this.$modal.msgSuccess("修改成功，新密码是：" + value);
            });
          }).catch(() => {});
      },
      handleDelete(row) {
        const id = row.userId;
        this.$confirm('是否确认删除该数据?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return deleteTaxApi(id);
          }).then(() => {
            this.queryParams.pageNum = 1;
            this.getList();
            this.msgSuccess("删除成功");
          })
      },
      // 重置
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 搜索
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      statusFormat(row, col, val, index) {
        return getLabel(accessStatus, val)
      },
      entSateFormat(row, col, val, index) {
        return getLabel(entStateOptions, val)
      },
      authStateFormat(row, col, val, index) {
        return getLabel(authStateOptions, val)
      }
    }
  }
</script>

<style>
</style>
