<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item class="treeselectForm treeselectForm2 treeselectFormz" label="组织架构" prop="orgIds" style="width: auto">
        <tree @treeselectFn="treeselectFn" :treeval="queryParams.orgIds" ></tree>
      </el-form-item>
      <el-form-item label="经销商编号" prop="chainCode">
        <el-input
          v-model="queryParams.chainCode"
          placeholder="请输入经销商编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经销商名称" prop="chainName">
        <el-input
          v-model="queryParams.chainName"
          placeholder="请输入经销商名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="组织机构id" prop="orgId">-->
<!--        <el-input-->
<!--          v-model="queryParams.orgId"-->
<!--          placeholder="请输入组织机构id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="组织机构code" prop="orgCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.orgCode"-->
<!--          placeholder="请输入组织机构code"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="组织机构" prop="orgName">-->
<!--        <el-input-->
<!--          v-model="queryParams.orgName"-->
<!--          placeholder="请输入组织机构"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @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="['sfa:chainsRoute:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['sfa:chainsRoute:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['sfa:chainsRoute:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sfa:chainsRoute:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chainsRouteList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="${comment}" align="center" prop="chailsRouteId" />-->
      <el-table-column label="经销商编号" align="center" prop="chainCode" />
      <el-table-column label="经销商名称" align="center" prop="chainName" />
      <el-table-column label="组织机构id" align="center" prop="orgId" />
      <el-table-column label="组织机构code" align="center" prop="orgCode" />
      <el-table-column label="组织机构" align="center" prop="orgName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sfa:chainsRoute:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改经销商负责人可排线配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form :model="queryParamsChains" size="mini" ref="queryParamsChains" :inline="true" v-show="showSearch" label-width="90px">
        <el-form-item class="treeselectForm treeselectForm2 treeselectFormz" label="组织架构" prop="orgIds" style="width: auto">
          <tree @treeselectFn="treeselectFns" :treeval="queryParamsChains.orgIds" ></tree>
        </el-form-item>
        <el-form-item label="经销商编号" prop="chainCode">
          <el-input
            v-model="queryParamsChains.chainCode"
            placeholder="请输入经销商编号"
            clearable
            @input="onInput()"
          />
        </el-form-item>
        <el-form-item label="经销商名称" prop="chainName">
          <el-input
            v-model="queryParamsChains.chainName"
            placeholder="请输入经销商名称"
            clearable
            @input="onInput()"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQueryChains">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQueryChains">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table ref="multipleTable" v-loading="loadingChains" :data="chainsListChains" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="组织架构" align="center" width="180"  prop="orgName" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="经销商编号" align="center"  width="140" prop="chainCode" >
          <template slot-scope="scope">
            <span class="btn" :data-clipboard-text="scope.row.chainCode">{{scope.row.chainCode}}&nbsp;&nbsp;<i class="el-icon-document-copy"></i></span>
          </template>
        </el-table-column>
        <el-table-column label="经销商名称" align="center" prop="chainName"  width="170"  :show-overflow-tooltip="true" />
        <el-table-column label="省" align="center" prop="provinceName" width="100"/>
        <el-table-column label="市" align="center" prop="cityName" width="100" :show-overflow-tooltip="true"/>
        <el-table-column label="区（县）" align="center" prop="districtName" width="100" :show-overflow-tooltip="true"/>
        <el-table-column label="客户分类1" align="center" prop="typeName1" width="100" :show-overflow-tooltip="true"/>
        <el-table-column label="客户分类2" align="center" prop="typeName2" width="100" :show-overflow-tooltip="true"/>
        <el-table-column label="客户分类3" align="center" prop="typeName3" width="100" :show-overflow-tooltip="true"/>
        <el-table-column label="姓名" align="center" prop="nickName" width="100" :show-overflow-tooltip="true"/>
        <el-table-column label="员工号" align="center" prop="workCode" width="100" :show-overflow-tooltip="true"/>
      </el-table>

      <pagination
        v-show="totalChains>0"
        :total="totalChains"
        :page.sync="queryParamsChains.pageNum"
        :limit.sync="queryParamsChains.pageSize"
        @pagination="getListChains"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="经销商详情" :visible.sync="openView" width="960px" append-to-body>
      <el-descriptions title="" border>
        <el-descriptions-item label="销售部名称">{{formView.orgName}}</el-descriptions-item>
        <el-descriptions-item label="经销商代码">{{formView.chainCode}}</el-descriptions-item>
        <el-descriptions-item label="经销商名称">{{formView.chainName}}</el-descriptions-item>
      </el-descriptions>
      <br>
      <el-table v-loading="loadingChains" :data="formView.chainsBossList" v-if="formView.chainsBossList.length>0">
        <el-table-column label="负责人账号" align="center"  prop="userName" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="负责人" align="center" prop="nickName"    :show-overflow-tooltip="true" />
        <el-table-column label="负责人岗位" align="center" prop="postName"/>
        <el-table-column label="负责人电话" align="center" prop="phonenumber"  :show-overflow-tooltip="true"/>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView=false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChainsRoute, getChainsRoute, delChainsRoute, addChainsRoute, updateChainsRoute } from "@/api/sfa/chainsRoute";
