<template>
  <div id="MaterialClassDivision">
    <div v-if="isList">
      <el-card style="position: relative;">
      <el-form class="search_area" label-width="130px">
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-form-item label="物料一级大类编码">
              <el-input
                v-model="queryParams.materialClassifyOne"
                clearable
                size="mini"
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="物料一级大类名称">
              <el-input
                v-model="queryParams.materialClassifyOneName"
                clearable
                size="mini"
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="物料二级大类编码">
              <el-input
                v-model="queryParams.materialClassifyTwo"
                clearable
                size="mini"
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="物料二级大类名称">
              <el-input
                v-model="queryParams.materialClassifyTwoName"
                clearable
                size="mini"
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="1.5"> -->
            <!-- <el-form-item label="" label-width="20px"> -->
            <div style="position: absolute;top: 3px;right: 10px;">
              <el-button type="primary" size="mini" @click="searchList">搜索</el-button>
              <el-button size="mini" plain @click="resetList">重置</el-button>
            </div>
            <!-- </el-form-item> -->
          <!-- </el-col> -->
        </el-row>

        <CollapseTransition>
          <div v-show="expanded">
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-form-item label="物料三级大类编码">
              <el-input
                v-model="queryParams.materialClassifyThree"
                clearable
                size="mini"
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="物料三级大类名称">
              <el-input
                v-model="queryParams.materialClassifyThreeName"
                clearable
                size="mini"
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="物料四级大类编码">
              <el-input size="mini" style="width: 200px" readonly v-model="queryParams.materialClassify">
                <el-button slot="append" icon="el-icon-more" @click="chooseFourClass"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="物料四级大类名称">
              <el-input
                v-model="queryParams.materialClassifyFourName"
                clearable
                size="mini"
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-form-item label="物料编码">
              <el-input style="width: 200px" size="mini" readonly v-model="queryParams.materialCode">
                  <el-button slot="append" icon="el-icon-more" @click="test01"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="物料名称">
              <el-input
                v-model="queryParams.materialName"
                clearable
                size="mini"
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="订单员">
              <el-select
                  ref="staffs"
                  size="mini"
                  style="width: 200px"
                  v-model="queryParams.orderPersonal"
                  clearable
                  @focus="chooseRefer('CONTACTS_PARAM', true, '订单员')"
                >
                  <el-option
                    v-for="item in staffOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.code"
                  />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="采购员">
              <el-select
                  ref="staffs2"
                  size="mini"
                  style="width: 200px"
                  v-model="queryParams.buyer"
                  clearable
                  @focus="chooseRefer('CONTACTS_PARAM', true, '采购员')"
                >
                  <el-option
                    v-for="item in staff2Options"
                    :key="item.id"
                    :label="item.name"
                    :value="item.code"
                  />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-form-item label="生产厂家">
            <el-input
              v-model="queryParams.manufacturer"
              size="mini"
              placeholder=""
              clearable
              style="width: 200px"
            />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="备注">
            <el-input
              v-model="queryParams.remark"
              size="mini"
              placeholder=""
              clearable
              style="width: 200px"
            />
            </el-form-item>
          </el-col>
        </el-row>
        </div>
      </CollapseTransition>
      </el-form>
      <el-divider class="lines"><i style="cursor: pointer;" :class="expanded?'el-icon-arrow-up':'el-icon-arrow-down'" @click="drop"></i></el-divider>

        <div class="btn_grooup">
          <el-button type="primary" size="mini" @click="addDivision">新增</el-button>

          <!-- <el-dropdown size="mini" @command="handleCommand">
            <el-button size="mini" type="primary" style="margin-left: 10px;">
              导入<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="数据导入">数据导入</el-dropdown-item>
              <el-dropdown-item command="模板下载">模板下载</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

          <el-dropdown size="mini" @command="handleCommand">
            <el-button size="mini" type="primary" style="margin: 0 10px;">
              导出<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="Excel导出">Excel导出</el-dropdown-item>
              <el-dropdown-item command="导出明细">导出明细</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown> -->

          <el-button type="primary" size="mini" @click="deletes">删除</el-button>
        </div>
        <el-table 
          :data="tableList" 
          fit
          :cell-style="{ borderColor: '#c0c0c0' }"
          :header-cell-style="{ borderColor: '#c0c0c0' }"
          class="exporttable"
          border
          height="390"
          max-height="390"
          style="font-size: 12px;"
          @selection-change="handleSelectionChange"
          @row-click="rowSelect"
          @row-dblclick="doubleClick"
          ref="table"
        >
          <el-table-column show-overflow-tooltip type="selection" width="55" fixed="left"/>
          <el-table-column show-overflow-tooltip label="序号" type="index" align="center" width="50px" fixed="left"/>
          <el-table-column show-overflow-tooltip label="物料分类编码" align="center" width="120" prop="materialClassify"/>
          <!-- <el-table-column show-overflow-tooltip label="物料分类名称" align="center" width="200" prop="materialClassifyName" /> -->
          <el-table-column show-overflow-tooltip label="一级分类名称" align="center" width="120" prop="materialClassifyOneName" />
          <el-table-column show-overflow-tooltip label="二级分类名称" align="center" width="120" prop="materialClassifyTwoName" />
          <el-table-column show-overflow-tooltip label="三级分类名称" align="center" width="120" prop="materialClassifyThreeName"/>
          <el-table-column show-overflow-tooltip label="四级分类名称" align="center" width="120" prop="materialClassifyFourName" />
          <el-table-column show-overflow-tooltip label="物料编码" align="center" width="120" prop="materialCode" />
          <el-table-column show-overflow-tooltip label="物料名称" align="center" width="150" prop="materialName" />
          <el-table-column show-overflow-tooltip label="生产厂商" align="center" width="150" prop="manufacturer" />
          <el-table-column show-overflow-tooltip label="所属组织" align="center" width="200" prop="orgName"/>
          <el-table-column show-overflow-tooltip label="订单员" align="center" width="100" prop="orderPersonalName" />
          <el-table-column show-overflow-tooltip label="采购员" align="center" width="100" prop="buyerName" />
          <el-table-column show-overflow-tooltip label="订单部门" align="center" width="150" prop="orderDeptName" />
          <el-table-column show-overflow-tooltip label="部门" align="center" width="150" prop="puDeptName" />
          <el-table-column show-overflow-tooltip label="创建人" align="center" width="100" prop="createByName" />
          <el-table-column show-overflow-tooltip label="创建时间" align="center" width="150" prop="createTime" />
          <el-table-column show-overflow-tooltip label="修改人" align="center" width="100" prop="updateByName" />
          <el-table-column show-overflow-tooltip label="修改时间" align="center" width="150" prop="updateTime" />
          <el-table-column
          fixed="right"
          label="操作"
          align="center"
          width="150"
          >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="check(scope.row)">查看</el-button>
            <el-button type="text" size="mini" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="deleteids(scope.row)">删除</el-button>
          </template>
        </el-table-column>
        </el-table>

        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total=total>
        </el-pagination>
      </el-card>
    </div>

    <Add v-model="isList" v-if="!isList" :pageStu="page" :disable="disable" :row="rowDetail" @refresh="getList"/>
  
    <fourClass
      ref="fourClass"
      @doSubmit="acceptFourClass"
      :single="true"
    />

    <popDialog
      ref="contractSelect"
      @doSubmit="selectionsToInput"
      :single="true"
    />

    <Refers ref="refer" @doSubmit="selectionsRefer" :single="true"/>
  </div>
