<template>
  <div>
    <div class="app-container" v-if="showDetail">
      <div class="top">
        <div class="top-left">
          <div @click="showDetail=!showDetail" style="cursor:pointer;">返回</div>
          <div style="width: 0;height: 38px;border: 1px dashed #aaa;padding: 0;"></div>
          <div>编辑站点</div>
        </div>
        <el-button type="primary" @click="submitForm(1)" style="margin: 10px 20px 0 0;">确定</el-button>
      </div>
      <div class="top1"></div>
      <div class="app-title">站点信息</div>
      <el-form :model="form" ref="form" size="small" label-width="100px" :rules="rules">
        <el-row style="padding: 10px 15px;">
          <el-col :span="6">
            <el-form-item label="公司" prop="unitName" :rules="[{required:true}]" :show-colon="true">
              <template>{{$store.state.user.completeName}}</template>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="单位" prop="unitId">
              <el-select v-model="form.unitId" @change="handleUnit" style="width: 100%;">
                <el-option v-for="item in unitList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门（车间）" prop="workshopId" label-width="130px">
              <el-select v-model="form.workshopId" @change="handleWorkShop" style="width: 100%;">
                <el-option v-for="item in workShopList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="站点编号" prop="code">
              <el-input v-model="form.code" placeholder="请输入站点编号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="站点名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入站点名称" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="使用状态" prop="isEnable">
              <el-select v-model="form.isEnable" style="width: 100%;">
                <el-option label="启用" :value="0"></el-option>
                <el-option label="禁用" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <textarea style="width: 100%;height: 100px;opacity: 0.5;" v-model="form.remark"></textarea>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- <div class="app-title">班组信息</div>
      <siteGroup :data="form" />
      <div class="app-title">仓库信息</div>
      <siteWarehouse :data="form" /> -->
    </div>
    <div class="app-container" v-show="!showDetail">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="部门（车间）" prop="workshopName" label-width="130px">
          <el-input
            v-model="queryParams.workshopName"
            placeholder="请输入部门（车间）"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="使用状态" prop="isEnable">
          <el-select v-model="queryParams.isEnable">
            <el-option label="启用" value="0"></el-option>
            <el-option label="禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="站点编号" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入站点编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="站点名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入站点名称"
            clearable
            @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="['business:site:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['business:site:export']"
          >导出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleStencil"
            v-hasPermi="['business:site:export']"
          >导出上传模板</el-button>
        </el-col>
        <el-col :span="1.5">
          <FileUploader
            uploadUrl="/business/site/importData "
            uploadButtonText="导入"
            v-hasPermi="['business:site:import']"
            @refreshTable="getList"
          />
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="siteList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index"/>
        <el-table-column label="单位" align="center" prop="unitName" />
        <el-table-column label="部门（车间）" align="center" prop="workshopName" />
        <el-table-column label="站点名称" align="center" prop="name" />
        <el-table-column label="站点编号" align="center" prop="code" />
        <el-table-column label="使用状态" align="center">
          <template scope="scope">
            {{scope.row.isEnable==0?'启用':scope.row.isEnable==1?'禁用':scope.row.isEnable}}
          </template>
        </el-table-column>
        <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="['business:site:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['business:site: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"
      />
    </div>

    <!-- 添加或修改维修站点-站点对话框 -->
    <el-dialog title="新增站点" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="站点编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入站点编号" />
        </el-form-item>
        <el-form-item label="站点名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入站点名称" />
        </el-form-item>
        <el-form-item label="单位" prop="unitId">
          <el-select v-model="form.unitId" @change="handleUnit" style="width: 100%;">
            <el-option v-for="item in unitList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门（车间）" prop="workshopId">
          <el-select v-model="form.workshopId" @change="handleWorkShop" style="width: 100%;">
            <el-option v-for="item in workShopList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用状态" prop="isEnable">
          <el-select v-model="form.isEnable" style="width: 100%;">
            <el-option label="启用" :value="0"></el-option>
            <el-option label="禁用" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <textarea v-model="form.remark" style="width: 100%;height: 50px;"></textarea>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSite, getSite, delSite, addSite, updateSite } from "@/api/business/site";
