<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div class="screen-container">
      <div class="top clearfix">
        <div class="title fl">条件筛选</div>
       <el-button class="fr" type="primary" @click="isOpen = !isOpen" size="mini">{{isOpen ? '收起':'展开'}}<i class="el-icon&#45;&#45;right" :class="isOpen ? 'el-icon-arrow-up':'el-icon-arrow-down'"></i></el-button>
      </div>
      <el-form ref="screenForm" :model="screenForm" label-width="70px" size="small" label-position="left">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="结算单号" prop="orderShareId">
              <el-input v-model="screenForm.orderShareId" placeholder="请输入结算单号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="订单编号" prop="orderId">
              <el-input v-model="screenForm.orderId" placeholder="请输入订单编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="销售人员" prop="workUserName">
              <el-input v-model="screenForm.workUserName" placeholder="请输入销售人员"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="screenForm.phone" placeholder="请输入手机号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="结算状态" prop="state">
              <el-select v-model="screenForm.status" placeholder="全部">
                <el-option label="全部" value=""></el-option>
                <el-option :label="item.label" :value="item.value" v-for="(item, index) in select_status" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="网点名称" prop="websitId">
              <el-select v-model="screenForm.websitId" placeholder="全部" filterable>
                <el-option label="全部" value=""></el-option>
                <el-option :label="item.name" :value="item.id" v-for="(item, index) in websiteList" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="职位名称" prop="position">
              <el-select v-model="screenForm.position" placeholder="全部" filterable>
                <el-option label="全部" value=""></el-option>
                <el-option :label="item.position" :value="item.position" v-for="(item, index) in positionList" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6" v-show="isOpen">
            <el-form-item label="申请时间" prop="applyDate">
              <el-date-picker
                v-model="screenForm.applyDate"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6" v-show="isOpen">
            <el-form-item label="支付时间" prop="payDate">
              <el-date-picker
                v-model="screenForm.payDate"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6" v-show="isOpen">
            <el-form-item label="结算时间" prop="settlementDate">
              <el-date-picker
                v-model="screenForm.settlementDate"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          
          <el-col :xs="24" :sm="isOpen ? 24 : 12" :lg="isOpen ? 12 : 6" class="tr">
            <el-form-item label="">
              <el-button size="small" @click="resetScreenForm">清空</el-button>
              <el-button size="small" type="primary" @click="submitScreenForm">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="mymain-container">
      <div class="btn-group clearfix">
        <div class="fr">
          <el-button size="small" type="primary" icon="el-icon-download" @click="handleExport">导出</el-button>
        </div>
      </div>
      <div class="table">
        <el-table v-loading="listLoading" :data="dataList" element-loading-text="Loading" border fit highlight-current-row stripe>
          <el-table-column align="center" label="操作" width="100" fixed="left">
            <template slot-scope="scope">
              <el-popconfirm title="您确定要结算吗？" @onConfirm="handleSettlement(scope.row.orderId)" v-if="scope.row.orderStatus == 'OVER' && scope.row.status == 'ING'" style="margin-right: 10px">
                <el-button slot="reference" type="text">结算</el-button>
              </el-popconfirm>
              <el-button type="text" @click="showRemark(scope.row)">备注</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="结算单号" prop="orderShareId" min-width="170"></el-table-column>
          <el-table-column align="center" label="订单号" prop="orderId" min-width="180"></el-table-column>
          <el-table-column align="center" label="业务员" prop="workUserName"></el-table-column>
          <el-table-column align="center" label="业务员所属网点" prop="websitName" min-width="180" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="业务员职位" prop="position" min-width="180" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="业务员手机" prop="workUserPhone" min-width="120"></el-table-column>
          <el-table-column align="center" label="分销金额" prop="amount"></el-table-column>
          <el-table-column align="center" label="买家" prop="userName" min-width="100" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="买家手机" prop="phone" min-width="120"></el-table-column>
          <el-table-column align="center" label="支付金额" prop="payment"></el-table-column>
          <el-table-column align="center" label="结算状态" prop="status">
            <template slot-scope="scope">
              {{ scope.row.status | SETTLEMENT_CURRENT_STATUS_FILTER }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="结算时间" prop="settleTime" width="160"></el-table-column>
          <el-table-column align="center" label="备注" prop="remark" width="150" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="创建时间" prop="createTime" width="160"></el-table-column>
        </el-table>
      </div>
    </div>
    <div class="pagination clearfix">
      <div class="fr">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal">
        </el-pagination>
      </div>
    </div>

    <el-dialog :title="'订单备注'" :visible.sync="remarkVisible" :show-close="false" width="50%" :close-on-click-modal="false">
      <el-input
        type="textarea"
        :autosize="{ minRows: 8, maxRows: 16}"
        placeholder="请输入备注内容"
        v-model="remark"
        maxlength="100"
        show-word-limit>
      </el-input>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveRemark">保 存</el-button>
        <el-button @click="cancelRemark">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { COMMON_SELECT } from '@/utils/select_data'
import {getList, settlement, getWebsiteList, getPositionList, saveRemark} from "@/api/settlement";
import {downloadFiles} from '@/utils/util'

export default {
  name: "index",
  data() {
    return {
      isOpen: false, // 是否展开条件筛选
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: null, // 列表数据
      listLoading: false, // 列表加载loading
      screenForm: { // 筛选表单数据
        orderShareId: '', // 结算单号
        orderId: '', // 订单编号
        workUserName: '', // 销售人员
        phone: '', // 手机号
        status: '', // 结算状态
        applyDate: '', // 申请时间
        payDate: '', // 支付时间
        settlementDate: '', // 结算时间
        websitId: '', // 网点名称
        position: '', // 职位名称
      },
      select_status: COMMON_SELECT.SETTLEMENT_CURRENT_STATUS,
      websiteList: [],
      positionList: [],

      remarkVisible: false,
      handleId: null,
      remark: '',
    }
  },
  created() {
    this.getList();
    this.getWebsiteList();
    this.getPositionList();
  },
  methods: {
    // 查询列表
    getList() {
      this.listLoading = true;

      let params = {
        orderShareId: this.screenForm.orderShareId, // 结算单号
        orderId: this.screenForm.orderId, // 订单编号
        workUserName: this.screenForm.workUserName, // 销售人员
        phone: this.screenForm.phone, // 手机号
        status: this.screenForm.status, // 结算状态
        startTime: this.screenForm.applyDate ? this.screenForm.applyDate[0] : '', // 申请时间
        endTime: this.screenForm.applyDate ? this.screenForm.applyDate[1] : '', // 申请时间
        startPayTime: this.screenForm.payDate ? this.screenForm.payDate[0] : '', // 支付时间
        endPayTime: this.screenForm.payDate ? this.screenForm.payDate[1] : '', // 支付时间
        startSettleTime: this.screenForm.settlementDate ? this.screenForm.settlementDate[0] : '', // 结算时间
        endSettleTime: this.screenForm.settlementDate ? this.screenForm.settlementDate[1] : '', // 结算时间
        websitId: this.screenForm.websitId,
        position: this.screenForm.position,
        pageNum: this.currentPage,
        pageSize: this.pageSize
      };
      getList(params).then((res) => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      })
    },
    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1;
      this.getList();
    },
    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields();
      this.currentPage = 1;
      this.getList();
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getList();
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },

    // 结算
    handleSettlement(id) {
      settlement({
        orderId: id
      }).then(res => {
        this.$successMsg('操作成功');
        this.currentPage = 1;
        this.getList();
      })
    },

    // 网点列表
    getWebsiteList() {
      getWebsiteList({
        pageNum: 1,
        pageSize: 1000
      }).then(res => {
        this.websiteList = res.data.records;
      })
    },

    // 职位列表
    getPositionList() {
      getPositionList({
        pageNum: 1,
        pageSize: 1000
      }).then(res => {
        this.positionList = res.data.records;
      })
    },

    showRemark(item) {
      this.remarkVisible = true;
      this.handleId = item.orderShareId;
      this.remark = item.remark;
    },

    cancelRemark() {
      this.remarkVisible = false;
      this.handleId = '';
      this.remark = '';
    },

    saveRemark() {
      const params = {
        orderShareId: this.handleId,
        remark: this.remark
      }
      saveRemark(params).then(() => {
        this.cancelRemark();
        this.getList();
        this.$successMsg('保存成功');
      })
    },

    // 导出
    handleExport() {
      let screenData = {
        orderShareId: this.screenForm.orderShareId, // 结算单号
        orderId: this.screenForm.orderId, // 订单编号
        workUserName: this.screenForm.workUserName, // 销售人员
        phone: this.screenForm.phone, // 手机号
        status: this.screenForm.status, // 结算状态
        startTime: this.screenForm.applyDate ? this.screenForm.applyDate[0] : '', // 申请时间
        endTime: this.screenForm.applyDate ? this.screenForm.applyDate[1] : '', // 申请时间
        startPayTime: this.screenForm.payDate ? this.screenForm.payDate[0] : '', // 支付时间
        endPayTime: this.screenForm.payDate ? this.screenForm.payDate[1] : '', // 支付时间
        startSettleTime: this.screenForm.settlementDate ? this.screenForm.settlementDate[0] : '', // 结算时间
        endSettleTime: this.screenForm.settlementDate ? this.screenForm.settlementDate[1] : '', // 结算时间
        websitId: this.screenForm.websitId,
        position: this.screenForm.position,
        pageNum: this.currentPage,
        pageSize: this.pageSize
      };
      downloadFiles('order/share/export', screenData);
    },
  }
}
</script>

<style scoped>

</style>
