<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @input="resetSearch" clearable standout="bg-primary text-white" v-model="msg.Name" label="项目名称"
            @clear="resetSearch" maxlength="20" />
        </div>
        <div class="col-3">
          <q-select class="q-pb-lg q-pr-lg" filled stack-label clearable option-value="Id" option-label="Name"
            v-model="msg.SaleState" ref="SaleState" :options="saleDownList" label="销售状态" :dense="false" emit-value
            map-options @input="resetSearch">
          </q-select>
        </div>
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat
        class="sticky-column-table sticky-right-column-table" separator="none" :data="dataList" :columns="columns"
        row-key="name" :visible-columns="visibleColumns">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">留学信息</div>
          <q-space />
          <div class="page-option">
            <q-btn color="accent" size="sm" class="q-mr-md" icon="add" label="新增留学" @click="EditStudy(null)" />
          </div>
        </template>
        <template v-slot:body="props">
          <q-tr :props="props">
            <q-td auto-width>
              <q-btn size="sm" color="accent" round dense @click="props.row.expand = !props.row.expand"
                :icon="props.row.expand ? 'remove' : 'add'" />
            </q-td>
            <template v-for="col in props.cols">
              <q-td v-if="col.name == 'Name'" style="width:200px;">
                {{col.value}}
              </q-td>
              <q-td v-else-if="col.name == 'SupplierName' ">
                <div>{{col.value}}</div>
                <div>
                  <a :href="props.row.SupplierContract" style="color:#2961FE;text-decoration:none;"
                    v-if="props.row.SupplierContract&& isHaveViewContractAction" target="_blank">
                    查看合同
                  </a>
                </div>
              </q-td>
              <q-td v-if="col.name=='SchoolName'">
                {{col.value}}
              </q-td>
              <q-td v-if="col.name=='SuggestPrice'">
                {{col.value}}
              </q-td>
              <q-td v-if="col.name=='SellPrice'">
                {{col.value}}
              </q-td>
              <q-td v-if="col.name == 'PreferentialList'" style="padding-right:0px">
                <div v-for="x in col.value">
                  <div class="border-bottom" v-if="x.PriceDiscountType == 1">
                    买{{ x.BuyNum }}送{{ x.SendNum }}
                  </div>
                  <div class="border-bottom" v-if="x.PriceDiscountType == 2">
                    单人报名享
                    <template v-if="x.PriceType==0">
                      {{ 100 - x.PriceMoney }}折
                    </template>
                    <template v-else>
                      {{x.PriceMoney}} 元
                    </template>
                    优惠
                  </div>
                  <div class="border-bottom" v-if="x.PriceDiscountType == 3">
                    双人报名享<template v-if="x.PriceType==0">
                      {{ 100 - x.PriceMoney }}折
                    </template>
                    <template v-else>
                      {{x.PriceMoney}} 元
                    </template>
                    优惠
                  </div>
                  <div class="border-bottom" v-if="x.PriceDiscountType == 4">
                    续费享<template v-if="x.PriceType==0">
                      {{ 100 - x.PriceMoney }}折
                    </template>
                    <template v-else>
                      {{x.PriceMoney}} 元
                    </template>
                    优惠
                  </div>
                  <div class="remark-font" v-if="x.PriceDiscountType == 0">
                    暂无优惠政策
                  </div>
                </div>
                <div v-if="!col.value || col.value.length == 0">
                  <div class="remark-font">暂无优惠政策</div>
                </div>
              </q-td>
              <q-td v-else-if="col.name == 'PreferentialListSellCommission'" style="padding-right:0px;padding-left:0px">
                <div v-for="x in col.value">
                  <div class="border-bottom" style="padding-left: 16px;padding-right: 16px"
                    :class="{ 'text-red': x.SaleCommissionType == 1 }" v-if="x.PriceDiscountType != 0">
                    {{ x.SaleCommissionType == 1 ? "￥" : ""
                  }}{{ x.SaleCommissionMoney
                  }}{{ x.SaleCommissionType == 0 ? "%" : "" }}
                  </div>
                  <div v-if="x.PriceDiscountType == 0">
                    <div class="remark-font">暂无佣金信息</div>
                  </div>
                </div>
                <div v-if="!col.value || col.value.length == 0">
                  <div class="remark-font">暂无佣金信息</div>
                </div>
              </q-td>
              <q-td v-else-if="col.name == 'PreferentialListB2BCommission'" style="padding-left:0px">
                <div v-for="x in col.value">
                  <div class="border-bottom" style="padding-left: 16px"
                    :class="{ 'text-red': x.B2BCommissionType == 1 }" v-if="x.PriceDiscountType != 0">
                    {{ x.B2BCommissionType == 1 ? "￥" : ""
                  }}{{ x.B2BCommissionMoney
                  }}{{ x.B2BCommissionType == 0 ? "%" : "" }}
                  </div>
                  <div v-if="x.PriceDiscountType == 0">
                    <div class="remark-font">暂无佣金信息</div>
                  </div>
                </div>
                <div v-if="!col.value || col.value.length == 0">
                  <div class="remark-font">暂无佣金信息</div>
                </div>
              </q-td>
              <q-td v-else-if="col.name=='EducationCommission'" style="padding-left:0px">
                <div v-for="x in col.value">
                  <div class="border-bottom" style="padding-left: 16px"
                    :class="{ 'text-red': x.EduCommissionType == 1 }" v-if="x.PriceDiscountType != 0">
                    {{ x.EduCommissionType == 1 ? "￥" : ""
                  }}{{ x.EduCommissionMoney
                  }}{{ x.EduCommissionType == 0 ? "%" : "" }}
                  </div>
                  <div v-if="x.PriceDiscountType == 0">
                    <div class="remark-font">暂无佣金信息</div>
                  </div>
                </div>
                <div v-if="!col.value || col.value.length == 0">
                  <div class="remark-font">暂无佣金信息</div>
                </div>
              </q-td>
              <q-td v-else-if="col.name=='SaleStateName'">
                <template v-if="props.row.SaleState==1">
                  {{col.value}}
                </template>
                <template v-if="props.row.SaleState!=1">
                  <a style="cursor:pointer;color:blue;" @click="ShowInfo(props.row)" title="点击查看审核信息"> {{col.value}}</a>
                </template>
              </q-td>
              <q-td v-else-if="col.name == 'Id'" style="width:200px;">
                <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
                  @click="EditStudy(props.row)" />
                <q-btn-dropdown flat size="xs" color="dark" label="更多" style="margin-left:10px;">
                  <q-list>
                    <q-item clickable v-close-popup @click="EditStudyPrice(props.row)">
                      <q-item-section>
                        <q-item-label>价格设置</q-item-label>
                      </q-item-section>
                    </q-item>
                    <q-item clickable v-close-popup v-if="props.row.SaleState==1||props.row.SaleState==4"
                      @click="applyApply(props.row)">
                      <q-item-section>
                        <q-item-label>申请审核</q-item-label>
                      </q-item-section>
                    </q-item>
                    <q-item clickable v-close-popup @click="GoToOrder(props.row)">
                      <q-item-section>
                        <q-item-label>订单中心</q-item-label>
                      </q-item-section>
                    </q-item>
                    <q-item clickable v-close-popup @click="GoToPayment(props.row)">
                      <q-item-section>
                        <q-item-label>收支明细</q-item-label>
                      </q-item-section>
                    </q-item>
                  </q-list>
                </q-btn-dropdown>
              </q-td>
            </template>
          </q-tr>
          <q-tr v-show="props.row.expand" :props="props">
            <q-td colspan="100%" class="bg-grey-2">
              <div style="display:flex;">
                <div style="flex-shrink:0">备注：</div>
                <div style="width:100%;word-break:break-all;word-wrap:break-word;white-space:normal">
                  {{props.row.Remark}}</div>
              </div>
            </q-td>
          </q-tr>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.pageIndex" color="primary" :max="PageCount"
            :input="true" @input="changePage" />
        </template>
      </q-table>
      <studyAbroad-form v-if="isShowStudy" :save-obj="studyObj" :Type="1" @close="closeStudyForm"
        @success="refreshPage">
      </studyAbroad-form>
      <studyAbroadprice-form v-if="isShowStudyPrice" :save-obj="studyObj" :Type="1" @close="closeStudyForm"
        @success="refreshPage">
      </studyAbroadprice-form>
      <studyproauditinfo-form v-if="isShowInfo" :Id="studyObj.Id" @close="closeStudyForm"></studyproauditinfo-form>
    </div>
  </div>
