<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="票务名称">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入票务名称"
          clearable
          style="width: 200px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="票档名称">
        <el-input
          v-model="queryParams.seatTypeName"
          placeholder="请输入票档名称"
          clearable
          style="width: 200px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="团队名称" v-if="['group'].includes(channelType)">
        <el-input
          v-model="queryParams.teamName"
          placeholder="请输入团队名称"
          clearable
          style="width: 200px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="configPermi.add"
            >价格设置</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="dataList" border>
      <el-table-column label="序号" align="center" type="index" width="50"></el-table-column>
      <el-table-column label="票务名称" align="center" prop="goodsName" />
      <el-table-column label="票档名称" align="center" prop="seatTypeName" />
      <el-table-column width="220" v-if="['group'].includes(channelType)" label="成团要求" align="center" prop="originalAmount11">
        <template slot-scope="scope">
          <div>
            <div 
            v-for="(item,index) in setTourist(scope.row)" 
            :key="index"
            >
              {{ item.label }}{{ item.info }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="!['window','group'].includes(channelType)" :label="setSaleAmounttile1()" align="center" prop="originalAmount" />
      <el-table-column :label="setSaleAmounttile()" align="center" prop="saleAmount" />
      <el-table-column v-if="channelType == 'retail'" label="佣金比例(%)" align="center" prop="retailRate" />
      <el-table-column v-if="['retail'].includes(channelType)" label="佣金(元)" align="center" prop="retailAmount" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="最后修改时间" align="center" prop="updateTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <switchBoxVue  v-model="scope.row.priceStatus" @changeFun="ionlineApi(scope.row)" :active-value="1" />
          </template>
        </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <span v-hasPermi="configPermi.edit" style="display: inline-block;">
            <el-button
              size="mini"
              type="text"
              style="margin-left: 10px;"
              @click="handleEdit(scope.row)"
              v-if="['group'].includes(channelType)"
            >编辑</el-button>
          </span>
          <span v-hasPermi="configPermi.deatils" style="display: inline-block;">
            <el-button
              size="mini"
              type="text"
              style="margin-left: 10px;"
              @click="handleUpdate(scope.row)"
              v-if="!['group'].includes(channelType)"
            >价格日历</el-button>
          </span>

          <span v-hasPermi="configPermi.tuandui" style="display: inline-block;">
            <el-button
              v-if="['group'].includes(channelType)"
              size="mini"
              type="text"
              style="margin-left: 10px;"
              @click="bindTeamFun(scope.row)"
            >绑定团队</el-button>
          </span>
          
          <span v-hasPermi="configPermi.del" style="display: inline-block;">
            <el-button
              size="mini"
              type="text"
              style="margin-left: 10px;"
              @click="handleDelete(scope.row,scope.index)"
            >删除</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增/编辑弹框 -->
    <add-and-edit
      ref="addAndEdit"
      @getList="getList"
      :channelType="channelType"
    />

    <!--  日历价格  -->
    <calendarBox ref="calendarBox" :channelType="channelType" />

    <!--  绑定团队 -->
    <!-- <bindTeamBox ref="bindTeamBox" /> -->

    <!--  绑定团队 -->
    <bindTeamBoxNew :dict="dict" ref="bindTeamBoxNew" />
  </div>
</template>

<script>

import { pageList, deleteById, updateById } from '@/api/priceConfiguration/index'
import addAndEdit from "./dialog/addAndEdit.vue";
import calendarBox from './dialog/calendarBox.vue';
import switchBoxVue from '@/components/switchBox.vue';
import bindTeamBox from './dialog/bindTeamBox.vue';
import bindTeamBoxNew from './dialog/bindTeamBoxNew.vue';
export default {
  name: "PriceConfigurationUilt",
  dicts: ['distribution_type', 'team_type'],
  components: { addAndEdit,calendarBox,switchBoxVue,bindTeamBox,bindTeamBoxNew },
  data() {
    return {
      // 遮罩层
      loading: true,
      configPermi: {
        add: [],
        del: [],
        deatils: [],
        tuandui: []
      },
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: undefined
      },
      /** 
       * 查询的时候 channelType渠道类型: 来自字典channel_type. applet小程序  retail分销 window窗口  group团购
       * 
       */
      channelType: ''
    };
  },
  created() {
    this.channelType = this.$route.query.channelTyp
    this.configPermi = {
        add: [`${this.channelType}:priceConfigurationUitl:add`],
        del: [`${this.channelType}:priceConfigurationUitl:deatils`],
        deatils: [`${this.channelType}:priceConfigurationUitl:deatils`],
        tuandui: [`${this.channelType}:priceConfigurationUitl:tuandui`],
        edit: [`${this.channelType}:priceConfigurationUitl:edit`],
    }
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      pageList(this.addDateRange({ ...this.queryParams,channelType: this.channelType }))
      .then(response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {
        pageNum: 1,
        pageSize: this.queryParams.pageSize
      }
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs["addAndEdit"].openDialog("新增数据", null);
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.$refs["addAndEdit"].openDialog("新增数据", row);
    },
    /** 日历 */
    handleUpdate(row) {
      this.$refs["calendarBox"].openDialog("修改数据", row);
    },

    /**  绑定团队 */
    bindTeamFun(row) {
      // this.$refs["bindTeamBox"].openDialog("绑定团队", row);
      this.$refs["bindTeamBoxNew"].openDialog("绑定团队", row);
    },

    /** 警用和启用 */
    ionlineApi(row) {
      this.$confirm("您确认要将该票的价格信息进行" + (row.priceStatus == 1 ? '禁用吗？' : '启用吗？'), '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateById({ id: row.id, priceStatus: row.priceStatus == 1 ? 0 : 1 }).then((res) => {
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList();
          }
        });
      }).catch(() => {this.getList();});
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('您确认要将该票的价格信息进行删除吗？').then(function() {
        return deleteById({id: row.id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /**  */
    setSaleAmounttile1(){
      let str = '划线价(元)'
      if(this.channelType == "applet") {
        str = "划线价(元)"
      }
      if(this.channelType == "retail") {
        str = "销售价(元)"
      }
      if(this.channelType == "window") {
        str = "划线价(元)"
      }
      if(this.channelType == "group") {
        str = "划线价(元)"
      }
      return str
    },
    /**  */
    setSaleAmounttile(){
      let str = '销售价(元)'
      if(this.channelType == "applet") {
        str = "销售价(元)"
      }
      if(this.channelType == "retail") {
        str = "分销价(元)"
      }
      if(this.channelType == "window") {
        str = "销售价(元)"
      }
      if(this.channelType == "group") {
        str = "销售价(元)"
      }
      return str
    },
    setTourist(row) {
      let list = [
        { label: '价格类型：',info: ["一人一价",'一口价'][row.priceType] },
        { label: '游客限制类型：',info: ["不限制",'限制最低人数','限制人数范围'][row.touristLimit] },
      ]
      if(row.touristLimit == 1) {
        list.push({
          label: '游客限制最小值：',info: row.touristMin + "人"
        })
      }else if(row.touristLimit == 2){
        list.push({
          label: '游客限制：',info: row.touristMin + "人" + "-" +row.touristMax + "人"
        })
      }
      return list
    }
  },
};
</script>
