<template>
  <basic-container>
    <div class="avue-crud">
      <el-row :hidden="!search" style="padding:6px 18px;margin-bottom: 5%">
        <!-- 列表模块 -->
        <el-table ref="table" v-loading="loading"
                  :data="data"
                  height="20%"
                  style="width: 100%"
                  :border="option.border">
<!--          <el-table-column type="selection" v-if="option.selection" width="55" align="center"></el-table-column>-->
<!--          <el-table-column type="expand" v-if="option.expand" align="center"></el-table-column>-->
<!--          <el-table-column v-if="option.index" label="序号" type="index" width="60" align="center">-->
<!--          </el-table-column>-->
          <template v-for="(item,index) in option.columnBasic">
            <!-- table字段 -->
            <el-table-column v-if="item.hide!==true"
                             :prop="item.prop"
                             :label="item.label"
                             :width="item.width"
                             :key="index">
            </el-table-column>
          </template>
        </el-table>
      </el-row>
      <el-row style="margin-bottom: 5%">
        <!-- 列表模块 -->
        <el-table ref="table" v-loading="loading"
                  @selection-change="selectionChange"
                  :data="data"
                  height="20%"
                  style="width: 100%"
                  :border="option.border">
<!--          <el-table-column type="selection" v-if="option.selection" width="55" align="center"></el-table-column>-->
<!--          <el-table-column type="expand" v-if="option.expand" align="center"></el-table-column>-->
<!--          <el-table-column v-if="option.index" label="序号" type="index" width="60" align="center">-->
<!--          </el-table-column>-->
          <template v-for="(item,index) in option.columnTakeOver">
            <!-- table字段 -->
            <el-table-column v-if="item.hide!==true"
                             :prop="item.prop"
                             :label="item.label"
                             :width="item.width"
                             :key="index">
            </el-table-column>
          </template>
          <!-- 操作栏模块 -->
          <el-table-column prop="menu" label="操作" :width="220" align="center">
            <template #="{row}">
<!--              <el-button type="primary" text icon="el-icon-view" @click="handleView(row)">查看</el-button>-->
              <el-button type="primary" text icon="el-icon-edit" @click="handleCallDelivery('3',row)">编辑</el-button>
              <!--              <el-button type="primary" text icon="el-icon-delete" @click="rowDel(row)">删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>


      </el-row>
      <el-row>
        <!-- 列表模块 -->
        <el-table ref="table" v-loading="loading"
                  @selection-change="selectionChange"
                  :data="dataPare"
                  :height="height"
                  style="width: 100%"
                  :border="option.border">
          <el-table-column type="selection" v-if="option.selection" width="55" align="center"></el-table-column>
          <el-table-column type="expand" v-if="option.expand" align="center"></el-table-column>
          <el-table-column v-if="option.index" label="序号" type="index" width="60" align="center">
          </el-table-column>
          <template v-for="(item,index) in option.columnParcels">
            <!-- table字段 -->
            <el-table-column v-if="item.hide!==true"
                             :prop="item.prop"
                             :label="item.label"
                             :width="item.width"
                             :key="index">
            </el-table-column>
          </template>
          <!-- 操作栏模块 -->
          <el-table-column prop="menu" label="操作" :width="220" align="center">
            <template #="{row}">
<!--              <el-button type="primary" text icon="el-icon-view" @click="handleView(row)">查看</el-button>-->
<!--              <el-button type="primary" text icon="el-icon-edit" @click="handleEdit(row)">编辑</el-button>-->
<!--              <el-button type="primary" text icon="el-icon-delete" @click="rowDel(row)">删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <div class="avue-crud__pagination" style="width:100%">
          <!-- 分页模块 -->
          <el-pagination align="right"
                         background
                         @size-change="sizeChange"
                         @current-change="currentChange"
                         :current-page="page.currentPage"
                         :page-sizes="[10, 20, 30, 40, 50, 100]"
                         :page-size="page.pageSize"
                         layout="total, sizes, prev, pager, next, jumper"
                         :total="page.total">
          </el-pagination>
        </div>
      </el-row>
      <el-dialog v-model="dialogFormCustomer" title="修改客户信息">
        <el-form :model="formCustomer">
          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input v-model="formCustomer.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth">
            <el-input v-model="formCustomer.phone" autocomplete="off" />
          </el-form-item>
          <el-form-item label="地址" :label-width="formLabelWidth">
            <el-input
              v-model="formCustomer.address"
              :rows="2"
              type="textarea"
              placeholder="请填写地址"
            />
          </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormCustomer = false">取消</el-button>
        <!--        <el-button type="primary" @click="dialogFormVisible = false"> 确定 </el-button>-->
        <el-button type="primary" @click="callFordelivery('3')"> 确定 </el-button>
      </span>
        </template>
      </el-dialog>
    </div>
  </basic-container>
