<template>
  <div id="deliveryAddressList">
    <div v-if="isList">
      <el-form class="search_area" label-width="120px">
        <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">
              <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-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" clearable @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>

      <el-card>
        <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">启用</el-button>
          <el-button type="primary" size="mini">停用</el-button>
          <el-button type="primary" size="mini" @click="toNC">同步NC</el-button>
          <el-button type="primary" size="mini">删除</el-button>
        </div>
        <el-table 
          :data="tableList" 
          fit
          max-height="480"
          style="font-size: 12px;"
          @selection-change="handleSelectionChange"
        >
          <el-table-column show-overflow-tooltip type="selection" width="55" />
          <el-table-column show-overflow-tooltip label="仓库属性" align="center" prop="warehouseProperty"/>
          <el-table-column show-overflow-tooltip label="仓库档案名称" align="center" width="200" 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="150" prop="name"/>
          <el-table-column show-overflow-tooltip label="详细地址" align="center" width="150" prop="address"/>
          <el-table-column show-overflow-tooltip label="物料分类" align="center" width="150" prop="materialClassifyName" />
          <el-table-column show-overflow-tooltip label="联系人" align="center" width="150" prop="contactsName" />
          <el-table-column show-overflow-tooltip label="联系人电话" align="center" prop="contactsPhone" width="150" />
          <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="150" 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="150" 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="150" prop="sendStatus">
            <template slot-scope="scope">
              {{ scope.row.sendStatus == 'Y' ? '已同步' : '未同步'}}
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="启用" align="center" width="150" prop="status">
            <template slot-scope="scope">
              {{ scope.row.status == 'Y' ? '启用' : '停用' }}
            </template>
          </el-table-column>
          <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, 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"/>

    <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 } 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: {
    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.msgWarning("同步NC只能进行单条操作！");
      } else {
        console.log('参数', this.allSelection)
        toNc(this.allSelection[0]).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess("操作成功!");
            this.getList(this.queryParams)
          }
        })
      }
    },
    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.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
    },
    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 {
  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;
}
.el-pagination {
  margin-top: 10px;
  text-align: right;
}
</style>