<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model.trim="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编号" prop="clientCode">
        <el-input
          v-model.trim="queryParams.clientCode"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交货期" prop="deliveryDate">
        <el-date-picker
          clearable
          v-model="queryParams.deliveryDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择交货期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="跟单人" prop="merchandiser">
        <el-select v-model="queryParams.merchandiser" clearable filterable remote :remote-method="getUserList" placeholder="请选择跟单人">
          <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="订单状态" prop="state">
        <el-select v-model="queryParams.state" clearable placeholder="请选择订单状态">
          <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">搜索</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="['od:salesOrder: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="['od:salesOrder: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="['od:salesOrder:remove']"
        >删除
        </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']"
        >导出
        </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="订单编号" prop="orderNo"/>
      <el-table-column label="客户编号" prop="clientNo"/>
      <el-table-column label="交货期" prop="deliveryDate" width="180"/>
      <el-table-column label="总金额" prop="amount" :formatter="r => this.formatMoney(r.amount)"/>
      <el-table-column label="跟单人" prop="merchandiser"/>
      <el-table-column label="备注" prop="remark"/>
      <el-table-column label="订单状态" 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="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['od:salesOrder:edit']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['od:salesOrder:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="onSync(scope.row)"
            v-hasPermi="['od:salesOrder:edit']"
          >同步
          </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('是否确认删除销售订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },

  }
}
</script>