</template>

<script>
import { getList, getDetail, add, update, remove,addStock } from "@/api/distribution/distributionStockArticle";
import { getListOwn } from "@/api/distribution/distributionParcelList";
import { addCallDelivery } from "@/api/distribution/distrilbutionCallDelivery";
import { addClient } from "@/api/distribution/distributionClientMessage";
import { addService } from "@/api/distribution/distributionServiceLog";
import option from "@/option/distribution/distributionStockArticle";
import { mapGetters } from "vuex";
import { getDictionaryBiz } from '@/api/system/dict';

export default {
  data () {
    return {
      height: 0,
      // 弹框标题
      title: '',
      // 是否展示弹框
      box: false,
      // 是否显示查询
      search: true,
      // 加载中
      loading: true,
      // 是否为查看模式
      view: false,
      // 查询信息
      query: {},
      // 分页信息
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 40
      },
      // 表单数据
      form: {},
      formCall: {},// 通知
      formService: {}, // 服务
      formCustomer: {}, // 客户
      // 选择行
      selectionList: [],
      // 表单配置
      option: option,
      // 表单列表
      data: [],
      dataPare: [],
      distributionType: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      dialogFormCustomer: false,
      dialogFormService: false,
      formLabelWidth: '120px',
      handler:true, //首次
      trade:false, // 商
      municipal:true, // 市
      taker:true, // 自提
    }
  },
  mounted () {
    this.init();
    this.onLoad(this.page);
  },
  watch:{
    '$route.query.id':{
      handler(newVal, oldVal) {
        // console.log(newVal, oldVal);
        this.onLoad(this.page);
        this.onLoadOwn(this.page);
      },
      deep: true,
      immediate: true
    }
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.validData(this.permission.basicdataBrandCategory_add, false),
        viewBtn: this.validData(this.permission.basicdataBrandCategory_view, false),
        delBtn: this.validData(this.permission.basicdataBrandCategory_delete, false),
        editBtn: this.validData(this.permission.basicdataBrandCategory_edit, false)
      };
    },
    ids () {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    handleMoke(){
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$router.push({
        path: '/distribution/reservation/reservationFrom',
        query:{
          id: this.ids,
          name: '添加预约单'


        }
      });
    },
    handleStockList(){
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$router.push({
        path: '/distribution/inventory/distributionStockArticleFrom',
        query: {
          id : this.ids,
          name : "转库存单"
        }
      });


      //
      // this.$confirm("确定将选择的订单转为库存品吗？", {
      //   confirmButtonText: "确定",
      //   cancelButtonText: "取消",
      //   type: "warning"
      // })
      //   .then(() => {
      //     return addStock(this.ids);
      //   })
      //   .then(() => {
      //     this.selectionClear();
      //     this.onLoad(this.page);
      //     this.$message({
      //       type: "success",
      //       message: "操作成功!"
      //     });
      //   });
    },
    //确定通知提货按钮
    callFordelivery(inde){
      switch (inde) {
        case '1':
          console.log(this.formCall);
          this.formCall.waybillNumber = this.ids;
          addCallDelivery(this.formCall).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.dialogFormVisible = false;
            this.formCall={}
          });
          break;
        case '2':
          // console.log("服务类型",this.formService);
          addService(this.formService).then(() =>{
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.dialogFormService = false;
            this.formService={}
          })
          break;
        case '3':
          // console.log("客户信息！！！",this.formCustomer);
          this.formCustomer.stockArticleId = this.$route.query.id;
          console.log("客户信息！！！",this.formCustomer);
          addClient(this.formCustomer).then(() =>{
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.dialogFormCustomer = false;
            this.formCustomer={}
            this.onLoad(this.page);
          })
          break;
      }
    },
    //提货跳转
    handlebill(row){
      console.log(">>>>",this.ids);
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$router.push({
        path: '/distribution/inventory/distrilbutionBillLading',
        query:{
          id: this.ids,


        }
      });
    },
    //通知提货
    handleCallDelivery(ind,row){
      switch (ind) {
          case '1':
            let ids = [];
            this.selectionList.forEach(ele => {
              ids.push(ele.orderSelfNumbering);
            });
            this.formCall.waybill = ids;
            this.dialogFormVisible = true;
            break;
            case '2':

              this.dialogFormService = true;
            break;
            case '3':
              this.dialogFormCustomer = true;
              console.log("<><>",row);
              this.formCustomer.name = row.customerName;
              this.formCustomer.phone = row.customerTelephone;
              this.formCustomer.address = row.customerAddress;
            break;
      }

    },
    init () {
      this.height = this.setPx(document.body.clientHeight - 580);
      getDictionaryBiz('distribution_type').then(res => {
        this.distributionType = res.data.data;
      });

    },
    searchHide () {
      this.search = !this.search;
    },
    searchChange () {
      this.onLoad(this.page);
    },
    searchReset () {
      this.query = {};
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },
    handleSubmit () {
      if (!this.form.id) {
        add(this.form).then(() => {
          this.box = false;
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
      } else {
        update(this.form).then(() => {
          this.box = false;
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        })
      }
    },

    //全部
    handleEntire(){
      this.handler = true;
      this.taker = true;
      this.trade = false;

    },
    //商配
    handleTrade(){
      // this.handler = true;
      this.trade = true;
    },
    //商配
    handleMunicipal(){
      // this.handler = true;
      // this.taker = true;
    },
    //自提
    handlePickUpStore(){
      this.loading = true;
      // console.log("this.query",this.query);
      let params = {};
      this.query.typeService = '1';
      getList(this.page.currentPage, this.page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
        this.handler = false;
        this.taker = false;
      });
    }, //自提的通知
    handleInform(index){

      this.loading = true;
      // console.log("this.query",this.query);
      let params = {};
      this.query.typeService = '1';
      switch (index) {
          case '1':
            this.query.notification = index;
            break;
          case '2':
            this.query.notification = index;
            break;
      }
      this.query.typeService = '1';
      getList(this.page.currentPage, this.page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
        this.handler = false;
        this.taker = false;
      });
    },
    handleAdd () {
      this.title = '新增'
      this.form = {}
      this.box = true
    },
    handleEdit (row) {
      this.title = '编辑'
      this.box = true
      getDetail(row.id).then(res => {
        this.form = res.data.data;
      });
    },
    handleView (row) {
      this.title = '查看'
      this.view = true;
      this.box = true;
      getDetail(row.id).then(res => {
        this.form = res.data.data;
      });
    },
    handleDelete () {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.selectionClear();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    rowDel (row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    beforeClose (done) {
      done()
      this.form = {};
      this.view = false;
    },
    selectionChange (list) {
      // console.log("添加阿斯顿发生",list);
      this.selectionList = list;
    },
    selectionClear () {
      this.selectionList = [];
      // this.$refs.table.clearSelection();
    },
    currentChange (currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },
    sizeChange (pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },
    onLoad (page, params = {}) {
      this.loading = true;
      this.query.id = this.$route.query.id;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    } ,
    onLoadOwn (page, params = {}) {
      this.loading = true;
      getListOwn(page.currentPage, page.pageSize, {stockArticleId : this.$route.query.id}).then(res => {
        console.log(">>>><>><",res.data.data);
        const data = res.data.data;
        this.page.total = data.total;
        this.dataPare = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>
<style scoped>
.el-button--text {
  margin-right: 15px;
}
.el-select {
  width: 300px;
}
.el-input {
  width: 300px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
