<template>
  <my-page>
    <template slot="left">
      <OrderList style="width: 400px;" status="1" @change="handleOrderChange"></OrderList>
    </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="itemName">
          <el-input class="filter-input" v-model="queryParams.itemName" placeholder="请输入费目名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="是否对账" prop="lockStatus">
          <el-select class="filter-input" v-model="queryParams.lockStatus" placeholder="请选择是否对账" clearable >
            <el-option label="未对账"
              value="0" />
            <el-option label="已对账"
              value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="beginDate">
          <el-date-picker class="filter-input" v-model="queryParams.beginDate" value-format="yyyy-MM-dd" type="date" placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endDate">
          <el-date-picker class="filter-input" v-model="queryParams.endDate" value-format="yyyy-MM-dd" type="date" placeholder="请选择结束时间">
          </el-date-picker>
        </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" style="height: 28px;padding-top: 3px;">
          <el-radio-group v-model="queryParams.rpFlag" @change="handleQuery">
            <el-radio label="">全部</el-radio>
            <el-radio label="0">应收</el-radio>
            <el-radio label="1">应付</el-radio>
          </el-radio-group>
        </el-col>
        <el-col :span="1.5">
          <el-tooltip class="item" effect="dark" :disabled="!!order.id" content="请先选择订单" placement="top">
            <el-popconfirm title="确定自动结算选中订单吗？" @confirm="handleAutoAdd">
              <el-button class="tool-btn" slot="reference" :disabled="!order.id" type="primary" plain icon="el-icon-cpu" size="mini"
              v-hasPermi="['finance:amortize:add']">自动计算</el-button>
            </el-popconfirm>
          </el-tooltip>
        </el-col>
        <el-col :span="1.5">
          <el-tooltip class="item" effect="dark" :disabled="!!order.id" content="请先选择订单" placement="top">
            <el-button class="tool-btn" type="primary" plain :disabled="!order.id" icon="el-icon-plus" size="mini" @click="handleAdd('0')"
            v-hasPermi="['finance:amortize:add']">新增应收</el-button>
          </el-tooltip>
        </el-col>
        <el-col :span="1.5">
          <el-tooltip class="item" effect="dark" :disabled="!!order.id" content="请先选择订单" placement="top">
            <el-button class="tool-btn" type="primary" plain :disabled="!order.id" icon="el-icon-plus" size="mini" @click="handleAdd('1')"
            v-hasPermi="['finance:amortize:add']">新增应付</el-button>
          </el-tooltip>
        </el-col>
        <el-col :span="1.5">
          <el-button class="tool-btn" type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['finance:amortize:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button class="tool-btn" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['finance:amortize:remove']">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button class="tool-btn" type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['finance:amortize:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="costList" :highlight-current-row="true" @sort-change="handleSortChange"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="" type="index" width="40" align="center" />
        <el-table-column label="客户名称" align="center" prop="custName" sortable='custom' />
        <el-table-column label="费用名称" align="center" prop="itemName" sortable='custom' />
        <el-table-column label="费用" align="center" prop="itemCost" sortable='custom' />
        <el-table-column label="数量(吨)" align="center" prop="itemNum" sortable='custom' />
        <el-table-column label="费率/运费" align="center" prop="itemRate" sortable='custom' />
        <el-table-column label="应收/应付" align="center" prop="rpFlag">
          <template slot-scope="scope">
            <el-tag :type="scope.row.rpFlag == 1 ? 'success' : ''" size="small">{{ scope.row.rpFlag == 0 ? '应收' : '应付'
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否对账" align="center" prop="lockStatus">
          <template slot-scope="scope">
            <el-tag :type="scope.row.lockStatus == 1 ? 'success' : ''" size="small">{{ scope.row.lockStatus == 1 ? '已对账' : '未对账'
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="已核销" align="center" prop="writeoffCost" sortable='custom' />
        <el-table-column label="币种" align="center" prop="currency" sortable='custom' >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_curr_type" :value="scope.row.currency" />
          </template>
        </el-table-column>
        <el-table-column label="订单号" align="center" prop="orderNo" sortable='custom' />
        <el-table-column label="日期" align="center" prop="inputDate" width="180" sortable='custom'>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" sortable='custom' />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleRead(scope.row)"
              v-hasPermi="['finance:amortize:query']">查看</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['finance:amortize:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['finance:amortize:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination hide-on-single-page :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />

      <!-- 添加或修改费用对话框 -->
      <el-dialog v-dialogDrag :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false">
        <AmortizeEdit ref="edit" v-if="open" :rpFlag="rpFlag" :order="order" :bid="showId" :readonly="readonly" @saved="refresh"></AmortizeEdit>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if="!readonly">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </my-page>
</template>

<script>
import { pageListCost, getCost, delCost, addCost, updateCost, autoCalc } from "@/api/amortize/cost";
import OrderList from '@/views/tm/components/OrderList';
import AmortizeEdit from '@/views/finance/components/AmortizeEdit';
import { isArray } from 'xe-utils';

export default {
  name: "Amortize",
  dicts: ['sys_curr_type'],
  components: {
    OrderList,
    AmortizeEdit,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 费用表格数据
      costList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        orderNo: null,
        itemName: null,
        cargoInfo: null,
        beginDate: null,
        endDate: null,
        lockStatus: null,
        rpFlag: '',
        orderByColumn: 'createTime',
        isAsc: 'DESC',
      },
      showId: null,
      readonly: false,
      order: {},
      rpFlag: '0',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleOrderChange(orders) {
      const order = orders[0] || {};
      this.order = order;
      this.queryParams.orderId = order.id;
      this.queryParams.orderNo = order.orderNo;
      this.handleQuery();
    },
    /** 查询费用列表 */
    getList() {
      this.loading = true;
      pageListCost(this.queryParams).then(response => {
        this.costList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(e => {
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    //排序
    handleSortChange(col) {
      this.$sortBy(col, this.queryParams);
      this.getList();
    },
    refresh() {
      this.open = false;
      this.getList();
    },
    /** 搜索按钮操作 */
    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;
    },
    handleAutoAdd() {
      autoCalc({
        ...this.order,
        orderId: this.order.id,
      }).then(response => {
        this.getList();
      }).catch(e => {
        
      });
    },
    /** 查看按钮操作 */
    handleRead(row) {
      const id = row.id || this.ids;
      this.open = true;
      this.showId = id;
      this.readonly = true;
      this.title = '查看费用信息';
    },
    /** 新增按钮操作 */
    handleAdd(type) {
      this.rpFlag = type;
      this.open = true;
      this.showId = '';
      this.readonly = false;
      this.title = type === '1' ? '新增应付费用':'新增应收费用';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids;
      this.open = true;
      this.showId = id;
      this.readonly = false;
      this.title = '编辑费用信息';
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.edit.submitForm();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(`是否确认删除${Array.isArray(ids) ?'选中的这些':'当前'}费用？`).then(function () {
        return delCost(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('amortize/cost/export', {
        ...this.queryParams
      }, `cost_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.tool-btn {
  margin-bottom: 5px;
}
</style>