<template>
  <div class="page">
    <topBar v-if="topList.length > 0"
            :topList="topList"
            :moren="moren"></topBar>
    <div class="thetopbox">
      <el-form label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'仓库编号'">
              <el-input clearable
                        v-model="queryParams.code"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'仓库名称'">
              <el-input clearable
                        v-model="queryParams.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'联系人'">
              <el-input clearable
                        v-model="queryParams.contacts"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'联系方式'">
              <el-input clearable
                        v-model="queryParams.mobilePhone"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'创建日期'">
              <el-date-picker clearable
                              v-model="queryParams.creatTime"
                              @change="checkTime"
                              type="daterange"
                              range-separator="——"
                              :start-placeholder="'开始日期'"
                              :end-placeholder="'结束日期'"
                              value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item class="dizhi"
                          :label="'详细地址'"
                          prop="">
              <el-cascader style="width: 100%"
                           clearable
                           v-model="queryParams.shoppingAddress"
                           :options="areaData"
                           :props="regionParams"
                           @change="handleChange1"></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label=""
                          label-width="40px">
              <el-input clearable
                        v-model="queryParams.wareAddress"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div class="searchbox">
              <el-button class="my_search"
                         @click="getSearch">
                {{ '搜索' }}</el-button>
              <el-button class="my_reset"
                         @click="reChongzhi">
                {{ '重置' }}</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button size="mini"
                     class="thebtn1"
                     @click="addData"
                     v-has-buttons="['warehouseConfigAdd']">{{'添加'}}</el-button>
          <el-button size="mini"
                     class="thebtn2"
                     @click="handleExport"
                     v-has-buttons="['warehouseConfigExport']">
            {{ '导出' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table :data="tableData"
        v-loading="loading"
                  height="680px"
                  style="width: 100%"
                  :header-cell-style="{ background: '#EEEEEE' }"
                  @selection-change="handleSelectionChange">
          <el-table-column type="selection"
                           width="55"> </el-table-column>
          <el-table-column align="center"
                           prop="code"
                           :label="'仓库编号'"
                           width="180">
          </el-table-column>
          <el-table-column align="center"
                           prop="name"
                           :label="'仓库名称'">
          </el-table-column>
          <el-table-column align="center"
                           width="150px"
                           prop=""
                           :label="'绑定地区'">
            <template slot-scope="scope">
              <el-popover placement="top-start"
                          title=""
                          width="200"
                          trigger="hover">
                <div>{{ scope.row.samstrAll }}</div>
                <div slot="reference">{{ scope.row.samstr }}</div>
              </el-popover></template>
          </el-table-column>
          <el-table-column align="center"
                           prop="contacts"
                           :label="'联系人'">
          </el-table-column>
          <el-table-column align="center"
                           prop="mobilePhone"
                           :label="'联系方式'">
          </el-table-column>
          <el-table-column min-width="120px"
                           align="center"
                           prop="provinceVal"
                           :label="'省'">
          </el-table-column>
          <el-table-column align="center"
                           prop="cityVal"
                           :label="'市'">
          </el-table-column>
          <el-table-column align="center"
                           prop="countyVal"
                           :label="'区/县'">
          </el-table-column>
          <el-table-column align="center"
                           min-width="150px"
                           prop="wareAddress"
                           :label="'详细地址'">
          </el-table-column>

          <el-table-column align="center"
                           min-width="120px"
                           prop="creationTime"
                           :label="'创建日期'">
          </el-table-column>
          <el-table-column align="center"
                           prop="time"
                           :label="'操作'"
                           min-width="120px">
            <template slot-scope="scope">
              <el-button @click.native.prevent="update(scope.row)"
                         type="text"
                         size="small"
                         class="button-s"
                         style="color: #009b58"
                         v-has-buttons="['warehouseConfigUpdate']">
                {{ '修改' }}
              </el-button>
              <el-button class="button-s"
                         @click.native.prevent="deleteRow(scope.row)"
                         type="text"
                         size="small"
                         v-has-buttons="['warehouseConfigDelete']"
                         style="color: #e41b1b">
                {{ '删除' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getSearch" />
    <el-dialog :title="dialogTitle"
               :close-on-click-modal="false"
               :visible.sync="dialogVisible"
               width="40%"
               center
               :before-close="handleClose">
      <el-form ref="form"
               :rules="rules"
               :model="form"
               label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'仓库编号'"
                          prop="code">
              <el-input clearable
                        disabled
                        v-model="form.code"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司"
                          prop="pkCorp">
              <el-select clearable
                         v-model="form.pkCorp"
                         :placeholder="'请选择'">
                <el-option v-for="(item, index) in corpList"
                           :key="index"
                           :label="item.corpName"
                           :value="item.pkId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item :label="'仓库名称'"
                          prop="name">
              <el-input clearable
                        v-model="form.name"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item :label="'联系人'"
                          prop="contacts">
              <el-input clearable
                        v-model="form.contacts"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'联系方式'"
                          prop="mobilePhone">
              <el-input clearable
                        v-model="form.mobilePhone"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item :label="'详细地址'"
                          class="dizhi"
                          prop="shoppingAddress">
              <el-cascader v-model="form.shoppingAddress"
                           :options="areaData"
                           :props="regionParams"
                           @change="handleChange2"></el-cascader>
              <el-input clearable
                        type="textarea"
                        v-model="form.wareAddress"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="'绑定地区'"
                          >
              <el-checkbox-group v-model="form.provinceList">
                <el-checkbox v-for="item in areaData2"
                             :key="item.id"
                             :label="item.id">{{ item.name }}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="handleClose">{{'取消'}}</el-button>
        <el-button type="primary"
                   @click="onSubmit('form')">{{'确认'}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from '@/components/topBar'
import * as api from '@/api/site.js'
import * as sys from '@/api/system'
import * as supply from '@/api/supply.js'

export default {
  name: 'WarehouseConfig',
  components: {
    topBar,
  },
  data() {
    return {
      moren: 'warehouseConfig',
      topList: [
        {
          name: '仓库配置',
          path: 'warehouseConfig',
        },
      ],
      tableData: [],
      loading:false,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        shoppingAddress: ['', '', ''],
        creatTime: [],
      },
      total: 0,
      multipleSelection: [],
      areaData: [],
      regionParams: {
        label: 'name',
        value: 'id',
        children: 'children',
        expandTrigger: 'hover',
      },
      dialogVisible: false,
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        code: [{ required: true, message: '请输入', trigger: 'blur' }],
        contacts: [
          { required: true, message: '请输入', trigger: 'blur' },
        ],
        mobilePhone: [
          { required: true, message: '请输入', trigger: 'blur' },
        ],
        // provinceList: [
        //   { required: true, message: '请选择省', trigger: 'change' },
        // ],

        shoppingAddress: [
          { required: true, message: '请选择地址', trigger: 'change' },
        ],
        pkCorp: [
          { required: true, message: '请输入', trigger: 'blur' },
        ],
      },
      form: {
        code: '',
        name: '',
        province: '',
        city: '',
        county: '',
        wareAddress: '',
        memberCode: '',
        shopowner: '',
        provinceList: [],
        shoppingAddress: ['', '', ''],
      },
      ifadd: '', //是否添加 添加1 修改2
      dialogTitle: '',
      areaData2: [],
      corpList: [],
    }
  },
  created() {
    this.getSearch()
    this.getData()
  },
  methods: {
    //列表展示考核
    update(row) {
      this.dialogTitle = '修改'
      sys.getBdStorehouse(row.pkId).then((res) => {
        if (!res.data.provinceList) {
          this.$set(this.form, 'provinceList', [])
        }
        this.form = res.data

        let arr = [
          res.data.province,
          res.data.city + '',
          (res.data.county || '') + '',
        ]
        arr = arr.filter((item) => item != '')
        this.$set(this.form, 'shoppingAddress', arr)
        this.$forceUpdate()
        this.ifadd = 2
        this.dialogVisible = true
      })
    },
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then((_) => {
        this.download(
          '/system/manage/storehouse/export',
          { ...this.queryParams },
          `仓库配置${new Date().getTime()}.xlsx`
        )
      })
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = ''
          if (this.ifadd == 1) {
            url = sys.saveStoreHouse
          } else if (this.ifadd == 2) {
            url = sys.updateStoreHouse
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: 'success',
              })
              this.dialogVisible = false
              this.getSearch()
            }
          })
        }
      })
    },

    handleClose() {
      this.dialogVisible = false
      this.$refs['form'].clearValidate()
      this.$refs['form'].resetFields()
    },
    checkTime(val) {
      this.queryParams.startDate = val ? val[0] : ''
      this.queryParams.endDate = val ? val[1] : ''
    },
    handleChange1(val) {
      this.queryParams.province = val[0]
      this.queryParams.city = val[1]
      this.queryParams.county = val[2] || ''
    },
    handleChange2(val) {
      this.form.province = val[0]
      this.form.city = val[1]
      this.form.county = val[2] || ''
    },
    getData() {
      api.areaList().then((res) => {
        this.areaData = res.data
        this.areaData2 = res.data
        this.areaData2.forEach((ele, index) => {
          this.areaData2[index].id = Number(ele.id)
        })
        console.log('🌈areaData2', this.areaData2)
      })
      supply.corpList().then((res) => {
        this.corpList = res.rows
      })
    },
    addData() {
      this.ifadd = 1
      this.dialogTitle = '添加'
      this.form = {
        shoppingAddress: ['', '', ''],
        provinceList: [],
      }
      sys.getStoreHouseCode().then((res) => {
        if (res.code == 200) {
          this.form.code = res.msg
          this.dialogVisible = true
        }
      })
    },

    //删除
    deleteRow(row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        sys.delBdStorehouse(row.pkId).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: 'success',
            })
          }
          this.getSearch()
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        shoppingAddress: ['', '', ''],
      }
      this.getSearch()
    },
    getSearch() {
      this.loading = true
      sys.getStoreHouseList(this.queryParams).then((res) => {
        this.loading = false
        res.rows.forEach((res) => {
          res.samstrAll = res.samList.map((obj) => obj.provinceVal).join(',')
          if (res.samstrAll.length > 8) {
            res.samstr = res.samstrAll.substring(0, 8) + '...'
          } else {
            res.samstr = res.samstrAll
          }
        })
        this.tableData = res.rows
        this.total = res.total
      })
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-range-editor.el-input__inner {
  width: 100%;
}

::v-deep .el-table thead {
  color: #000000;
}

::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 5px 20px 0px 0;
    background: #ffffff;
    border-radius: 8px;
    .searchbox {
      display: flex;
      align-items: center;
      margin-left: 40px;
    }
    .searchtitle {
      margin-right: 10px;
    }
    .searchbtn {
      background: #08143f;
      color: #ffffff;
    }
  }
  .main {
    //margin-top: 20px;
    background: #f9f9f9; //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          color: #ffffff;
          background: #48b2fd;
        }
        .thebtn2 {
          color: #ffffff;
          background: #ffad41;
        }
      }
    }
  }
}
</style>
