<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="人员类型" prop="companyUserType">
        <el-select v-model="queryParams.companyUserType" placeholder="请选择人员类型" clearable size="small">
          <el-option
            v-for="dict in companyUserTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="人员名称" prop="companyUserName">
        <el-input
          v-model="queryParams.companyUserName"
          placeholder="请输入人员名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员电话" prop="companyUserPhone">
        <el-input
          v-model="queryParams.companyUserPhone"
          placeholder="请输入人员电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号" prop="companyUserAccount">
        <el-input
          v-model="queryParams.companyUserAccount"
          placeholder="请输入账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员状态" prop="companyUserStatus">
        <el-select v-model="queryParams.companyUserStatus" placeholder="请选择人员状态" clearable size="small">
          <el-option label="正常" value="0" />
          <el-option label="冻结" value="1" />
        </el-select>
      </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:mallAppCompanyUser: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:mallAppCompanyUser: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:mallAppCompanyUser:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          :loading="exportLoading"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['mall:mallAppCompanyUser:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallAppCompanyUserList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="人员类型" align="center" prop="companyUserType">
        <template slot-scope="scope">
          <dict-tag :options="companyUserTypeOptions" :value="scope.row.companyUserType"/>
        </template>
      </el-table-column>
      <el-table-column label="人员名称" align="center" prop="companyUserName" />
      <el-table-column label="人员电话" align="center" prop="companyUserPhone" />
      <el-table-column label="账号" align="center" prop="companyUserAccount" />
<!--      <el-table-column label="密码" align="center" prop="companyUserPassword" />-->
      <el-table-column label="人员状态" align="center" prop="companyUserStatus" >
        <template slot-scope="scope">
          <div v-if="scope.row.companyUserStatus === 0">正常</div>
          <div v-if="scope.row.companyUserStatus === 1">冻结</div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createAt" />
      <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:mallAppCompanyUser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:mallAppCompanyUser:remove']"
          >删除</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"
    />

    <!-- 添加或修改app-公司人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="人员类型">
          <el-radio-group v-model="form.companyUserType">
            <el-radio
              v-for="dict in companyUserTypeOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="人员名称" prop="companyUserName">
          <el-input v-model="form.companyUserName" placeholder="请输入人员名称" />
        </el-form-item>
        <el-form-item label="人员电话" prop="companyUserPhone">
          <el-input v-model="form.companyUserPhone" placeholder="请输入人员电话" />
        </el-form-item>
        <el-form-item label="账号" prop="companyUserAccount">
          <el-input v-model="form.companyUserAccount" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="companyUserPassword">
          <el-input v-model="form.companyUserPassword" placeholder="请输入密码" maxlength="20" show-password />
        </el-form-item>
        <el-form-item label="人员状态">
          <el-radio-group v-model="form.companyUserStatus">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">冻结</el-radio>
          </el-radio-group>
        </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>
  </div>
</template>

<script>
import { listMallAppCompanyUser, getMallAppCompanyUser, delMallAppCompanyUser, addMallAppCompanyUser, updateMallAppCompanyUser, exportMallAppCompanyUser } from "@/api/mall/mallAppCompanyUser";

export default {
  name: "MallAppCompanyUser",
  props: ["companyId"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      companyUserNames: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // app-公司人员表格数据
      mallAppCompanyUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 公司人员类型：0-公司管理员，1-业务员字典
      companyUserTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        sysUserId: null,
        roleId: null,
        companyUserType: null,
        companyUserName: null,
        companyUserPhone: null,
        companyUserAccount: null,
        companyUserPassword: null,
        provinceId: null,
        provinceName: null,
        cityId: null,
        cityName: null,
        areaId: null,
        areaName: null,
        address: null,
        longitude: null,
        latitude: null,
        companyUserStatus: null,
        createAt: null,
        updateAt: null,
        delFlag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyUserName: [
          { required: true, message: "请输入人员名称", trigger: "blur" }
        ],
        companyUserPhone: [
          { required: true, message: "请输入人员电话", trigger: "blur" }
        ],
        companyUserAccount: [
          { required: true, message: "账号不能为空", trigger: "blur" },
          { min: 3, max: 20, message: '账号长度必须介于 3 和 20 之间', trigger: 'blur' }
        ],
        companyUserPassword: [
          { required: true, message: "用户密码不能为空", trigger: "blur" },
          { min: 6, max: 20, message: '用户密码长度必须介于 6 和 20 之间', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("company_user_type").then(response => {
      this.companyUserTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询app-公司人员列表 */
    getList() {
      this.loading = true;
      if (this.companyId != null){
        this.queryParams.companyId = this.companyId;
      }
      listMallAppCompanyUser(this.queryParams).then(response => {
        this.mallAppCompanyUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        companyUserId: null,
        companyId: null,
        sysUserId: null,
        roleId: null,
        companyUserType: 0,
        companyUserName: null,
        companyUserPhone: null,
        companyUserAccount: null,
        companyUserPassword: null,
        provinceId: null,
        provinceName: null,
        cityId: null,
        cityName: null,
        areaId: null,
        areaName: null,
        address: null,
        longitude: null,
        latitude: null,
        companyUserStatus: 0,
        createAt: null,
        updateAt: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      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
      getMallAppCompanyUser(companyUserId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.companyId = this.companyId;
        if (valid) {
          if (this.form.companyUserId != null) {
            updateMallAppCompanyUser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMallAppCompanyUser(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const companyUserIds = row.companyUserId || this.ids;
      const companyUserNames = row.companyUserName || this.companyUserNames;
      this.$confirm('是否确认删除人员名称为"' + companyUserNames + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMallAppCompanyUser(companyUserIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有app-公司人员数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportMallAppCompanyUser(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