</template>

<script>
import Add from './add.vue'
import CollapseTransition from '@/components/MyCollapse/collapse.vue'
import {getDivisionList, delDivision} from '@/api/purchase/basic.js'
// 物料分类
import fourClass from '@/components/PopDialog/fourClass.vue'
// 物料编码
import popDialog from '@/components/PopDialog/index.vue'
import Refers from '@/components/Refers/refers.vue'
export default {
  name: 'MaterialClassDivision',
  components: {
    Add,
    CollapseTransition,
    fourClass,
    popDialog,
    Refers
  },
  data() {
    return {
      expanded: false,
      // 页面配置
      isList: true,
      // 页面状态
      page: '',
      queryParams: {
        materialClassifyOne: '',
        materialClassifyOneName: '',
        materialClassifyTwo: '',
        materialClassifyTwoName: '',
        materialClassifyThree: '',
        materialClassifyThreeName: '',
        materialClassify: '',
        materialClassifyFourName: '',
        materialCode: '',
        materialName: '',
        orderPersonal: '',
        buyer: '',
        manufacturer: '',
        remark: '',
        pageNum: 1,
        pageSize: 10
      },
      options: [{
        value: 'Y', label: '是',
      }, {
        value: 'N', label: '否'
      }],
      staffOptions: [],
      staff2Options: [],
      referCondition: {
        type: '',
        isPage: true,
        title: ''
      },
      tableList: [],
      total: 0,
      rowDetail: {},
      disable: false,
      allSelection: []
    }
  },
  created() {
    this.getList(this.queryParams)
  },
  methods: {
    getList(params){
      getDivisionList(params).then(res => {
        if (res.code === 200) {
          this.tableList = res.rows
          this.total = res.total
        }
      })
    },
    searchList() {
      this.getList(this.queryParams)
    },
    resetList() {
      this.queryParams = {
        materialClassifyOne: '',
        materialClassifyOneName: '',
        materialClassifyTwo: '',
        materialClassifyTwoName: '',
        materialClassifyThree: '',
        materialClassifyThreeName: '',
        materialClassify: '',
        materialClassifyFourName: '',
        materialCode: '',
        materialName: '',
        orderPersonal: '',
        buyer: '',
        manufacturer: '',
        remark: '',
        pageNum: 1,
        pageSize: 10
      }
      this.getList(this.queryParams)
    },
    deletes() {
      if (this.allSelection.length == 0) {
        this.$modal.notifyWarning("请至少选择一条数据!");
      } else {
        let param = this.allSelection.map(item => item.id)
        let rows = param.join()
        this.$modal.confirm('确认删除选择数据?').then(() => {
          delDivision(rows).then(res => {
            if (res.code === 200) {
              this.$modal.notifySuccess("删除成功");
              this.getList(this.queryParams)
            }
          })
        }).catch(() => { })
      }
    },
    handleSelectionChange(selection) {
      console.log('选中', selection)
      this.allSelection = selection
    },
    // 表格选中数据
    rowSelect(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    handleCommand(command) {
      alert(command)
    },
    addDivision() {
      this.isList = false
      this.page = 'add'
      this.disable = false
    },
    check(row) {
      this.isList = false
      this.page = 'check'
      this.rowDetail = row
      this.disable = true
    },
    doubleClick(row) {
      this.isList = false
      this.page = 'check'
      this.rowDetail = row
      this.disable = true
    },
    edit(row) {
      this.isList = false
      this.page = 'edit'
      this.rowDetail = row
      this.disable = false
    },
    deleteids(row) {
      console.log('row', row)
      this.$modal.confirm('确认信息').then(() => {
        delDivision(row.id).then(res => {
          if (res.code === 200) {
            this.$modal.notifySuccess("删除成功");
            this.getList(this.queryParams)
          }
        })
        }).catch(() => {})
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryParams.pageSize = val
      this.getList(this.queryParams)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryParams.pageNum = val
      this.getList(this.queryParams)
    },
    drop() {
      this.expanded = !this.expanded
    },
    // 物料分类显示列表
    chooseFourClass() {
      this.$refs.fourClass.init()
    },
    // 选择物料分类
    acceptFourClass(selections) {
      console.log('收到的四级分类', selections)
      this.queryParams.materialClassify = selections.code
    },
    // 显示物料编码列表
    test01() {
      this.$refs.contractSelect.init()
    },
    selectionsToInput(selections) {
      this.queryParams.materialCode = selections[0].code
    },
    // 选择默认采购组织
    chooseRefer(type, isPage, title) {
      this.referCondition.type = type
      this.referCondition.isPage = isPage
      this.referCondition.title = title
      this.$refs.refer.init(this.referCondition)
    },
    selectionsRefer(selection) {
      if (this.referCondition.title == '订单员') {
        this.staffOptions = selection
        this.queryParams.orderPersonal = selection[0].code
      }
      if (this.referCondition.title == '采购员') {
        this.staff2Options = selection
        this.queryParams.buyer = selection[0].code
      }
    }
  }
}
</script>

<style lang="scss" scoped>
#MaterialClassDivision {
  padding: 12px;
  box-sizing: border-box;
  overflow-y: scroll;
}
.btn_grooup {
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-end;
}
.lines {
  margin-top: 0;
}
::v-deep .el-table__row > td {
  border-right: none;
}
 ::v-deep .el-card .el-form-item {
  margin-bottom: 3px;
}
.el-pagination {
  margin-top: 10px;
  text-align: right;
}
</style>
<style>
.exporttable {
  border: solid 1px #c0c0c0;
}
.el-table .el-table__header-wrapper th {
  font-size: 14px;
}
</style>