</template>

<script>
  import {
    queryStudyAbroadPage,
    saveSaleState,
    querySaleStateList
  } from '../../api/studyabroad/index'
  import studyAbroadForm from '../../components/studyAbroad/studyAbroad-form'
  import studyAbroadpriceForm from '../../components/studyAbroad/studyAbroadprice-form'
  import studyproauditinfoForm from '../../components/studyAbroad/studyproauditinfo-form'
  import {
    mapState
  } from "vuex";

  export default {
    meta: {
      title: "留学产品"
    },
    components: {
      studyAbroadForm,
      studyAbroadpriceForm,
      studyproauditinfoForm
    },
    data() {
      return {
        msg: {
          Type: 1, //1留学 2就业
          pageIndex: 1,
          pageSize: 10,
          rowsPerPage: 10,
          Name: "",
          IsQPrice: 1,
          SaleState: "" //销售状态
        },
        dataList: [],
        loading: true,
        columns: [{
            name: 'Remark',
            label: '备注',
            field: 'Remark',
            align: 'left'
          },
          {
            name: 'Name',
            label: '项目名称',
            field: 'Name',
            align: 'left'
          },
          {
            name: 'SupplierName',
            label: '供应商',
            field: 'SupplierName',
            align: 'left'
          },
          {
            name: 'SchoolName',
            label: '所属校区',
            field: 'SchoolName',
            align: 'left'
          },
          {
            name: 'SuggestPrice',
            label: '建议卖价',
            field: 'SuggestPrice',
            align: 'left',
            field: row => row.SuggestPrice.toFixed(2)
          },
          {
            name: 'SellPrice',
            label: '实际卖价',
            field: 'SellPrice',
            align: 'left',
            field: row => row.SellPrice.toFixed(2)
          },
          {
            name: "PreferentialList",
            required: true,
            label: "优惠政策",
            align: "left",
            field: row => row.PreferentialList
          },
          {
            name: "PreferentialListSellCommission",
            required: true,
            label: "销售佣金",
            align: "left",
            field: row => row.PreferentialList
          },
          {
            name: "PreferentialListB2BCommission",
            required: true,
            label: "同行佣金",
            align: "left",
            field: row => row.PreferentialList
          },
          {
            name: "EducationCommission",
            required: true,
            label: "教育同行佣金",
            align: "left",
            field: row => row.PreferentialList
          },
          {
            name: 'SaleStateName',
            label: '销售状态',
            field: 'SaleStateName',
            align: 'left'
          },
          {
            name: 'Id',
            label: '操作',
            field: 'Id'
          }
        ],
        //表格可见列
        visibleColumns: [
          'Remark', 'Name', 'SchoolName', 'SuggestPrice', 'SellPrice', "PreferentialList",
          "PreferentialListSellCommission",
          "PreferentialListB2BCommission",
          "EducationCommission", 'SaleStateName', 'Id', 'SupplierName',
        ], //可见列
        PageCount: 0,
        isShowStudy: false, //是否显示留学弹窗
        studyObj: {}, //留学对象
        isShowStudyPrice: false, //是否显示价格设置
        saleDownList: [], //销售状态下拉
        isShowInfo: false, //是否显示审核信息
      }
    },
    computed: mapState({
      //是否有查看合同权限
      isHaveViewContractAction(state) {
        if (state.user.userInfo && state.user.userInfo.ActionMenuList && state.user.userInfo.ActionMenuList.length >
          0) {
          let action = state.user.userInfo.ActionMenuList.find(x => {
            if (x.FunctionCode == "View_Supplier_Contract") {
              return x;
            }
          });
          return action && action.FunctionCode;
        }
        return false;
      },
      //是否有查看供应商权限
      isHaveViewSupplierAction(state) {
        if (state.user.userInfo && state.user.userInfo.ActionMenuList && state.user.userInfo.ActionMenuList.length >
          0) {
          let action = state.user.userInfo.ActionMenuList.find(x => {
            if (x.FunctionCode == "View_Supplier") {
              return x;
            }
          });
          return action && action.FunctionCode;
        }
        return false;
      }
    }),
    mounted() {
      this.getList();
    },
    created() {
      this.getSaleStateList();
      if (!this.isHaveViewSupplierAction) {
        this.visibleColumns.splice(this.visibleColumns.length - 1, 1);
      }
    },
    methods: {
      //跳转到留学就业订单中心
      GoToOrder(item) {
        this.OpenNewUrl("/studyAbroad/studyemploymentorder", {
          SourceId: item.Id
        });
      },
      //跳转到收支明细
      GoToPayment(item) {
        this.OpenNewUrl("/studyAbroad/studypayment", {
          SourceId: item.Id,
          School_Id: item.School_Id,
        });
      },
      //显示详情
      ShowInfo(item) {
        this.studyObj = item;
        this.isShowInfo = true;
      },
      //获取销售状态列表
      getSaleStateList() {
        querySaleStateList().then(res => {
          this.saleDownList = res.Data;
        })
      },
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1;
        this.getList();
      },
      //获取留学分页列表
      getList() {
        this.loading = true;
        queryStudyAbroadPage(this.msg).then(res => {
            this.loading = false;
            var tempArray = res.Data.PageData;
            if (tempArray && tempArray.length > 0) {
              tempArray.forEach(item => {
                item.expand = false;
              })
            }
            this.dataList = tempArray;
            this.PageCount = res.Data.PageCount;
          })
          .catch(() => {
            this.loading = false;
          });
      },
      changePage(val) {
        this.msg.pageIndex = val;
        this.getList();
      },
      //关闭弹窗
      closeStudyForm() {
        this.isShowStudy = false;
        this.isShowStudyPrice = false;
        this.isShowInfo = false;
        this.refreshPage();
      },
      //刷新页面
      refreshPage() {
        this.getList();
      },
      //编辑留学
      EditStudy(obj) {
        if (obj) {
          this.studyObj = obj;
        } else {
          this.studyObj = null;
        }
        this.isShowStudy = true;
      },
      //显示价格设置
      EditStudyPrice(obj) {
        this.studyObj = obj;
        this.isShowStudyPrice = true;
      },
      //申请审核
      applyApply(obj) {
        let applyMsg = {
          Id: obj.Id,
          SaleState: 2
        };
        this.$q.dialog({
          title: '提示信息',
          message: '是否要提交主管审核？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          saveSaleState(applyMsg).then(res => {
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '操作成功',
              position: 'top'
            })
            this.refreshPage();
          })

        }).onCancel(() => {

        });

      }
    }
  }

</script>
<style scoped>
  .border-bottom {
    border-bottom: 1px dashed #EEE;
    padding-bottom: 5px;
    margin-bottom: 5px;
  }

</style>
<style lang="sass">
  @import url('~assets/css/table.sass')

</style>
