<template>
  <div class="app-container">
    <div v-show="!isShowDetail && !isShowForm && !isShowExamine && !isShowReturn">
      <!-- 筛选条件 -->
      <div class="screen-container">
        <el-form ref="screenForm" :model="screenForm" label-width="100px" size="small" label-position="left">
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="订单号" prop="orderNum">
                <el-input v-model="screenForm.orderNum" placeholder="请输入订单号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="原订单号" prop="originOrderNo">
                <el-input v-model="screenForm.originOrderNo" placeholder="请输入原订单号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="经销商名称" prop="customerKeyword">
                <el-input v-model="screenForm.customerKeyword" placeholder="请输入经销商名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="产品名称" prop="productName">
                <el-input v-model="screenForm.productName" placeholder="请输入产品名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="订单日期" prop="date">
                <el-date-picker
                  v-model="screenForm.date"
                  type="datetimerange"
                  range-separator="至"
                  style="width: 100%;"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="申请人" prop="createName">
                <el-input v-model="screenForm.createName" placeholder="请输入申请人"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="审核人" prop="confirmName">
                <el-input v-model="screenForm.confirmName" placeholder="请输入审核人"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="订单类型" prop="orderType">
                <el-select v-model="screenForm.orderType" placeholder="选择订单类型" style="width: 100%">
                  <el-option label="全部" value="" />
                  <el-option v-for="item in orderType" :key="item.val" :label="item.label" :value="item.val"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :xs="24" :sm="24" :lg="24" 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="fl">
            <el-button size="small" type="primary" icon="el-icon-plus" @click="toForm()">新增</el-button>
          </div>
          <div class="fr">
            <ExportButton :exUrl="'admin/user/mch/export'" :exParams="exParams" />
          </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="订单号" prop="displaceOrderId" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="订单日期" prop="orderDate" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="原订单号" prop="orderNo" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="订单类型" min-width="160" show-overflow-tooltip>
              <template slot-scope="{ row }">
                {{ findOrderType(row.orderType) }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="经销商编号" prop="customerId" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="经销商名称" prop="customerName" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="产品编码" prop="materialNumber" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="产品名称" prop="materialName" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="规格型号" prop="specification" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="业务员" prop="saleTypeName" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="申请人" prop="createName" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="申请日期" prop="createTime" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="审核人" prop="confirmName" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="审核日期" prop="confirmTime" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="审核状态" min-width="100" show-overflow-tooltip>
              <template slot-scope="{ row }">
                {{ findExamineStatus(row.examineStatus) }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="160" fixed="right">
              <template slot-scope="scope">
                <el-popconfirm style="margin-right: 10px;" title="确定撤回吗？" @onConfirm="handleWithdraw(scope.row.displaceOrderId)" v-if="scope.row.examineStatus === 'WAIT'" >
                  <el-button slot="reference" type="text">撤回</el-button>
                </el-popconfirm>
                <el-button type="text" @click="toForm(scope.row)" v-if="scope.row.examineStatus === 'SAVE'">编辑</el-button>
                <el-button type="text" @click="toExamine(scope.row)" v-if="scope.row.examineStatus === 'WAIT'">审批</el-button>
                <el-button type="text" @click="toReturn(scope.row)" v-if="scope.row.examineStatus === 'OK'">退订</el-button>
                <el-button type="text" @click="toDetail(scope.row)">详情</el-button>
                <el-popconfirm style="margin-left: 10px;" title="确定提审吗？" @onConfirm="handleSubmit(scope.row.displaceOrderId)" v-if="scope.row.examineStatus === 'SAVE'" >
                  <el-button slot="reference" type="text">提审</el-button>
                </el-popconfirm>
                <el-popconfirm style="margin-left: 10px;" title="确定删除吗？" @onConfirm="handleDelete(scope.row.displaceOrderId)" >
                  <el-button slot="reference" type="text">删除</el-button>
                </el-popconfirm>
              </template>
            </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>
    </div>

    <DisplaceDetail :listItem="queryItem" v-if="isShowDetail" @backListFormDetail="backList" />
    <DisplaceForm :listItem="queryItem" v-if="isShowForm" @backListFormDetail="backList" />
    <DisplaceExamine :listItem="queryItem" v-if="isShowExamine" @backListFormDetail="backList" />
    <DisplaceReturn :listItem="queryItem" v-if="isShowReturn" @backListFormDetail="backList" />
  </div>
</template>

<script>
import {deleteOrder, getList, revokeOrder, submitData} from "@/api/supply/displace";
import DisplaceDetail from "@/views/supply/displace/components/displace_detail";
import DisplaceForm from "@/views/supply/displace/components/displace_form";
import DisplaceExamine from "@/views/supply/displace/components/displace_examine";
import DisplaceReturn from "@/views/supply/displace/components/displace_return";

export default {
  components: {
    DisplaceDetail,
    DisplaceForm,
    DisplaceExamine,
    DisplaceReturn
  },
  data() {
    return {
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: null, // 列表数据
      listLoading: false, // 列表加载loading
      screenForm: { // 筛选表单数据
        orderNum: '',
        originOrderNo: '',
        customerKeyword: '',
        productName: '',
        date: '',
        createName: '',
        confirmName: '',
        orderType: '',
      },
      queryItem: {},
      isShowDetail: false,
      isShowForm: false,
      isShowExamine: false,
      isShowReturn: false,
      orderType : [
        { label: '商用工程', val: 'TRADE'},
        { label: '家用工程', val: 'HOME'},
        { label: '零售订单', val: 'RETAIL'},
        { label: '销售政策单', val: 'RETAIL_POLICY'}
      ],
      examineStatus : [
        { label: '保存', val: 'SAVE'},
        { label: '待审核', val: 'WAIT'},
        { label: '通过', val: 'OK'},
        { label: '不通过', val: 'FAIL'},
        { label: '关闭', val: 'CLOSE'}
      ],

    }
  },

  computed: {
    exParams() {
      return {
        retreatOrderNo: this.screenForm.orderNum,
        customerKeyword: this.screenForm.jxsNum,
        productName: this.screenForm.chName,
        specification: this.screenForm.model,
        startTime: this.screenForm.date ? this.screenForm.date[0] : '',
        endTime: this.screenForm.date ? this.screenForm.date[1] : '',
        status: 1, // 1:退货单，2:电商退货单
      }
    }
  },

  created() {
    this.getList();
  },

  methods: {
    // 查询按钮权限
    checkBtnRole(value) {
      // let btnRole = this.$route.meta.roles;
      // if(!btnRole) {return true}
      // let index = btnRole.indexOf(value);
      // return index >= 0;
      return true
    },

    // 查询列表
    getList() {
      this.listLoading = true;

      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        displaceOrderId: this.screenForm.orderNum,
        originOrderNo: this.screenForm.originOrderNo,
        customerKeyword: this.screenForm.customerKeyword,
        productName: this.screenForm.productName,
        createName: this.screenForm.createName,
        confirmName: this.screenForm.confirmName,
        orderType: this.screenForm.orderType,
        startOrderTime: this.screenForm.date ? this.screenForm.date[0] : '',
        endOrderTime: this.screenForm.date ? this.screenForm.date[1] : ''
      };
      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();
    },
    // 进入表单
    toForm(item) {
      this.queryItem = item;
      this.isShowForm = true;
    },

    // 进入审批
    toExamine(item) {
      this.queryItem = item;
      this.isShowExamine = true;
    },

    // 进入详情
    toDetail(item) {
      this.queryItem = item;
      this.isShowDetail = true;
    },

    // 进入退订
    toReturn(item) {
      this.queryItem = item;
      this.isShowReturn = true;
    },

    backList() {
      this.queryItem = {};
      this.isShowDetail = false;
      this.isShowForm = false;
      this.isShowExamine = false;
      this.isShowReturn = false;
    },
    handleSubmit(id) {
      submitData({id}).then(res => {
        this.$successMsg();
        this.getList();
      })
    },
    // 撤回
    handleWithdraw(id) {
      revokeOrder({id}).then(res => {
        this.$successMsg();
        this.getList();
      })
    },

    // 删除
    handleDelete(id) {
      deleteOrder({ids: id}).then(res => {
        this.$successMsg();
        this.getList();
      })
    },

    // 批量删除
    batchDelete() {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = this.multipleSelection.map(item => {
          return item.parentId;
        });
        deleteOrder({ids: ids.join(',')}).then(res => {
          this.$successMsg();
          this.getList();
        })
      }).catch(() => {});
    },
    findOrderType(val) {
      const obj = this.orderType.find((value) => value.val === val)
      return obj ? obj.label : ''
    },
    findExamineStatus(val) {
      const obj = this.examineStatus.find((value) => value.val === val)
      return obj ? obj.label : ''
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