import { listUnit } from "@/api/business/unit";
import { listWorkshop } from "@/api/business/workshop";
// import siteGroup from '../siteGroup/index'
// import siteWarehouse from '../siteWarehouse/index'

export default {
  name: "Site",
  // components:{
  //   siteGroup,
  //   siteWarehouse
  // },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修站点-站点表格数据
      siteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: undefined,
        name: undefined,
        unitId: undefined,
        unitName: undefined,
        workshopId: undefined,
        workshopName: undefined,
        isEnable: undefined,
         createTime: undefined,
        isDelete: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "站点名称不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "站点编号不能为空", trigger: "blur" }
        ],
        unitId: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        unitName: [
          { required: true, message: "部门（车间）不能为空", trigger: "blur" }
        ],
        workshopId: [
          { required: true, message: "部门（车间）不能为空", trigger: "blur" }
        ],
        isEnable: [
          { required: true, message: "使用状态不能为空", trigger: "blur" }
        ],
      },
      unitList: [],
      workShopList: [],
      showDetail: false,
    };
  },
  created() {
    this.getList();
    this.getUnit()
  },
  methods: {
    getUnit(){
      listUnit({
        pageNum: 1,
        pageSize: 999,
      }).then(res=>{
        this.unitList=res.rows
      })
    },
    /** 查询维修站点-站点列表 */
    getList() {
      this.loading = true;
      listSite(this.queryParams).then(response => {
        this.siteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        code: undefined,
        name: undefined,
        unitId: undefined,
        unitName: undefined,
        workshopId: undefined,
        workshopName: undefined,
        isEnable: 0,
        remark: undefined,
        createBy: undefined,
         createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        isDelete: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      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.form={...row}
      this.open=true;
      // this.getBan()
      // this.getCang()
      // this.getWorkShop()
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateSite(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.showDetail=false;
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSite(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除维修站点-站点编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delSite(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/site/export', {
        ...this.queryParams,
        ids: this.ids
      }, `维修站点`)
    },
    /** 模板按钮操作 */
    handleStencil() {
      this.download("business/site/importTemplate", {}, `维修站点模板`);
    },
    handleUnit(e){
      console.log(e);
      var item=this.unitList.find(item=>item.id==e)
      if(item){
        this.form.unitName=item.name
        this.form.workshopId=''
        this.form.workshopName=''
        this.getWorkShop()
      }
    },
    handleWorkShop(e){
      var item=this.workShopList.find(item=>item.id==e)
      if(item){
        this.form.workshopId=item.id;
        this.form.workshopName=item.name
      }
    },
    getWorkShop(){
      listWorkshop({
        pageNum: 1,
        pageSize: 999,
        unitId: this.form.unitId
      }).then(res=>{
        this.workShopList=res.rows
      })
    },
    getBan(){

    },
    getCang(){

    }
  }
};
</script>
<style scoped lang="scss">
@import "~@/assets/styles/variables.scss";
.top{
  width: calc(100% - #{$base-sidebar-width});
  height: 50px;
  position: fixed;
  top: 84px;
  right: 0;
  z-index: 99;
  background: #FFF;
  box-shadow: 1px 1px 10px;
  font-size: 16px;
  line-height: 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: width 0.28s;
}
.top-left{
  display: flex;
  align-items: center;
  margin-top: 10px;
}
.top-left>div{
  padding: 0 20px;
  text-align: center;
}
.top1{
  width: 100%;
  height: 50px;
}
.app-title{
  font-size: 16px;
  font-weight: 600;
  padding: 10px 0;
}
.hideSidebar .top {
  width: calc(100% - 54px);
}

.sidebarHide .top {
  width: 100%;
}

.mobile .top {
  width: 100%;
}
</style>