<template>
  <div class="app-container">
    <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('订单编号')" prop="orderNo">
        <el-input v-model.trim="queryParams.orderNo" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('客户名称')" prop="clientName">
        <el-input v-model.trim="queryParams.clientName" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('交货期')" prop="deliveryDate">
        <el-date-picker clearable v-model="queryParams.deliveryDate" type="date" value-format="yyyy-MM-dd" >
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('跟单人')" prop="merchandiser">
        <el-select v-model="queryParams.merchandiser" clearable filterable remote :remote-method="getUserList">
          <el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.nickName"/>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('订单状态')" prop="state">
        <el-select v-model="queryParams.state" clearable>
          <el-option v-for="item in dict.type.sales_order_state" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('common.search') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.reset') }}</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="['od:salesOrder:add']" >{{$t('新增')}}</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="['od:salesOrder:edit']" >{{$t('修改')}}</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="['od:salesOrder:remove']" >{{$t('删除')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['od:salesOrder:export']" >{{$t('导出')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column :label="$t('订单编号')" prop="orderNo"/>
      <el-table-column :label="$t('客户编号')" prop="clientNo"/>
      <el-table-column :label="$t('客户名称')" prop="clientName"/>
      <el-table-column :label="$t('交货期')" prop="deliveryDate" width="180"/>
      <el-table-column :label="$t('总金额')" prop="amount" :formatter="r => this.formatMoney(r.amount)"/>
      <el-table-column :label="$t('跟单人')" prop="merchandiser"/>
      <el-table-column :label="$t('common.remark')" prop="remark"/>
      <el-table-column :label="$t('订单状态')" prop="state">
        <template slot-scope="{row}">
          {{ dict.type.sales_order_state.find(item => item.value == row.state).label }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.option')" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['od:salesOrder:edit']"
          >{{$t('详情')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['od:salesOrder:remove']"
          >{{$t('删除')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="onSync(scope.row)"
            v-hasPermi="['od:salesOrder:edit']"
          >{{$t('同步')}}</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"
    />

  </div>
</template>

<script>


import { delOrder, listOrder } from '@/api/mes/od/salesOrder'
import { listUser } from '@/api/system/user'

export default {
  dicts: ['mes_client_type','sys_yes_no', 'sales_order_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售订单表格数据
      orderList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null, clientCode: null, deliveryDate: null, amount: null, merchandiser: null, state: null,
        mdClient: {clientName: null}
      },
      userList: [],
    }
  },
  created() {
    this.getList()
    this.getUserList()
  },
  methods: {
    /** 查询销售订单列表 */
    getList() {
      this.loading = true
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows
        this.total = response.total
      }).finally(() => this.loading = false)
    },
    getUserList(nickName) {
      listUser({nickName, pageNum: 1, pageSize: 100}).then(res => this.userList = res.rows)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/order/sales/detail')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push('/order/sales/detail?id=' + row.id)
    },
    /** 同步数据 */
    onSync(row) {
      console.log(row.id)
      this.$router.push('/order/sales/sync?id=' + row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm(this.$t('common.confirm_del_data')).then(function() {
        return delOrder(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },

  }
}
</script>
