<template>
  <div>
    <en-table-layout :tableData="tableData.data" :loading="loading">
      <div slot="toolbar" class="inner-toolbar">
        <div class="toolbar-btns">
          <el-button size="mini" type="primary" icon="el-icon-circle-plus-outline"
                     v-if="checkPermission(['marketingManager:add'])" @click="handleAdd">添加</el-button>
        </div>
        <div class="toolbar-search">
          <en-table-search @search="searchEvent" placeholder="请输入真实姓名/手机号搜索">
          </en-table-search>
        </div>
      </div>
      <template slot="table-columns">
        <el-table-column prop="real_name" label="真实姓名"></el-table-column>
        <el-table-column prop="dept_name" label="所属部门"></el-table-column>
        <el-table-column prop="mobile" label="手机号" />
        <el-table-column prop="disable_flag" label="状态" width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.disable_flag === 0 ? '正常' : '禁用' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="350px">
          <template slot-scope="scope">
            <el-button size="mini" v-if="checkPermission(['marketingManager:edit'])"
              @click="handleEditSupplier(scope.$index, scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </template>

      <el-pagination v-if="tableData" slot="pagination" @size-change="handlePageSizeChange"
        @current-change="handlePageCurrentChange" :current-page="tableData.page_no" :page-sizes="[10, 20, 50, 100]"
        :page-size="tableData.page_size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.data_total">
      </el-pagination>
    </en-table-layout>
    <el-dialog :title="marketingManagerForm.id ? '编辑营销经理' : '添加营销经理'" :visible.sync="dialogVisible" width="400px"
      @open="handleDialogOpen" :modal-append-to-body="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="marketingManagerForm" :rules="marketingManagerRules" ref="marketingManagerForm" label-width="120px"
        inline>
        <el-form-item label="真实姓名" prop="real_name">
          <el-input v-model="marketingManagerForm.real_name" :maxlength="30" clearable placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="所属部门" prop="dept_id">
          <el-cascader v-model="marketingManagerForm.dept_id" :options="deptList" :props="{
            label: 'name',
            value: 'id',
            emitPath: false,
            checkStrictly: true
          }" clearable></el-cascader>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="marketingManagerForm.mobile" :maxlength="20" clearable placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="disable_flag">
           <el-select v-model="marketingManagerForm.disable_flag" placeholder="请选择状态">
              <el-option label="正常" :value="0"/>
             <el-option label="禁用" :value="1"/>
           </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitSupplierForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as API_BasicSetting from '@/api/basicSetting'
import * as API_Setting from '@/api/setting'
import { Foundation } from '~/ui-utils'
export default {
  name: 'marketingManager',
  data() {
    return {
      deptList: [],
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 列表数据
      tableData: '',
      // 高级搜索数据
      advancedForm: {
        dept_id: ''
      },
      // 营销经理表单
      marketingManagerForm: {},
      // 营销经理表单 规则
      marketingManagerRules: {
      },
      // 营销经理表单 dialog
      dialogVisible: false
    }
  },
  mounted() {
    this.GET_SupplierList()
    this.GET_DeptList()
  },
  methods: {
    /** 获取部门 */
    GET_DeptList() {
      API_Setting.getDeptList().then(response => {
        this.deptList = Foundation.buildTree(response, '0')
      })
    },
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_SupplierList()
    },

    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.GET_SupplierList()
    },
    /** 编辑营销经理 */
    handleEditSupplier(index, row) {
      this.marketingManagerForm = this.MixinClone(row)
      this.dialogVisible = true
    },
    /** 添加营销经理 */
    handleAdd() {
      this.marketingManagerForm = {
        disable_flag: 0
      }
      this.dialogVisible = true
    },
    /** dialog打开后重置form表单校验结果 */
    handleDialogOpen() {
      setTimeout(() => { this.$refs['marketingManagerForm'].clearValidate() })
    },

    /** 提交营销经理表单 */
    submitSupplierForm() {
      this.$refs['marketingManagerForm'].validate((valid) => {
        if (valid) {
          const { id } = this.marketingManagerForm
          const params = this.MixinClone(this.marketingManagerForm)
          if (id) {
            API_BasicSetting.editMarketingManager(id, params).then(response => {
              this.dialogVisible = false
              this.$message.success('修改成功！')
              this.GET_SupplierList()
            })
          } else {
            API_BasicSetting.addMarketingManager(params).then(response => {
              this.dialogVisible = false
              this.$message.success('新增成功！')
              this.GET_SupplierList()
            })
          }
        } else {
          this.$message.error('表单填写有误，请核对！')
          return false
        }
      })
    },

    /** 搜索事件触发 */
    searchEvent(data) {
      this.params = {
        ...this.params,
        keyword: data
      }
      this.params.page_no = 1
      this.GET_SupplierList()
    },

    /** 获取营销经理列表 */
    GET_SupplierList() {
      this.loading = true
      API_BasicSetting.getMarketingManagerList(this.params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
.form-item-remark {
  width: 1000px;
}

.form-item-remark-input {
  width: 845px;
}

.face-image {
  display: block;
  width: 50px;
  height: 50px;
  margin: 0 auto;
}

/deep/ .el-dialog__body {
  padding: 10px 20px;
}
</style>
