<template>
  <div class="app-container">
    <div class="app-container-search main-bgcolor main-radius">
      <h1 class="">员工管理</h1>
      <br />
      <!-- 搜索工作栏 -->
      <SearchInfo
        ref="searchForm"
        :searchFormParam="searchFormParam"
        :showSearch.sync="showSearch"
        @handleQuery="handleQuery"
        @resetQuery="resetQuery"
      ></SearchInfo>
    </div>

    <div class="app-container-list main-bgcolor main-radius shopping">
      <div class="shopping-top">
        <div class="shopping-title">员工列表</div>
        <el-button class="shopping-add-btn" type="primary" plain @click="handleAdd" 
          v-hasPermi="['basic:workers-info:create']">添加员工</el-button>
      </div>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="编号" align="center"  width="80px">
        <template slot-scope="scope">
            {{ (scope.$index+1)+(searchFormParam.queryParams.pageNo-1)*searchFormParam.queryParams.pageSize }}
        </template>
      </el-table-column>
      <el-table-column label="员工姓名" align="center" prop="workersName" />
      <el-table-column label="员工编码" align="center" prop="workersCode" />
      <el-table-column label="所属商户" align="center" prop="belongBusinessName" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="备注" align="center" prop="remarks" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button v-if="scope.row.openid !== ''" size="mini" type="text" icon="el-icon-edit" @click="handleUnbindingWX(scope.row)"
                     v-hasPermi="['basic:workers-info:update']">微信解绑</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['basic:workers-info:update']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['basic:workers-info:delete']">删除</el-button>
        </template>
      </el-table-column>
      <div slot="empty">
            <EmptyTable />
      </div>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="searchFormParam.queryParams.pageNo" :limit.sync="searchFormParam.queryParams.pageSize"
                @pagination="getList"/>
    </div>
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="top">
        <el-form-item label="所属商户" prop="belongBusinessName">
          <div style="display: flex;">
            <el-select v-model="form.belongBusinessId" :placeholder=this.belongBusinessPlaceholder clearable filterable style="flex: 1;"> 
              <el-option v-for="businessInfo in businessNameList"
               :key="businessInfo.index" :label="businessInfo.businessName" :value="businessInfo.id" @click.native="labelClick(businessInfo.businessName)"/>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="员工姓名" prop="workersName">
          <el-input v-model="form.workersName" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="员工编码" prop="workersCode">
          <el-input v-model="form.workersCode" placeholder="请输入员工编码" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </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 { createWorkersInfo, updateWorkersInfo, deleteWorkersInfo, getWorkersInfo, getWorkersInfoPage, unbindingWX } from "@/api/basic/workersInfo";
import SearchInfo from "@/components/Customer/SearchForm"
import EmptyTable from '@/components/EmptyTable'
import { getBusinessNameList } from "@/api/basic/businessInfo";
export default {
  name: "WorkersInfo",
  components: {
    SearchInfo,
    EmptyTable,
  },
  data() {
    return {
      //所属商户下拉框提示
      belongBusinessPlaceholder:'请选择所属商户',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //头部查询表单组件的参娄
      searchFormParam:{
        // 查询参数
        queryParams: {
          pageNo: 1,
          pageSize: 10,
          workersName: null,
          workersCode: null,
          belongBusinessName: null,
        },
        //表单对象
        dataSource:[
          {
            key:'workersName',
            itemType:'input',
            label:'员工姓名',
            placeholder:'请输入',
            onEnterKeyUp:this.handleQuery
          },
          {
            key:'workersCode',
            itemType:'input',
            label:'员工编码',
            placeholder:'请输入',
            onEnterKeyUp:this.handleQuery
          },
          {
            key:'belongBusinessName',
            itemType:'input',
            label:'所属商户',
            placeholder:'请输入',
            onEnterKeyUp:this.handleQuery
          },
        ]
      },
      //商户名称列表
      businessNameList:{},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workersName: [{ required: true, message: "员工姓名不能为空", trigger: "blur" }],
        workersCode: [{ required: true, message: "员工编码不能为空", trigger: "blur" }],
        contactPhone: [
          { required: true, message: '联系电话不能为空', trigger: "blur" },
          {
            pattern:  /^1[3|4|5|7|8|9][0-9]{9}$/,
            message: '联系电话格式有误！',
            trigger: 'blur',
          }
        ],
        belongBusinessName: [{ required: true, message: "所属商户不能为空", trigger: "blur" }],
        belongBusinessId: [{ required: true, message: '所属商户不能为空', trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      if(this.searchFormParam.queryParams.workersCode == ''){
        this.searchFormParam.queryParams.workersCode = null;
      }
      // 执行查询
      getWorkersInfoPage(this.searchFormParam.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 获取当前分页列表 */
    getWorkersInfoList() {
      this.loading = true;
      let queryParams = {
          pageNo: this.searchFormParam.queryParams.pageNo,
          pageSize: this.searchFormParam.queryParams.pageSize,
      }
      // 执行查询
      getWorkersInfoPage(queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },    
    /** 获取商户名称列表 */
    getBusinessNameList(){
      getBusinessNameList().then(response => {
        this.businessNameList = response.data;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        workersName: undefined,
        workersCode: undefined,
        belongBusinessId: undefined,
        belongBusinessName: undefined,
        contactPhone: undefined,
        remarks: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.searchFormParam.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm", "searchForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.belongBusinessPlaceholder = '请选择所属商户';
      this.getBusinessNameList();
      this.open = true;
      this.title = "添加员工";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      this.getBusinessNameList();
      getWorkersInfo(id).then(response => {
        this.form = response.data;

        //当商户被删除时，无法显示对应的商户名称，重新定义
        let isTip = true;
        for(var x=0;x<this.businessNameList.length;x++){
          if(this.businessNameList[x].id == this.form.belongBusinessId){
            isTip = false;
          }
        }
        if( isTip ) {
          this.form.belongBusinessId = undefined
          this.form.belongBusinessName = undefined
          this.belongBusinessPlaceholder = '所属商户已删除，请重新选择'
        }else{
          this.belongBusinessPlaceholder = '请选择所属商户'
        }

        this.open = true;
        this.title = "修改员工";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        
        // 修改的提交
        if (this.form.id != null) {
          updateWorkersInfo(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getWorkersInfoList();
          });
          return;
        }
        // 添加的提交
        createWorkersInfo(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getWorkersInfoList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除该员工?').then(function() {
          return deleteWorkersInfo(id);
        }).then(() => {
          this.getWorkersInfoList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },

    handleUnbindingWX(row) {
      const id = row.id;
      this.$modal.confirm('是为该员工解绑微信?').then(function() {
          return unbindingWX(id);
        }).then(() => {
          this.getWorkersInfoList();
          this.$modal.msgSuccess("解绑成功");
        }).catch(() => {});
    },

    //获取el-select选择的标签 
    labelClick(data){
      this.form.belongBusinessName = data;
    },
    
  }
};
</script>
