<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>
          <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="ProductTypeName" :props="props" style="width:100px;">
              {{props.row.ProductTypeName}}
            </q-td>
            <q-td key="Name" :props="props" style="width:100px;">
              {{props.row.Name}}
            </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="StudyCountryName" :props="props">
              {{props.row.StudyCountryName}}
            </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="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="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="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||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="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,
    deleteStudyAbroad
  } 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: 'ProductTypeName',
            label: '类型',
            field: 'ProductTypeName',
            align: 'left'
          },
          {
            name: 'Name',
            label: '项目名称',
            field: 'Name',
            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: "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', 'StudyCountryName', 'SchoolName', 'SuggestPrice', 'SellPrice', "PreferentialList",
          "PreferentialListSellCommission",
          "PreferentialListB2BCommission",
          "EducationCommission", 'SaleStateName', 'Id', 'SupplierName', 'ProductTypeName'
        ], //可见列
        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: {
      //删除留学就业产品
      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>