<template>
  <div class="app-container">
    <el-form :model="queryParams" size="mini" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
        <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
            @input="onInput()"
          />
        </el-form-item>
        <el-form-item label="经销商名称" prop="chainName">
          <el-input
            v-model="queryParams.chainName"
            placeholder="请输入经销商名称"
            clearable
            @input="onInput()"
          />
        </el-form-item>
        <el-form-item label="是否关户" prop="close">
          <el-select
            v-model="queryParams.close"
            clearable
            placeholder="请选择是否关户"
            @change="onInput">
            <el-option
              v-for="item in isNotOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictLabel">
            </el-option>
          </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chainsList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="经销商ID" align="center" prop="chainId" />-->
      <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"  :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-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['sfa:chains:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['sfa:chains: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="500px" append-to-body>
      <el-form ref="form" :model="form" size="mini" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="18">
            <el-form-item label="经销商编号" prop="chainCode">
              <el-input v-model="form.chainCode" placeholder="请输入经销商编号" />
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="经销商名称" prop="chainName">
              <el-input v-model="form.chainName" placeholder="请输入经销商名称" />
            </el-form-item>
          </el-col>
          <!--        <el-form-item label="删除标记" prop="delFlag">-->
          <!--          <el-input v-model="form.delFlag" placeholder="请输入删除标记" />-->
          <!--        </el-form-item>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChains, getChains, delChains, addChains, updateChains } from "@/api/sfa/chains";
import tree from "@/views/components/sfa/treeselect";
export default {
  name: "Chains",
    components: { tree },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 经销商表格数据
      chainsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chainCode: null,
        chainName: null,
          // valid:'0'
        close:"否"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
          chainCode:[ { required: true, message: '请输入经销商编号', trigger: 'blur' }],
          chainName:[ { required: true, message: '请输入经销商名称', trigger: 'blur' }],
      },
        isNotOptions:[],
    };
  },
  created() {
      // 是否
      this.getDicts("is_not").then(response => {
          this.isNotOptions = response.data;
      });
    this.getList();
  },
  methods: {
      onInput (){
          this.$forceUpdate();
      },
      treeselectFn(val){
          this.queryParams.orgIds = val;
          this.$forceUpdate();
          // this.getChainList();
      },
    /** 查询经销商列表 */
    getList() {
      this.loading = true;
      listChains(this.queryParams).then(response => {
        this.chainsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        chainId: null,
        chainCode: null,
        chainName: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    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 = "添加经销商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const chainId = row.chainId || this.ids
      getChains(chainId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改经销商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.chainId != null) {
            updateChains(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChains(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const chainIds = row.chainId || this.ids;
      // this.$modal.confirm('是否确认删除经销商编号为"' + chainIds + '"的数据项？').then(function() {
      this.$modal.confirm('是否确认删除选中信息？').then(function() {
        return delChains(chainIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sfa/chains/export', {
        ...this.queryParams
      }, `chains_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
