<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" label-width="100px">
      <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="企业名称">
        <el-input v-model="queryParams.entName" clearable style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item label="社会信用代码">
        <el-input v-model="queryParams.license" style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item label="服务状态">
        <el-select v-model="queryParams.entState" style="width: 200px;">
          <el-option v-for="(item, index) in entStateOptions" :key="index" :label="item.dictLabel" :value="item.dictValue"></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="userName" width="120" align="center" />
      <el-table-column label="企业名称" prop="entName" align="center" width="120"></el-table-column>
      <el-table-column label="省" prop="provinceName" align="center" width="120"/>
      <el-table-column label="市" prop="cityName" align="center" width="120" />
      <el-table-column label="区" prop="districtName" align="center" width="120" />
      <el-table-column label="社会信用代码" prop="license" align="center" width="120"></el-table-column>
      <el-table-column label="联系人" align="center" prop="linkName" width="150"></el-table-column>
      <el-table-column label="联系人手机号" prop="linkMobile" align="center" width="120"></el-table-column>
      <el-table-column label="联系人邮箱" prop="linkEmail" align="center" width="120"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center" width="160"></el-table-column>
      <el-table-column label="服务时间" align="center" width="120">
        <template slot-scope="scope">
          <template v-if="scope.row.entState == '1'">
            <span>--</span>
          </template>
          <template v-else-if="scope.row.entState == '2'">
            <span>{{scope.row.startDate}}-{{scope.row.endDate}}</span>
          </template>
          <template v-else>
            <span>已到期</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="服务状态" prop="entState" align="center" width="120" :formatter="entSateFormat"></el-table-column>
      <el-table-column label="审核状态" prop="authState" align="center" width="120" :formatter="authStateFormat"></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="handleView(scope.row)">查看</el-button>
          <el-button type="text" @click="handleRePwd(scope.row)" v-if="scope.row.entState == '3'">重置密码</el-button>
          <el-button type="text" @click="handleAudit(scope.row)" v-if="scope.row.authState == '1'">审核</el-button>
          <template v-if="scope.row.authState == '2'">
            <el-button type="text" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button type="text" @click="handleStop(scope.row)">停用</el-button>
            <el-button type="text" @click="handleRemind(scope.row)">提醒</el-button>
          </template>
          <template v-if="scope.row.authState == '2' || scope.row.authState == '1'">
            <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

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

<script>
  import {getProvinceDataApi,getRegionChildrenApi} from "@/api/common/common"
  import {listEntApi, deleteEntApi, resetPwdApi, remindUserApi, stopEntUserApi} from '@/api/admin/ent/ent'
  import {entStateOptions, getLabel, authStateOptions} from '@/utils/dataFormat'
  export default {
    data () {
      return {
        loading: false,
        provinceDataList: [],
        cityDataList: [],
        districtDataList: [],
        entStateOptions: entStateOptions,
        authStateOptions: authStateOptions,
        dateRange: [],
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          provinceId: undefined,
          cityId: undefined,
          districtId: 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;
        listEntApi(this.queryParams).then(response => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      handleAdd() {
        this.$router.push("/ent/add"+'?type=add')
      },
      handleUpdate(row) {
        this.$router.push("/ent/edit/"+row.id+'?type=update')
      },
      // 审核
      handleAudit(row) {
        this.$router.push("/ent/info/"+row.id+'?type=audit')
      },
      // 查看
      handleView(row) {
        this.$router.push("/ent/info/"+row.id+'?type=view')
      },
      //重置密码
      handleRePwd(row) {
        this.$confirm('是否确认重置密码?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return resetPwdApi(row.id);
          }).then(() => {
            this.queryParams.pageNum = 1;
            this.getList();
            this.$message({
              message: '密码重置成功',
              type: 'success'
            });
          })
      },
      handleRemind(row) {
        this.$confirm('是否确认提醒用户服务时间到期?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return remindUserApi(row.id);
          }).then(() => {
            this.queryParams.pageNum = 1;
            this.getList();
            this.$message({
              message: '提醒成功',
              type: 'success'
            });
          })
      },
      handleStop(row) {
        this.$confirm('是否确认停用该企业账号?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return stopEntUserApi(row.id);
          }).then(() => {
            this.queryParams.pageNum = 1;
            this.getList();
            this.$message({
              message: '停用成功',
              type: 'success'
            });
          })
      },
      handleDelete(row) {
        const id = row.id;
        this.$confirm('是否确认删除该数据?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return deleteEntApi(id);
          }).then(() => {
            this.queryParams.pageNum = 1;
            this.getList();
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          })
      },
      // 重置
      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>
