<template>
  <div id="MaterialClassDivision">
    <div v-if="isList">
      <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">
              <el-button type="primary" size="mini" icon="el-icon-search" plain @click="searchList">搜索</el-button>
              <el-button size="mini" icon="el-icon-refresh" plain @click="resetList">重置</el-button>
            </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.materialClassifyFour">
                <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.material">
                  <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>

      <el-card>
        <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">删除</el-button>
        </div>
        <el-table 
          :data="tableList" 
          fit
          max-height="480"
          @selection-change="handleSelectionChange"
        >
          <el-table-column show-overflow-tooltip type="selection" width="55" />
          <el-table-column show-overflow-tooltip label="物料分类编码" align="center" prop="materialClassify"/>
          <el-table-column show-overflow-tooltip label="物料分类名称" align="center" width="200" prop="materialClassifyName" />
          <el-table-column show-overflow-tooltip label="一级分类名称" align="center" prop="materialClassifyOneName" />
          <el-table-column show-overflow-tooltip label="二级分类名称" align="center" width="150" prop="materialClassifyTwoName" />
          <el-table-column show-overflow-tooltip label="三级分类名称" align="center" width="150" prop="materialClassifyThreeName"/>
          <el-table-column show-overflow-tooltip label="四级分类名称" align="center" width="150" prop="materialClassifyFourName" />
          <el-table-column show-overflow-tooltip label="物料编码" align="center" width="150" prop="material" />
          <el-table-column show-overflow-tooltip label="物料名称" align="center" 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="120" prop="orgName"/>
          <el-table-column show-overflow-tooltip label="订单员" align="center" width="150" prop="orderPersonalName" />
          <el-table-column show-overflow-tooltip label="采购员" align="center" width="150" 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="150" prop="createdByName" />
          <el-table-column show-overflow-tooltip label="创建时间" align="center" width="150" prop="createdTime" />
          <el-table-column show-overflow-tooltip label="修改人" align="center" width="150" prop="updatedByName" />
          <el-table-column show-overflow-tooltip label="修改时间" align="center" width="150" prop="updatedTime" />
          <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
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="1"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="100"
          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: '',
        materialClassifyFour: '',
        materialClassifyFourName: '',
        material: '',
        materialName: '',
        orderPersonal: '',
        buyer: '',
        manufacturer: '',
        remark: '',
        pageNum: 1,
        pageSize: 5
      },
      options: [{
        value: 'Y', label: '是',
      }, {
        value: 'N', label: '否'
      }],
      staffOptions: [],
      staff2Options: [],
      referCondition: {
        type: '',
        isPage: true,
        title: ''
      },
      tableList: [],
      total: 0,
      rowDetail: {},
      disable: false
    }
  },
  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: '',
        materialClassifyFour: '',
        materialClassifyFourName: '',
        material: '',
        materialName: '',
        orderPersonal: '',
        buyer: '',
        manufacturer: '',
        remark: '',
        pageNum: 1,
        pageSize: 5
      }
      this.getList(this.queryParams)
    },
    handleSelectionChange() {},
    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
    },
    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.msgSuccess("删除成功");
            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.materialClassifyFour = selections.code
    },
    // 显示物料编码列表
    test01() {
      this.$refs.contractSelect.init()
    },
    selectionsToInput(selections) {
      this.queryParams.material = 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 {
  height: calc(100vh - 84px);
  padding: 12px;
  box-sizing: border-box;
  overflow-y: scroll;
}
.btn_grooup {
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-end;
}
.lines {
  margin-top: 0;
}
</style>