<template>
  <a-card size="small" :bordered="false" class="salesmanList-wrap">
    <!-- 搜索条件 -->
    <div ref="tableSearch" class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-item label="姓名">
              <a-input id="salesmanList-supplierName" v-model.trim="queryParam.name" allowClear placeholder="请输入姓名"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="状态">
              <v-select code="ENABLE_FLAG" id="salesmanList-enableFlag" v-model="queryParam.enableFlag" allowClear placeholder="请选择状态"></v-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="创建时间">
              <rangeDate ref="rangeDate" @change="dateChange" />
            </a-form-item>
          </a-col>
          <!-- <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="审核">
                <v-select code="ENABLE_FLAG" id="salesmanList-enabledFlag" v-model="queryParam.enabledFlag" allowClear placeholder="请选择状态"></v-select>
              </a-form-item>
            </a-col>
          </template> -->
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="salesmanList-refresh">查询</a-button>
            <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="salesmanList-reset">重置</a-button>
            <!-- <a @click="advanced=!advanced" style="margin-left: 5px">
              {{ advanced ? '收起' : '展开' }}
              <a-icon :type="advanced ? 'up' : 'down'"/>
            </a> -->
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮 -->
    <div class="table-operator">
      <a-button
        v-if="$hasPermissions('B_salesman_add')"
        id="salesmanList-add"
        type="primary"
        class="button-error"
        @click="handleEdit()">新增</a-button>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable fixPagination"
      ref="table"
      :style="{ height: tableHeight+84.5+'px' }"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ y: tableHeight }"
      bordered>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <!-- <a-button
          size="small"
          type="link"
          class="button-warning"
          @click="handleSend(record)"
          id="salesmanList-edit-btn">通过</a-button> -->
        <a-button
          size="small"
          type="link"
          class="button-info"
          @click="handleEdit(record,1)"
          v-if="$hasPermissions('B_salesman_edit')"
          id="salesmanList-edit-btn">编辑</a-button>
        <a-button
          size="small"
          type="link"
          v-if="$hasPermissions('B_salesman_detail')"
          class="button-success"
          @click="handleEdit(record,2)"
          id="salesmanList-edit-btn">详情</a-button>
        <span v-else>--</span>
      </template>
    </s-table>
    <!-- 新增业务员 -->
    <salesman-edit-modal :itemId="itemId" :modalTitle="modalTitle" :openModal="openModal" @close="closeModal" />
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import { employeeList } from '@/api/salesman'
import salesmanEditModal from './editModal.vue'
import rangeDate from '@/views/common/rangeDate.vue'
export default {
  components: { STable, VSelect, salesmanEditModal, rangeDate },
  data () {
    return {
      advanced: true, // 高级搜索 展开/关闭
      modalTitle: '', // 弹窗title
      queryParam: { //  查询条件
        beginDate: '',
        endDate: '',
        name: '', //  姓名
        enableFlag: undefined //  状态
      },
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 70, align: 'center' },
        { title: '姓名', dataIndex: 'name', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '性别', dataIndex: 'sexDictValue', width: 60, align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '联系电话', dataIndex: 'mobile', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入职日期', dataIndex: 'entryDate', width: 140, align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '创建时间', dataIndex: 'createDate', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', dataIndex: 'enableFlagDictValue', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 100, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return employeeList(Object.assign(parameter, this.queryParam)).then(res => {
          const data = res.data
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.disabled = false
          this.spinning = false
          return data
        })
      },
      itemId: '', //  当前id
      openModal: false // 弹框
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam = { //  查询条件
        beginDate: '',
        endDate: '',
        name: '', //  姓名
        enableFlag: undefined //  状态
      }
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit (row, type) {
      if (row) {
        this.itemId = row && row.id ? row.id : null
        if (type == 1) {
          this.modalTitle = '编辑'
        } else {
          this.modalTitle = '详情'
        }
      } else {
        this.modalTitle = '新增'
      }
      this.openModal = true
    },
    // //  通过
    // handleSend (row) {
    //   const _this = this
    //   this.$confirm({
    //     title: '提示',
    //     content: '审核通过后该类型名称不能更改，确定审核通过该类型名称？',
    //     centered: true,
    //     closable: true,
    //     onOk () {
    //       _this.spinning = true
    //       employeeAudit({
    //         salesBillSn: row.id
    //       }).then(res => {
    //         if (res.status == 200) {
    //           _this.$message.success(res.message)
    //           _this.$refs.table.refresh()
    //           _this.spinning = false
    //         } else {
    //           _this.spinning = false
    //         }
    //       })
    //     }
    //   })
    // },
    // 关闭弹框
    closeModal () {
      this.itemId = ''
      this.openModal = false
      this.$refs.table.refresh(true)
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 245
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      setTimeout(() => {
        _this.setTableH()
      }, 400)
    }
  },
  mounted () {
    const _this = this
    this.$nextTick(() => { // 页面渲染完成后的回调
      _this.setTableH()
    })
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      if (!vm.disabled) {
        vm.$refs.table.refresh()
      }
    })
  }
}
</script>
