<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item class="treeselectForm"  label="公司名称" >
        <el-select size="mini"   v-model="orgId" clearable placeholder="请选择公司名称" @change="onInput" multiple collapse-tags  style="width: 100%" >
          <el-option
            v-for="item in menuOptions"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="门店类型"  class="treeselectForm">
        <el-select filterable  size="mini"  v-model="storeCategory" clearable placeholder="请选择门店类型" @change="onInput" multiple collapse-tags style="width: 100%">
          <el-option
            v-for="item in sfastoretypeOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </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"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sfa:productItem: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:productItem: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:productItem: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:productItem:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productItemList" @selection-change="handleSelectionChange">
      <el-table-column label="公司名称" align="center" prop="orgName" />
      <el-table-column label="门店类型" align="center" prop="storeCategory"  >
        <template slot-scope="scope">
          {{storeCategoryFormatter(scope.row,scope.column)}}
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="url"  width="100px">
        <template slot-scope="scope">
          <el-image
            style="height: 26px;"
            :src="scope.row.url"
            :preview-src-list="getarray(scope.row.url)">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160px"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120px" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sfa:productItem:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            class="f-red"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sfa:productItem: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" :rules="rules" label-width="80px">
        <el-form-item label="公司名称" prop="orgId" >
          <el-select size="mini"   v-model="form.orgId" clearable placeholder="请选择公司名称" @change="onInput"  style="width: 100%">
            <el-option
              v-for="item in menuOptions"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门店类型" prop="storeCategory">
          <el-select size="mini"   v-model="form.storeCategory" clearable placeholder="请选择门店类型" @change="onInput"  style="width: 100%">
            <el-option
              v-for="item in sfastoretypeOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="url">
          <images-upload  v-model="form.url"></images-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProductItem,
  getProductItem,
  delProductItem,
  addProductItem,
  updateProductItem,
  companyList
} from "@/api/sfa/productItem";
import tree from "@/views/components/sfa/treeselectParentNo.vue";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listDept} from "@/api/system/dept";
export default {
  name: "ProductItem",
  components: {tree,Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 经营品项指导配置表格数据
      productItemList: [],
      menuOptions: [],
      orgId:[],
      storeCategory:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgId: null,
        storeCategory: null,
        url: null,
      },
      // 表单参数
      form: {},
      validTree:true,
      // 表单校验
      rules: {
        orgId: [
          { required: true, message: "组织架构不能为空", trigger: "change" }
        ],
        storeCategory: [
          { required: true, message: "门店类型不能为空", trigger: "change" }
        ],
        url: [
          { required: true, message: "图片地址不能为空", trigger: "change" }
        ]
      },
      sfastoretypeOptions:[]
    };
  },
  created() {
    this.getDicts("sfa_store_type").then(response => {
      this.sfastoretypeOptions = response.data;
    });
    this.getList();
    this.getTreeselect()
  },
  methods: {
    onInput (){
      this.$forceUpdate();
    },
    getarray(url){
      var array = [];
      array.push(url);
      return array;
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      companyList().then(response => {
        this.menuOptions =response.data})
    },
    /** 查询经营品项指导配置列表 */
    getList() {
      this.loading = true;
      this.queryParams.storeCategory=this.storeCategory.join(",")
      this.queryParams.orgIdStr=this.orgId.join(",")
      listProductItem(this.queryParams).then(response => {
        this.productItemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 门店类型
    storeCategoryFormatter(row,column){
      return this.selectDictLabel(this.sfastoretypeOptions, row.storeCategory);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orgId: null,
        storeCategory: null,
        url: null,
        createTime: null,
        createBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.storeCategory=[]
      this.orgId=[]
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加经营品项指导配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductItem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改经营品项指导配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductItem(this.form).then(response => {
              if(response.code==200){
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();}
            });
          } else {
            addProductItem(this.form).then(response => {
              if(response.code==200){
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();}
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      var slef=this;
      this.$modal.confirm('是否确认删除公司名称为"' + row.orgName + '"的数据项？').then(function() {
         delProductItem(ids).then(response => {
          if(response.code==200){
            slef.getList();
            slef.$modal.msgSuccess("删除成功");
          }
        });
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.storeCategory=this.storeCategory.join(",")
      this.queryParams.orgIdStr=this.orgId.join(",")
      this.download('sfa/productItem/export', {
        ...this.queryParams
      }, `productItem_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