import tree from "@/views/components/sfa/treeselect.vue";
import {listChains} from "@/api/sfa/chains";

export default {
  name: "ChainsRoute",
  components: {tree},
  data() {
    return {
      // 遮罩层
      loading: true,
      loadingChains: true,
      // 选中数组
      ids: [],
      chainsListChains:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalChains:0,
      openView:false,
      // 经销商负责人可排线配置表格数据
      chainsRouteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryParamsChains:{
        pageNum: 1,
        pageSize: 10,
        close:"否",
        orgId: null,
        orgCode: null,
        orgName: null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chainCode: null,
        chainName: null,
        orgId: null,
        orgCode: null,
        orgName: null,
      },
      // 表单参数
      form: {},
      formView:{
        chainsBossList:[]
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getListChains()
    this.getList();
  },
  methods: {
    getListChains() {
      this.loadingChains = true;
      var formData=this.queryParamsChains
      if(formData.orgIds!=null){
        if(formData.orgIds[0]==null){
          formData.orgIds=null
        }
      }
      listChains(this.queryParamsChains).then(response => {
        this.chainsListChains = response.rows;
        this.totalChains = response.total;
        this.loadingChains = false;
      });
    },
    /** 查询经销商负责人可排线配置列表 */
    getList() {
      this.loading = true;
      listChainsRoute(this.queryParams).then(response => {
        this.chainsRouteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        chailsRouteId: null,
        chainCode: null,
        chainName: null,
        orgId: null,
        orgCode: null,
        orgName: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
      };
      this.resetForm("form");

    },
    /** 搜索按钮操作 */
    handleQueryChains() {
      this.queryParamsChains.pageNum = 1;
      this.getListChains();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;

      this.getList();
    },
    onInput (){
      this.$forceUpdate();
    },
    treeselectFns(val){
      this.queryParamsChains.orgIds = val;
      this.$forceUpdate();
      // this.getChainList();
    },
    treeselectFn(val){
      this.queryParams.orgIds = val;
      this.$forceUpdate();
      // this.getChainList();
    },
    /** 重置按钮操作 */
    resetQueryChains() {
      this.resetForm("queryParamsChains");
      this.handleQueryChains();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.chainId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加经销商负责人可排线配置";
      this.$nextTick(()=>{
        this.$refs.multipleTable.clearSelection();

      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const chailsRouteId = row.chailsRouteId || this.ids
      getChainsRoute(chailsRouteId).then(response => {
        this.formView = response.data;
        this.openView = true;
        this.title = "修改经销商负责人可排线配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if(this.ids.length>0){
        this.form.chainIdList=this.ids
        addChainsRoute(this.form).then(response => {
          if(response.code==200){
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }
        });
      }else{
        this.$modal.msgWarning("请选择经销商");
      }

      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.chailsRouteId != null) {
      //       updateChainsRoute(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addChainsRoute(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const chailsRouteIds = row.chailsRouteId || this.ids;
      this.$modal.confirm('是否确认删除经销商负责人可排线配置编号为"' + chailsRouteIds + '"的数据项？').then(function() {
        return delChainsRoute(chailsRouteIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sfa/chainsRoute/export', {
        ...this.queryParams
      }, `chainsRoute_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
