<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库名" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="仓库编码" prop="warehouseCode">
        <el-input
          v-model="queryParams.warehouseCode"
          placeholder="请输入仓库编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="仓库类型" prop="warehouseType">
        <el-select v-model="queryParams.warehouseType" placeholder="请选择类型" clearable size="small">
             <el-option
                v-for="item in wareHouseTypeOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"/>
        </el-select>
      </el-form-item>

      <el-form-item label="联系人" prop="linkUsername">
        <el-input
          v-model="queryParams.linkUsername"
          placeholder="请输入联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="linkPhone">
        <el-input
          v-model="queryParams.linkPhone"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
     <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
           <el-option
                v-for="item in statusOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"/>
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="创建人ID"  prop="createUserId">
        <el-input
          v-model="queryParams.createUserId"
          placeholder="请输入创建人ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="cyan" 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="['oms:warehouse:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['oms:warehouse:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['oms:warehouse:delete']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['oms:warehouse:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warehouseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column  label="ID" width="55"  align="center" prop="warehouseId" />
      <el-table-column  label="仓库名" align="center" prop="warehouseName" />
      <el-table-column  label="仓库编码" width="120"  align="center" prop="warehouseCode" />
      <el-table-column  label="仓库类型" align="center" prop="warehouseType">
        <template slot-scope="scope">
            <el-tag prop="warehouseType" v-for="(item, index) in wareHouseTypeOptions"    v-if="scope.row.warehouseType==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="联系人" align="center" prop="linkUsername" />
      <el-table-column label="联系电话" align="center" prop="linkPhone" />
      <el-table-column width="100" label="所属省市区" align="center" prop="cityIds" />
      <el-table-column label="地址" align="center" prop="address" />

      <el-table-column  label="分配比例" align="center" prop="rate">
        <template slot-scope="scope">
          {{ !!scope.row.rate?parseInt(scope.row.rate*100)+"%":"0%"}}
        </template>
      </el-table-column>

    
       <el-table-column  label="状态" align="center" prop="status">
        <template slot-scope="scope">
            <el-tag prop="status" v-for="(item, index) in statusOptions"    v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>


       <el-table-column label="创建人" align="center" prop="createUserName" />
       <el-table-column width="100" label="创建时间" align="center" prop="createTime" />

       <el-table-column label="备注" align="center" prop="remark" />
    
      <el-table-column width="120" 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="['oms:warehouse:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['oms:warehouse:delete']"
          >删除</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="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="请输入仓库名" />
        </el-form-item>
         <el-form-item label="仓库类型" prop="warehouseType">
          <el-select v-model="form.warehouseType" placeholder="请选择状态" clearable size="small">
            <el-option
                  v-for="item in wareHouseTypeOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
          </el-select>
        </el-form-item>

         <el-form-item label="分配比例" prop="rate">
          <el-input v-model="form.rate" placeholder="请输入仓库分配比例" >
              <template slot="append">%</template>
          </el-input>
        </el-form-item>

        <el-form-item label="联系人" prop="linkUsername">
          <el-input v-model="form.linkUsername" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="linkPhone">
          <el-input v-model="form.linkPhone" placeholder="请输入联系电话" />
        </el-form-item>
     
       <el-form-item   label="所在城市" prop="cityIds">
                <el-cascader
                ref="citySelect"
                v-model="cityIds"
                :options="citys"
                @change="handleCityChange"></el-cascader>
          </el-form-item>


        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态" clearable size="small">
            <el-option
                  v-for="item in statusOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
          </el-select>
        </el-form-item>

      </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 { wlistWarehouse, getWarehouse, delWarehouse, addWarehouse, updateWarehouse, exportWarehouse } from "@/api/oms/warehouse";
import {getCitys} from "@/api/store/city";
export default {
  name: "Warehouse",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库表格数据
      warehouseList: [],
      wareHouseTypeOptions:[],
      statusOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
       cityIds:[],
        citys:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseName: null,
        warehouseCode: null,
        warehouseType: null,
        linkUsername: null,
        linkPhone: null,
        cityIds: null,
        address: null,
        status: null,
        createUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
            warehouseName: [
              { required: true, message: "仓库名称不能为空", trigger: "blur" }
            ],
            linkUsername: [
              { required: true, message: "联系人不能为空", trigger: "blur" }
            ],
            // cityIds: [
            //   { required: true, message: "所在城市不能为空", trigger: "blur" }
            // ],
           
      }
    };
  },
  created() {
     this.getDicts("oms_warehouse_type").then((response) => {
        this.wareHouseTypeOptions = response.data.filter(item => {
             return parseInt(item.dictValue)<=2
        });
     });
     this.getDicts("oms_warehouse_status").then((response) => {
        this.statusOptions = response.data;
     });
    this.getCitys();
    this.getList();
  },
  methods: {
    /** 查询仓库列表 */
    getList() {
      this.loading = true;
      wlistWarehouse(this.queryParams).then(response => {
        this.warehouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        warehouseId: null,
        warehouseName: null,
        warehouseCode: null,
        warehouseType: null,
        linkUsername: null,
        linkPhone: null,
        cityIds: null,
        address: null,
        remark: null,
        rate:null,
        status: '0',
        createUserId: null,
        createTime: null,
        updateTime: 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.warehouseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const warehouseId = row.warehouseId || this.ids
      getWarehouse(warehouseId).then(response => {
        this.form = response.data;
        if(this.form.warehouseType!=null){
            this.form.warehouseType = this.form.warehouseType.toString();
        }
        if(this.form.status!=null){
            this.form.status = this.form.status.toString();
        }
        if(!!this.form.rate){
            this.form.rate = this.form.rate*100;
        }
        this.open = true;
        this.title = "修改仓库";
      });
    },
    getCitys(){
        getCitys().then(res => {
            this.loading = false;
            this.citys=res.data;
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.warehouseId != null) {
            updateWarehouse(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addWarehouse(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const warehouseIds = row.warehouseId || this.ids;
      this.$confirm('是否确认删除仓库编号为"' + warehouseIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWarehouse(warehouseIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有仓库数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWarehouse(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    handleCityChange(value) {
          console.log(value);
          var nodes=this.$refs.citySelect.getCheckedNodes();
          this.form.cityIds=nodes[0].pathLabels[0]+"-"+nodes[0].pathLabels[1]+"-"+nodes[0].pathLabels[2];
          this.form.cityIdsStr=value.toString();
          console.log(this.form.cityIdsStr);
    },
    getCitys(){
          getCitys().then(res => {
             this.loading = false;
             this.citys=res.data;
          });
    },
  }
};
</script>
