<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 filled 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 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.ProductType" :options="productList" label="产品类型" :dense="false" emit-value map-options
            @input="resetSearch" />
        </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.StudyCountryId" :options="CountryList" label="就业国家" :dense="false" emit-value map-options
            @input="resetSearch" />
        </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>
          <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>
            <q-td key="Name" :props="props" style="width:100px;">
              {{props.row.Name}}
            </q-td>
            <q-td key="SaleStateName">
              <template v-if="props.row.SaleState==1">
                {{props.row.SaleStateName}}
              </template>
              <template v-if="props.row.SaleState!=1">
                <a style="cursor:pointer;color:blue;" @click="ShowInfo(props.row)" title="点击查看审核信息">
                  {{props.row.SaleStateName}}</a>
              </template>
            </q-td>
            <q-td key="SupplierName" :props="props">
              <div>{{props.row.SupplierName}}</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 key="SchoolName" :props="props">
              {{props.row.SchoolName}}
            </q-td>
            <q-td key="SuggestPrice" :props="props">
              {{props.row.SuggestPrice}}
            </q-td>
            <q-td key="SellPrice" :props="props">
              {{props.row.SellPrice}}
            </q-td>
            <q-td key="B2CRatio" :props="props">
              {{props.row.B2CRatio}}%
            </q-td>
            <q-td key="B2CReNewRatio" :props="props">
              {{props.row.B2CReNewRatio}}%
            </q-td>
            <q-td key="B2BRebateRatio" :props="props">
              {{props.row.B2BRebateRatio}}{{props.row.CommissionReType==1?'%':''}}
            </q-td>
            <q-td key="B2BReNewRatio" :props="props">
              {{props.row.B2BReNewRatio}}{{props.row.CommissionReType==1?'%':''}}
            </q-td>
            <q-td key="SchoolRebateRatio" :props="props">
              {{props.row.SchoolRebateRatio}}{{props.row.CommissionReType==1?'%':''}}
            </q-td>
            <q-td key="SchoolReNewRatio" :props="props">
              {{props.row.SchoolReNewRatio}}{{props.row.CommissionReType==1?'%':''}}
            </q-td>
            <q-td key="TransIntroductceRatio" :props="props">
              {{props.row.TransIntroductceRatio}}{{props.row.CommissionReType==1?'%':''}}
            </q-td>
            <q-td key="TransIntroductceReNewRatio" :props="props">
              {{props.row.TransIntroductceReNewRatio}}{{props.row.CommissionReType==1?'%':''}}
            </q-td>
            <q-td key="InnerRecommendRatio" :props="props">
              {{props.row.InnerRecommendRatio}}{{props.row.CommissionReType==1?'%':''}}
            </q-td>

            <q-td key="InnerRecommendReNewRatio" :props="props">
              {{props.row.InnerRecommendReNewRatio}}{{props.row.CommissionReType==1?'%':''}}
            </q-td>
            <q-td key="B2CRbRatio" :props="props">
              {{props.row.B2CRbRatio}}{{props.row.CommissionReType==1?'%':''}}
            </q-td>
            <q-td key="B2CRNRatio" :props="props">
              {{props.row.B2CRNRatio}}{{props.row.CommissionReType==1?'%':''}}
            </q-td>
            <!-- <q-td key="PreferentialList" style="padding-right:0px">
              <div v-for="(x,index) in props.row.PreferentialList" :key="index">
                <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">
                    {{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">
                    {{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">
                    {{x.PriceMoney}}%
                  </template>
                  <template v-else>
                    {{x.PriceMoney}} 元
                  </template>
                </div>
                <div class="remark-font" v-if="x.PriceDiscountType == 0">
                  暂无优惠政策
                </div>
              </div>
              <div v-if="! props.row.PreferentialList ||  props.row.PreferentialList.length == 0">
                <div class="remark-font">暂无优惠政策</div>
              </div>
            </q-td>
            <q-td key="PreferentialListSellCommission" style="padding-right:0px;padding-left:0px">
              <div v-for="(x,index) in props.row.PreferentialListSellCommission" :key="index">
                <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="!props.row.PreferentialListSellCommission || props.row.PreferentialListSellCommission.length == 0">
                <div class="remark-font">暂无佣金信息</div>
              </div>
            </q-td>
            <q-td key="PreferentialListB2BCommission" style="padding-left:0px">
              <div v-for="(x,index) in props.row.PreferentialListB2BCommission" :key="index">
                <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="!props.row.PreferentialListB2BCommission || props.row.PreferentialListB2BCommission.length == 0">
                <div class="remark-font">暂无佣金信息</div>
              </div>
            </q-td>
            <q-td key="EducationCommission" style="padding-left:0px">
              <div v-for="(x,index) in props.row.EducationCommission" :key="index">
                <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="!props.row.EducationCommission || props.row.EducationCommission.length == 0">
                <div class="remark-font">暂无佣金信息</div>
              </div>
            </q-td> -->

            <q-td key="Id" style="width:200px;">
              <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
                @click="EditStudy(props.row)" />
              <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="价格设置"
                @click="EditStudyPrice(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||props.row.SaleState==5"
                    @click="applyApply(props.row,2)">
                    <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-item clickable v-close-popup v-if="props.row.SaleState==3" @click="applyApply(props.row,5)">
                    <q-item-section>
                      <q-item-label>下架</q-item-label>
                    </q-item-section>
                  </q-item>
                  <q-item clickable v-close-popup @click="removeStudy(props.row,1)" v-if="props.row.Status==0">
                    <q-item-section>
                      <q-item-label>删除</q-item-label>
                    </q-item-section>
                  </q-item>
                  <q-item clickable v-close-popup @click="removeStudy(props.row,0)" v-if="props.row.Status==1">
                    <q-item-section>
                      <q-item-label>恢复</q-item-label>
                    </q-item-section>
                  </q-item>
                </q-list>
              </q-btn-dropdown>
            </q-td>
          </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="2" @close="closeStudyForm"
        @success="refreshPage">
      </studyAbroad-form>
      <studyAbroadprice-form v-if="isShowStudyPrice" :save-obj="studyObj" :Type="2" @close="closeStudyForm"
        @success="refreshPage(),isShowStudyPrice=false">
      </studyAbroadprice-form>
      <studyproauditinfo-form v-if="isShowInfo" :Id="studyObj.Id" @close="closeStudyForm"></studyproauditinfo-form>
    </div>
  </div>
</template>

<script>
  import {
    queryStudyAbroadPage,
    saveSaleState,
    querySaleStateList,
    deleteStudyAbroad,
    queryStudyAbroadProductType
  } from '../../api/studyabroad/index'
  import {
    queryCountryList
  } from '../../api/common/common'
  import studyAbroadForm from '../../components/studyAbroad/studyAbroad-form'
  import studyAbroadpriceForm from '../../components/studyAbroad/newstudyAbroadprice-form'
  import studyproauditinfoForm from '../../components/studyAbroad/studyproauditinfo-form'
  import {
    mapState
  } from "vuex";
  export default {
    meta: {
      title: "就业产品"
    },
    components: {
      studyAbroadForm,
      studyAbroadpriceForm,
      studyproauditinfoForm
    },
    data() {
      return {
        msg: {
          Type: 2, //1留学 2就业
          pageIndex: 1,
          pageSize: 10,
          rowsPerPage: 10,
          Name: "",
          IsQPrice: 1,
          SaleState: "", //销售状态
          ProductType: "", //产品类型
          StudyCountryId: "", //国家编号
        },
        dataList: [],
        loading: true,
        columns: [{
            name: 'Remark',
            label: '备注',
            field: 'Remark',
            align: 'left'
          },
          {
            name: 'Name',
            label: '项目名称',
            field: 'Name',
            align: 'left'
          },
          {
            name: 'SaleStateName',
            label: '销售状态',
            field: 'SaleStateName',
            align: 'left'
          },
          {
            name: 'SupplierName',
            label: '供应商',
            field: 'SupplierName',
            align: 'left'
          },
          // {
          //   name: 'StudyCountryName',
          //   label: '留学国家',
          //   field: 'StudyCountryName',
          //   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: "B2CRatio",
            required: true,
            label: "直客优惠",
            align: "left",
            field: (row) => row.B2CRatio
          },
          {
            name: "B2CReNewRatio",
            required: true,
            label: "直客优惠续费",
            align: "left",
            field: (row) => row.B2CReNewRatio
          },
          {
            name: "B2BRebateRatio",
            required: true,
            label: "一般同行返佣",
            align: "left",
            field: (row) => row.B2BRebateRatio
          },
          {
            name: "B2BReNewRatio",
            required: true,
            label: "一般同行续费返佣",
            align: "left",
            field: (row) => row.B2BReNewRatio
          },
          {
            name: "SchoolRebateRatio",
            required: true,
            label: "校园同行返佣",
            align: "left",
            field: (row) => row.SchoolRebateRatio
          },
          {
            name: "SchoolReNewRatio",
            required: true,
            label: "校园同行续费返佣",
            align: "left",
            field: (row) => row.SchoolReNewRatio
          },
          {
            name: "TransIntroductceRatio",
            required: true,
            label: "转介返佣",
            align: "left",
            field: (row) => row.TransIntroductceRatio
          },
          {
            name: "TransIntroductceReNewRatio",
            required: true,
            label: "转介续费返佣",
            align: "left",
            field: (row) => row.TransIntroductceReNewRatio
          },
          {
            name: "InnerRecommendRatio",
            required: true,
            label: "内推返佣",
            align: "left",
            field: (row) => row.InnerRecommendRatio
          },
          {
            name: "InnerRecommendReNewRatio",
            required: true,
            label: "内推续费返佣",
            align: "left",
            field: (row) => row.InnerRecommendReNewRatio
          },
          {
            name: "B2CRbRatio",
            required: true,
            label: "直客返佣",
            align: "left",
            field: (row) => row.B2CRbRatio
          },
          {
            name: "B2CRNRatio",
            required: true,
            label: "直客续费返佣",
            align: "left",
            field: (row) => row.B2CRNRatio
          },
          // {
          //   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: 'Id',
            label: '操作',
            field: 'Id'
          }
        ],
        //表格可见列
        visibleColumns: [
          'Remark', 'Name', 'StudyCountryName', 'SchoolName', 'SuggestPrice', 'SellPrice', "PreferentialList",
          "PreferentialListSellCommission",
          "PreferentialListB2BCommission",
          "EducationCommission", 'SaleStateName', 'Id', 'SupplierName', 'ProductTypeName',
        ], //可见列
        PageCount: 0,
        isShowStudy: false, //是否显示留学弹窗
        studyObj: {}, //留学对象
        isShowStudyPrice: false, //是否显示价格设置
        saleDownList: [], //销售状态下拉
        isShowInfo: false, //是否显示审核信息
        productList: [], //产品类型列表
        CountryList: [], //国家列表
      }
    },
    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();
      this.getCountryList();
      this.getProductType();
      if (!this.isHaveViewSupplierAction) {
        this.visibleColumns.splice(this.visibleColumns.length - 1, 1);
      }
    },
    methods: {
      //获取国家列表
      getCountryList() {
        queryCountryList({}).then(res => {
          if (res.Code == 1) {
            this.CountryList = res.Data;
          }
        });
      },
      //获取产品类型列表
      getProductType() {
        queryStudyAbroadProductType({}).then(res => {
          this.productList = res.Data;
        });
      },
      //删除留学就业产品
      removeStudy(item, status) {
        let delMsg = {
          Id: item.Id,
          Status: status
        };
        var message = "是否要删除该产品？";
        if (status == 0) {
          message = "是否要恢复该产品？";
        }
        this.$q.dialog({
          title: '提示信息',
          message: message,
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          deleteStudyAbroad(delMsg).then(res => {
            if (res.Code == 1) {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '删除成功！',
                position: 'top'
              })
              this.getList();
            } else {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: res.Message,
                position: 'top'
              })
            }
          })
        }).onCancel(() => {

        });
      },
      //跳转到留学就业订单中心
      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, SaleState) {
        let applyMsg = {
          Id: obj.Id,
          SaleState: SaleState
        };
        var str = "是否要提交主管审核？";
        if (SaleState == 5) {
          str = "是否要下架此产品？";
        }
        this.$q.dialog({
          title: '提示信息',
          message: str,
          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>