<template>
  <div class="app-container">
    <el-card shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <div style="float:right">
          <el-button type="primary" @click="handleSearchList" size="small">
            查询
          </el-button>
        </div>
      </div>
      <div style="margin-top: 15px">
        <el-form
          size="small"
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item label="企业名称">
            <el-input v-model="queryParams.companyName" placeholder="请输入" />
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button class="btn-add" @click="handlePage(null)" size="mini">
        添加企业
      </el-button>
    </el-card>
    <el-table
      style="width: 100%"
      class="table-container"
      :data="unscrambleList"
    >
      <el-table-column
        label="序号"
        align="center"
        type="index"
      ></el-table-column>
      <el-table-column label="企业全称" prop="companyName" align="center">
        <template slot-scope="scope">
          <el-link type="primary" :underline="false">{{
            scope.row.companyName
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="企业地址" align="center" prop="cityType">
        <template slot-scope="scope">
          {{
            optionList1.filter(item => item.value == scope.row.addressType)[0]
              .label
          }}——{{ scope.row.cityName }}
        </template>
      </el-table-column>
      <el-table-column
        label="登录手机号"
        align="center"
        prop="username"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button @click="handlePage(scope.row.id)" type="text" size="small"
            >编辑</el-button
          >
          <el-button
            @click="handleStatus(scope.row.id, false)"
            type="text"
            size="small"
            v-if="scope.row.deleteStatus != 1"
            >禁用</el-button
          >
          <el-button
            @click="handleStatus(scope.row.id, true)"
            type="text"
            size="small"
            v-else
            >启用</el-button
          >
          <el-button
            @click="handleDelete(scope.row.id)"
            type="text"
            size="small"
            >删除</el-button
          >
          <el-button
            @click="resetPwd(scope.row.userId)"
            type="text"
            size="small"
            >重置密码</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import {
  getCategoryList,
  handleCompanyUser,
  deleteCompanyUser,
  resetPw
} from '@/api/customer';
export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 100
      },
      unscrambleList: [],
      optionList1: [
        {
          value: '1',
          label: '地级市'
        },
        {
          value: '2',
          label: '合肥区县'
        }
      ]
    };
  },
  methods: {
    handleSearchList() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      getCategoryList(this.queryParams).then(({ data }) => {
        this.unscrambleList = data.list;
      });
    },
    handleStatus(companyId, isOpen) {
      handleCompanyUser({ companyId, isOpen }).then(({ message }) => {
        this.msgSuccess(message);
        this.getList();
      });
    },
    // 跳转页面
    handlePage(id) {
      this.$router.push({ path: '/addCustomer', query: { id } });
    },
    /** 删除按钮操作 */
    handleDelete(companyId) {
      this.$confirm('确认删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return deleteCompanyUser({ companyId });
        })
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        });
    },
    // 重置密码
    resetPwd(userId) {
      // resetPw
      this.$prompt('请输入新密码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
        // inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
        // inputErrorMessage: '邮箱格式不正确'
      })
        .then(({ value }) => {
          resetPw({ userId, password: value }).then(({ data }) => {
            this.msgSuccess(data);
          });
        })
        .catch(() => {});
    }
  },
  created() {
    this.getList();
  }
};
</script>
