<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24" >
        <div class="head-container">
        <!--  <el-input
            v-model="channelName"
            placeholder="请输入渠道名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          /> -->
        </div>
        <div class="head-container" style="overflow-x:scroll">
          <el-tree
            :props="defaultProps"
            ref="tree"
            :load="loadNode"
            lazy
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="渠道名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入渠道名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="mobile">
            <el-input
              v-model="queryParams.mobile"
              placeholder="请输入手机号码"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
         <el-form-item label="地区">
           <el-select
             v-model="queryParams.provinceId"
             placeholder="选择省份"
             clearable
             size="small"
             @change="getCityList()"
             style="width: 120px;margin-right: 1px;">
             <el-option
               v-for="item in provinceList"
               :key="item.areaId"
               :label="item.areaName"
               :value="item.areaId"
             />
           </el-select>
           <el-select
             v-model="queryParams.cityId"
             placeholder="选择市"
             clearable
             size="small"
              @change="getAreaList()"
             style="width: 120px;margin-right: 1px;">
             <el-option
               v-for="item in cityList"
               :key="item.areaId"
               :label="item.areaName"
               :value="item.areaId"
             />
           </el-select>
           <el-select v-model="queryParams.areaId" placeholder="选择区" clearable size="small" style="width: 120px;margin-right: 1px;">
             <el-option
               v-for="item in areaList"
               :key="item.areaId"
               :label="item.areaName"
               :value="item.areaId"
             />
           </el-select>
         </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"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:user:add']"
            >添加一级渠道</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="channelList">
          <el-table-column label="渠道ID" prop="channelId" width="100px" />
          <el-table-column label="渠道名称" prop="name" show-overflow-tooltip  width="180px"/>
          <el-table-column label="手机号码" prop="mobile" show-overflow-tooltip  width="120px"/>
          <el-table-column label="佣金比例" prop="commRate">
            <template slot-scope="scope">
              {{scope.row.commRate}}%
            </template>
          </el-table-column>
          <el-table-column label="渠道级别" prop="level">
            <template slot-scope="scope">
              {{scope.row.level}}级渠道
            </template>
          </el-table-column>
          <el-table-column label="经销商数" prop="siteCnt"  />
          <el-table-column label="用户数" prop="userCnt"  />
          <el-table-column label="状态" key="status">
           <template slot-scope="{ row, column }">
               <span v-if="getValue(row.status) == 1" style="color: blue;"> {{getDesc(row.status)}}</span>
               <span v-if="getValue(row.status) == 2" style="color: red;"> {{getDesc(row.status)}}</span>
           </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="200"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope" v-if="scope.row.userId !== 1">
              <el-button
                size="mini"
                type="text"
                disabled
                @click="handleDetail(scope.row)"
                v-hasPermi="['admin:channel:read']"
              >查看</el-button>
              <el-button
                size="mini"
                type="text"
                disabled
                @click="handleUpdate(scope.row)"
                v-hasPermi="['admin:channel:edit']"
              >编辑</el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleStatusChange(scope.row)"
                v-hasPermi="['admin:channel:edit']"
              >
              <span v-if="getValue(scope.row.status) == 1"> 停用</span>
              <span v-if="getValue(scope.row.status) == 2"> 启用</span>
              </el-button>
             <!-- <el-button
                size="mini"
                type="text"
                disabled
                icon="el-icon-plus"
                @click="handleAdd(scope.row)"
                v-hasPermi="['admin:channel:add']"
              >添加子渠道</el-button> -->
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="pageParams.pageNum"
          :limit.sync="pageParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 新建 -->
    <channel-create
      v-if="createShow"
      :dialog-visible="createShow"
      :edit-id="editId"
      @saveSuccess="submitSuccess"
      @close="hideDialog"
    />

    <!-- 详情 -->
    <channel-detail
      v-if="detailShow"
      :dialog-visible="detailShow"
      :edit-id="editId"
      @close="hideDialog"
    />

  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import { treeChannel, listChannel, updateChannelStatus} from "@/api/admin/channel";
