<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
<!--      <el-form-item label="公司ID" prop="companyId">-->
<!--        <el-input-->
<!--          v-model="queryParams.companyId"-->
<!--          placeholder="请输入公司ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="公司名" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基站管理员名" prop="companyUserName">
        <el-input
          v-model="queryParams.companyUserName"
          placeholder="请输入基站管理员名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基站管理员编号" prop="companyUserNumber">
        <el-input
          v-model="queryParams.companyUserNumber"
          placeholder="请输入基站管理员编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基站管理员账号" prop="companyUserAccount">
        <el-input
          v-model="queryParams.companyUserAccount"
          placeholder="请输入基站管理员账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="公司基站管理员密码" prop="companyUserPassword">-->
<!--        <el-input-->
<!--          v-model="queryParams.companyUserPassword"-->
<!--          placeholder="请输入公司基站管理员密码"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="系统用户ID" prop="sysUserId">-->
<!--        <el-input-->
<!--          v-model="queryParams.sysUserId"-->
<!--          placeholder="请输入系统用户ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="基站管理员状态" prop="companyUserStatus">
        <el-select v-model="queryParams.companyUserStatus" placeholder="请选择基站管理员状态" clearable>
          <el-option
            v-for="dict in dict.type.mall_company_user_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="省份ID" prop="provinceId">-->
<!--        <el-input-->
<!--          v-model="queryParams.provinceId"-->
<!--          placeholder="请输入省份ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="省份名" prop="provinceName">-->
<!--        <el-input-->
<!--          v-model="queryParams.provinceName"-->
<!--          placeholder="请输入省份名"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="城市ID" prop="cityId">-->
<!--        <el-input-->
<!--          v-model="queryParams.cityId"-->
<!--          placeholder="请输入城市ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="城市名" prop="cityName">-->
<!--        <el-input-->
<!--          v-model="queryParams.cityName"-->
<!--          placeholder="请输入城市名"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="区县ID" prop="areaId">-->
<!--        <el-input-->
<!--          v-model="queryParams.areaId"-->
<!--          placeholder="请输入区县ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="区县名" prop="areaName">-->
<!--        <el-input-->
<!--          v-model="queryParams.areaName"-->
<!--          placeholder="请输入区县名"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="地址" prop="address">-->
<!--        <el-input-->
<!--          v-model="queryParams.address"-->
<!--          placeholder="请输入地址"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <!-- 时间筛选 -->
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateAt"
          size="small"
          style="width: 215px !important"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始"
          end-placeholder="结束"
        ></el-date-picker>
      </el-form-item>
