<template>
  <div id="deliveryAddressList">
    <div v-if="isList">
      <el-card style="position: relative;">
      <el-form class="search_area" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-form-item label="仓库属性">
              <el-select clearable v-model="queryParams.warehouseProperty" size="mini" style="width: 200px">
                <el-option v-for="dict in dict.type.sys_warehouse_attribute" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="仓库档案名称">
              <el-select clearable size="mini" v-model="queryParams.warehouseName" @focus="chooseRefer('WAREHOUSE_PARAM', true, '仓库档案名称')" style="width: 200px">
                <el-option v-for="item in houseOptions" :key="item.id" :label="item.name" :value="item.name" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="收货仓库编码">
              <el-input
              v-model="queryParams.code"
              size="mini"
              clearable
              style="width: 200px"
            />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="收货仓库名称">
              <el-input
              v-model="queryParams.name"
              size="mini"
              clearable
              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" icon="el-icon-search" @click="searchList">搜索</el-button>
              <el-button size="mini" icon="el-icon-refresh" 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-select
                v-model="queryParams.materialClassifyName"
                size="mini"
                clearable
                @focus="chooseTreeRefer('MATERIALCLASSIFY_PARAM', false, '物料分类')"
                style="width: 200px"
                >
                <el-option v-for="item in classOptions" :key="item.id" :label="item.name" :value="item.name" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="1.5">
            <el-form-item label="详细地址">
            <el-input
              v-model="queryParams.address"
              size="mini"
              placeholder=""
              clearable
              style="width: 200px"
            />
            </el-form-item>
          </el-col>

          <el-col :span="1.5">
            <el-form-item label="联系人">
            <el-select clearable size="mini" v-model="queryParams.contactsName" @focus="chooseRefer('CONTACTS_PARAM', true, '联系人')" style="width: 200px">
              <el-option v-for="item in contactsOptions" :key="item.id" :label="item.name" :value="item.name" />
            </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="1.5">
            <el-form-item label="联系人电话">
            <el-input
              v-model="queryParams.contactsPhone"
              size="mini"
              clearable
              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
              v-model="queryParams.remark"
              size="mini"
              clearable
              style="width: 200px"
            />
            </el-form-item>
          </el-col>

          <el-col :span="1.5">
            <el-form-item label="同步状态">
              <el-select v-model="queryParams.sendStatus" size="mini" style="width: 200px" clearable>
                <el-option
                  v-for="item in options2"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="1.5">
            <el-form-item label="启用">
              <el-select v-model="queryParams.status" size="mini" style="width: 200px" clearable>
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </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="addAddress">新增</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="startUse">启用</el-button>
          <el-button type="primary" size="mini" @click="stopUse">停用</el-button>
          <el-button type="primary" size="mini" @click="toNC">同步NC</el-button>
          <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="430"
          max-height="430"
          style="font-size: 12px;"
          @selection-change="handleSelectionChange"
        >
          <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="warehouseProperty" :formatter="formatterWarehouseProperty"/>
          <el-table-column show-overflow-tooltip label="仓库档案名称" align="center" width="180" prop="warehouseName"/>
          <el-table-column show-overflow-tooltip label="收货仓库编码" align="center" prop="code" width="150"/>
          <el-table-column show-overflow-tooltip label="收货仓库名称" align="center" width="180" prop="name"/>
          <el-table-column show-overflow-tooltip label="详细地址" align="center" width="200" prop="address"/>
          <el-table-column show-overflow-tooltip label="物料分类" align="center" width="120" prop="materialClassifyName" />
          <el-table-column show-overflow-tooltip label="联系人" align="center" width="100" prop="contactsName" />
          <el-table-column show-overflow-tooltip label="联系人电话" align="center" prop="contactsPhone" width="120" />
          <el-table-column show-overflow-tooltip label="NC档案ID" align="center" width="150" prop="createTime" />
          <el-table-column show-overflow-tooltip label="备注" align="center" width="120" prop="remark" />
          <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 show-overflow-tooltip label="同步状态" align="center" width="100" prop="sendStatus">
            <template slot-scope="scope">
              {{ scope.row.sendStatus == 'Y' ? '已同步' : '未同步'}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="启用" align="center" prop="status">
            <template slot-scope="scope">
              {{ scope.row.status == 'Y' ? '启用' : '停用' }}
            </template>
          </el-table-column>
          <el-table-column
          fixed="right"
          label="操作"
          align="center"
          width="200"
          >
          <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"/>

    <Refers ref="refer" @doSubmit="selectionsToInput" :single="true"/>

    <TreeRefers ref="tree" @doSubmit="selectionsToInput2" :single="true"/>
  </div>
</template>

<script>
import Add from './add.vue'
import CollapseTransition from '@/components/MyCollapse/collapse.vue'
import {getAddressList, delAddress, toNc, enable } from '@/api/purchase/deliveryAddress.js'
import Refers from '@/components/Refers/refers.vue'
import TreeRefers from '@/components/Refers/treeRefer.vue'
export default {
  name: 'deliveryAddress',
  dicts: ['sys_warehouse_attribute'],
  components: {
    Add,
    CollapseTransition,
    Refers,
    TreeRefers
  },
  data() {
    return {
      expanded: false,
      // 页面配置
      isList: true,
      // 页面状态
      page: '',
      // 搜索框参数
      queryParams: {
        code: '',
        name: '',
        warehouse: '',
        warehouseName: '',
        warehouseCode: '',
        warehouseProperty: '',
        address: '',
        contacts: '',
        contactsName: '',
        contactsPhone: '',
        materialClassify: '',
        materialClassifyName: '',
        status: '',
        sendStatus: '',
        remark: '',
        pageNum: 1,
        pageSize: 10
      },
      options: [{
        value: 'Y', label: '是',
      }, {
        value: 'N', label: '否'
      }],
      options2: [{
        value: 'Y', label: '已同步',
      }, {
        value: 'N', label: '未同步'
      }],
      referCondition: {
        type: '',
        isPage: true,
        title: ''
      },
      houseOptions: [],
      classOptions: [],
      contactsOptions: [],
      tableList: [],
      total: 0,
      rowDetail: {},
      disable: false,
      allSelection:[]
    }
  },
  created() {
    this.getList(this.queryParams)
  },
  methods: {
    // 格式化表格内容
    formatterWarehouseProperty(row) {
      switch (row.warehouseProperty) {
        case '1':
          return '电商库存'
        case '2':
          return '区域分仓公共库存'
        case '3':
          return '各项目仓库存'
        case '4':
          return '中心仓公共库存'
        case '5':
          return '事业发展部仓库'
      }
    },
    getList(params){
      getAddressList(params).then(res => {
        if (res.code === 200) {
          this.tableList = res.rows
          this.total = res.total
        }
      })
    },
    searchList() {
      this.getList(this.queryParams)
    },
    resetList() {
      this.queryParams = {
        code: '',
        name: '',
        warehouse: '',
        warehouseName: '',
        warehouseCode: '',
        warehouseProperty: '',
        address: '',
        contacts: '',
        contactsName: '',
        contactsPhone: '',
        materialClassify: '',
        materialClassifyName: '',
        status: '',
        sendStatus: '',
        remark: '',
        pageNum: 1,
        pageSize: 10
      }
      this.getList(this.queryParams)
    },
    toNC() {
      if (this.allSelection.length == 0 || this.allSelection.length > 1) {
        this.$modal.notifyWarning("同步NC只能进行单条操作！");
      } else {
        console.log('参数', this.allSelection)
        toNc(this.allSelection[0]).then(res => {
          if (res.code === 200) {
            this.$modal.notifySuccess("操作成功!");
            this.getList(this.queryParams)
          }
        })
      }
    },
    startUse() {
      if (this.allSelection.length == 0 || this.allSelection.length > 1) {
        this.$modal.notifyWarning("启用停用只能进行单条操作！");
      } else {
        console.log('参数', this.allSelection)
        let param = {id: this.allSelection[0].id, status: 'Y' }
        enable(param).then(res => {
          if (res.code === 200) {
            this.$modal.notifySuccess("操作成功!");
            this.getList(this.queryParams)
          }
        })
      }
    },
    stopUse() {
      if (this.allSelection.length == 0 || this.allSelection.length > 1) {
        this.$modal.notifyWarning("启用停用只能进行单条操作！");
      } else {
        console.log('参数', this.allSelection)
        let param = { id: this.allSelection[0].id, status: 'N' }
        enable(param).then(res => {
          if (res.code === 200) {
            this.$modal.notifySuccess("操作成功!");
            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(() => {
          delAddress(rows).then(res => {
            if (res.code === 200) {
              this.$modal.notifySuccess("删除成功");
              this.getList(this.queryParams)
            }
          })
        }).catch(() => { })
      }
    },
    handleSelectionChange(selection) {
      console.log('选中', selection)
      this.allSelection = selection
    },
    handleCommand(command) {
      alert(command)
    },
    addAddress() {
      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(() => {
        delAddress(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
    },
    chooseRefer(type, isPage, title) {
      this.referCondition.type = type
      this.referCondition.isPage = isPage
      this.referCondition.title = title
      this.$refs.refer.init(this.referCondition)
    },
    selectionsToInput(selection) {
      if (this.referCondition.type == 'WAREHOUSE_PARAM') {
        this.houseOptions = selection
        // this.queryParams.warehouse = selection[0].id
        this.queryParams.warehouseName = selection[0].name
      }
      if (this.referCondition.type == 'CONTACTS_PARAM') {
        this.contactsOptions = selection
        // this.queryParams.contacts = selection[0].code
        this.queryParams.contactsName = selection[0].name
      }
    },
    chooseTreeRefer(type, isPage, title) {
      this.referCondition.type = type
      this.referCondition.isPage = isPage
      this.referCondition.title = title
      this.$refs.tree.init(this.referCondition)
    },
    selectionsToInput2(selection) {
      this.classOptions.push(selection)
      // this.queryParams.materialClassify = selection.id
      this.queryParams.materialClassifyName = selection.name
    },
  }
}
</script>

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