import { listAreaByPid} from "@/api/admin/area";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import ChannelCreate from './components/Create'
import ChannelDetail from './components/Detail'
export default {
  name: "ChannelIndex",
  components: {
    Treeselect,
    ChannelCreate,
    ChannelDetail
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 创建编辑
      createShow: false,
      // 详情
      detailShow: false,
      provinceList:[],
      cityList:[],
      areaList:[],
      // 编辑项ID
      editId: null,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      channelList: null,

      channelName:"",
      // 状态数据字典
      statusOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "name"
      },
      // 查询参数
      queryParams: {
        name: "",
        mobile: "",
        provinceId:"",
        cityId:"",
        areaId:""
      },
      pageParams: {
        pageNum: 1,
        pageSize: 10
      }
    };
  },
  watch: {
    // 根据渠道名称筛选渠道树
    channelName(val) {
      this.$refs.tree.filter(val);
    }
  },
  mounted() {
     this.getProvinceList()
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listChannel('pageNum='+this.pageParams.pageNum + '&pageSize='+this.pageParams.pageSize+'&', this.queryParams).then(response => {
          this.channelList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.parentId = data.channelId;
      this.getList();
    },
    // 懒加载节点
    loadNode(node, resolve) {
      if(node.level === 0){
       treeChannel({}).then(response => {
          return resolve(response.data);
       });
      }else{
        var params = {
          parentId:node.data.channelId
        }
        treeChannel(params).then(response => {
           return resolve(response.data);
        });
      }

    },

    // 省
    getProvinceList(){
      this.cityList = []
      this.areaList = []
      this.queryParams.cityId = ""
      this.queryParams.areaId = ""
      listAreaByPid(0).then(response => {
        // console.log("getProvinceList"+JSON.stringify(response))
         this.provinceList = response || [];
      });
    },

    getCityList(){
      this.cityList = []
      this.areaList = []
      this.queryParams.cityId = ""
      this.queryParams.areaId = ""
      var provinceId = this.queryParams.provinceId
      listAreaByPid(provinceId).then(response => {
        // console.log("getCityList"+JSON.stringify(response))
         this.cityList = response || [];
      });
    },

    getAreaList(){
      var cityId = this.queryParams.cityId
      listAreaByPid(cityId).then(response => {
        // console.log("getAreaList"+JSON.stringify(response))
         this.areaList = response || [];
      });
    },

    // 状态修改
    handleStatusChange(row) {
      var newStatus = this.getValue(row.status) == 2?1:2
      let text = this.getValue(row.status) == 2 ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.name + '"渠道吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          var params={
            channelId:row.channelId,
            status: newStatus
          }
          return updateChannelStatus(params);
        }).then(() => {
          this.msgSuccess(text + "成功");
          this.getList();
        }).catch(function() {
          // row.status = row.status === "0" ? "1" : "0";
        });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams= {
        pageNum: 1,
        pageSize: 10,
         name: "",
         mobile: "",
         provinceId:"",
         cityId:"",
         areaId:""
      },
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleDetail(row){

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.createShow = true
      this.editId = null
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

    },
    // 保存后的操作
    submitSuccess(){
      this.getList();
    },
    /**关闭弹窗*/
    hideDialog(){
      this.createShow = false
      this.detailShow = false
    },

    getDesc(val) {
      // console.log("val == "+val);
      const dataObj = JSON.parse(val);
      return (dataObj && dataObj.desc) || "";
    },

    getValue(val) {
      // console.log("val == "+val);
      const dataObj = JSON.parse(val);
      return (dataObj && dataObj.value) || "";
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const channelId = row.channelId;
      // this.$confirm('是否确认删除编号为"' + channelId + '"的数据项?', "警告", {
      //     confirmButtonText: "确定",
      //     cancelButtonText: "取消",
      //     type: "warning"
      //   }).then(function() {
      //     return delUser(userIds);
      //   }).then(() => {
      //     this.getList();
      //     this.msgSuccess("删除成功");
      //   }).catch(() => {});
    }
  }
};
</script>
<style lang="sass" scoped>

</style>