<!--      <el-form-item label="修改时间" prop="updateAt">-->
<!--        <el-input-->
<!--          v-model="queryParams.updateAt"-->
<!--          placeholder="请输入修改时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @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="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:mallCompanyUser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mall:mallCompanyUser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mall:mallCompanyUser:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['mall:mallCompanyUser:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallCompanyUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="公司基站管理员主键ID" align="center" prop="companyUserId" />-->
<!--      <el-table-column label="公司ID" align="center" prop="companyId" />-->
      <el-table-column label="公司名" align="center" prop="companyName" />
      <el-table-column label="基站管理员头像" align="center" prop="companyUserHead" width="120">
        <template slot-scope="scope">
          <image-preview :src="scope.row.companyUserHead" width="50px" height="50px"/>
        </template>
      </el-table-column>
      <el-table-column label="基站管理员名" align="center" prop="companyUserName" width="120" />
      <el-table-column label="基站管理员编号" align="center" prop="companyUserNumber" width="120" />
      <el-table-column label="基站管理员账号" align="center" prop="companyUserAccount" width="120" />
<!--      <el-table-column label="公司基站管理员密码" align="center" prop="companyUserPassword" />-->
<!--      <el-table-column label="系统用户ID" align="center" prop="sysUserId" />-->
      <el-table-column label="基站管理员状态" align="center" prop="companyUserStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mall_company_user_status" :value="scope.row.companyUserStatus"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="省份ID" align="center" prop="provinceId" />-->
      <el-table-column label="省份名" align="center" prop="provinceName" />
<!--      <el-table-column label="城市ID" align="center" prop="cityId" />-->
      <el-table-column label="城市名" align="center" prop="cityName" />
<!--      <el-table-column label="区县ID" align="center" prop="areaId" />-->
      <el-table-column label="区县名" align="center" prop="areaName" />
<!--      <el-table-column label="地址" align="center" prop="address" />-->
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="创建时间" align="center" prop="createAt" />
<!--      <el-table-column label="修改时间" align="center" prop="updateAt" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:mallCompanyUser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:mallCompanyUser:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleResetPwd(scope.row)"
            v-hasPermi="['mall:mallCompanyUser:edit']"
          >重置密码</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"
    />

    <!-- 添加或修改公司基站管理员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
<!--        <el-form-item label="公司ID" prop="companyId">-->
<!--          <el-input v-model="form.companyId" placeholder="请输入公司ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="公司" prop="companyId">
          <el-select
            v-model="form.companyId"
            placeholder="请选择公司"
          >
            <el-option
              v-for="item in companyList"
              :key="item.companyId"
              :label="item.companyName"
              :value="item.companyId"
            >
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="公司名" prop="companyName">-->
<!--          <el-input v-model="form.companyName" placeholder="请输入公司名" />-->
<!--        </el-form-item>-->
        <el-form-item label="基站管理员头像" prop="companyUserHead">
          <image-upload v-model="form.companyUserHead" limit="1" />
        </el-form-item>
        <el-form-item label="基站管理员名" prop="companyUserName">
          <el-input v-model="form.companyUserName" placeholder="请输入基站管理员名" />
        </el-form-item>
        <el-form-item label="基站管理员编号" prop="companyUserNumber">
          <el-input v-model="form.companyUserNumber" placeholder="请输入基站管理员编号" />
        </el-form-item>
        <el-form-item label="基站管理员账号" prop="companyUserAccount">
          <el-input v-model="form.companyUserAccount" placeholder="请输入基站管理员账号" />
        </el-form-item>
        <el-form-item v-if="form.companyUserId == undefined" label="基站管理员密码" prop="companyUserPassword">
          <el-input v-model="form.companyUserPassword" placeholder="请输入基站管理员密码" type="password" maxlength="20" show-password />
        </el-form-item>
<!--        <el-form-item label="系统用户ID" prop="sysUserId">-->
<!--          <el-input v-model="form.sysUserId" placeholder="请输入系统用户ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="基站管理员状态">
          <el-radio-group v-model="form.companyUserStatus">
            <el-radio
              v-for="dict in dict.type.mall_company_user_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="省份ID" prop="provinceId">-->
<!--          <el-input v-model="form.provinceId" placeholder="请输入省份ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="省份名" prop="provinceName">-->
<!--          <el-input v-model="form.provinceName" placeholder="请输入省份名" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="城市ID" prop="cityId">-->
<!--          <el-input v-model="form.cityId" placeholder="请输入城市ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="城市名" prop="cityName">-->
<!--          <el-input v-model="form.cityName" placeholder="请输入城市名" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="区县ID" prop="areaId">-->
<!--          <el-input v-model="form.areaId" placeholder="请输入区县ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="区县名" prop="areaName">-->
<!--          <el-input v-model="form.areaName" placeholder="请输入区县名" />-->
<!--        </el-form-item>-->
        <el-form-item label="区域选择:">
          <el-select
            style="width: 190px"
            v-model="form.provinceId"
            placeholder="请选择省"
          >
            <el-option
              v-for="item in provinceList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <el-select
            style="width: 190px; margin-left: 12px"
            v-model="form.cityId"
            placeholder="请选择市"
            @focus="selectCity"
          >
            <el-option
              v-for="item in cityList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <el-select
            style="width: 190px; margin-left: 13px"
            v-model="form.areaId"
            placeholder="请选择区"
            @focus="selectArea"
          >
            <el-option
              v-for="item in areaList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="创建时间" prop="createAt">-->
<!--          <el-input v-model="form.createAt" placeholder="请输入创建时间" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="修改时间" prop="updateAt">-->
<!--          <el-input v-model="form.updateAt" placeholder="请输入修改时间" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="删除标记：0-正常，1-删除" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入删除标记：0-正常，1-删除" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 重置密码对话框 -->
    <el-dialog :title="title" :visible.sync="openResetPwd" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="基站管理员密码" prop="companyUserPassword">
          <el-input v-model="form.companyUserPassword" placeholder="请输入基站管理员密码" type="password" maxlength="20" show-password />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormResetPwd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listMallCompanyUser,
  getMallCompanyUser,
  delMallCompanyUser,
  addMallCompanyUser,
  updateMallCompanyUser,
  updateMallCompanyUserResetPwd
} from "@/api/mall/mallCompanyUser";
import {getCityOrArea, getProvince} from "@/api/set/setRegion";
import {getCompany} from "@/api/mall/mallCompany";

export default {
  name: "MallCompanyUser",
  dicts: ['mall_company_user_status'],
  data() {
    return {
      // 重置密码显示弹出层
      openResetPwd: false,
      // 公司
      companyList: [],
      // 省
      provinceList: [],
      // 市
      cityList: [],
      // 区县
      areaList: [],
      // 创建时间时间范围
      daterangeCreateAt: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组
      companyUserNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公司基站管理员表格数据
      mallCompanyUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        companyName: null,
        companyUserHead: null,
        companyUserName: null,
        companyUserNumber: null,
        companyUserAccount: null,
        companyUserPassword: null,
        sysUserId: null,
        companyUserStatus: null,
        provinceId: null,
        provinceName: null,
        cityId: null,
        cityName: null,
        areaId: null,
        areaName: null,
        address: null,
        remarks: null,
        createAt: null,
        updateAt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyId: [
          { required: true, message: "公司不能为空", trigger: "blur" }
        ],
        companyUserName: [
          { required: true, message: "公司基站管理员名不能为空", trigger: "blur" }
        ],
        companyUserAccount: [
          { required: true, message: "公司基站管理员账号不能为空", trigger: "blur" },
          { min: 5, max: 20, message: '账号长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        companyUserPassword: [
          { required: true, message: "公司基站管理员密码不能为空", trigger: "blur" },
          { min: 6, max: 20, message: '密码长度必须介于 6 和 20 之间', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getProvinceList();
    this.getCompanyList();
  },
  methods: {
    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.reset();
      const companyUserId = row.companyUserId
      getMallCompanyUser(companyUserId).then(response => {
        this.form.companyUserId = response.data.companyUserId;
        this.openResetPwd = true;
        this.title = "重置密码";
      });
    },

    /** 重置密码提交按钮 */
    submitFormResetPwd() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.companyUserId != null) {
            updateMallCompanyUserResetPwd(this.form).then(response => {
              this.$modal.msgSuccess("重置成功");
              this.openResetPwd = false;
              this.getList();
            });
          }
        }
      });
    },

    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.form.companyUserHead = response.url;
    },
    // 获取公司
    getCompanyList() {
      getCompany().then((response) => {
        this.companyList = response.data.data;
      });
    },
    // 获取省
    getProvinceList() {
      getProvince().then((response) => {
        this.provinceList = response.data.data;
      });
    },
    // 获取市
    selectCity() {
      let provinceId = this.form.provinceId;
      getCityOrArea(provinceId).then((response) => {
        this.cityList = response.data.data;
      });
    },
    // 获取区县
    selectArea() {
      let cityId = this.form.cityId;
      getCityOrArea(cityId).then((response) => {
        this.areaList = response.data.data;
      });
    },

    /** 查询公司基站管理员列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateAt && "" != this.daterangeCreateAt) {
        this.queryParams.params["beginCreateAt"] = this.daterangeCreateAt[0];
        this.queryParams.params["endCreateAt"] = this.daterangeCreateAt[1];
      }
      listMallCompanyUser(this.queryParams).then(response => {
        this.mallCompanyUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openResetPwd = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        companyUserId: null,
        companyId: null,
        companyName: null,
        companyUserHead: null,
        companyUserName: null,
        companyUserNumber: null,
        companyUserAccount: null,
        companyUserPassword: null,
        sysUserId: null,
        companyUserStatus: "0",
        provinceId: null,
        provinceName: null,
        cityId: null,
        cityName: null,
        areaId: null,
        areaName: null,
        address: null,
        remarks: null,
        createAt: null,
        updateAt: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateAt = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.companyUserId)
      this.companyUserNames = selection.map(item => item.companyUserName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公司基站管理员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const companyUserId = row.companyUserId || this.ids
      getMallCompanyUser(companyUserId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公司基站管理员";
      });

      let provinceId = row.provinceId;
      getCityOrArea(provinceId).then((response) => {
        this.cityList = response.data.data;
      });

      let cityId = row.cityId;
      getCityOrArea(cityId).then((response) => {
        this.areaList = response.data.data;
      });

      this.upload.fileList = [];
      this.upload.fileList = [{ name: this.form.fileName, url: row.companyUserHead }];
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.companyUserId != null) {
            updateMallCompanyUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMallCompanyUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const companyUserIds = row.companyUserId || this.ids;
      const companyUserNames = row.companyUserName || this.companyUserNames;
      this.$modal.confirm('是否确认删除基站管理员名为"' + companyUserNames + '"的数据项？').then(function() {
        return delMallCompanyUser(companyUserIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mall/mallCompanyUser/export', {
        ...this.queryParams
      }, `mallCompanyUser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
