<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" label-width="68px">
      <el-row >
        <el-col :span="18">
          <el-form-item label="物料">
            <el-input v-model="queryParams.params.materialNameOrCode"></el-input>
          </el-form-item>
          <el-form-item label="客户">
            <el-input v-model="queryParams.params.customerNameOrCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item  style="float:right">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="btnQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="btnResetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div style="float:right">
        <el-button type="primary" size="mini" @click="btnAdd">新增</el-button>
      </div>
    <el-table
      size="mini"
      v-loading="loading"
      :data="list"
      height="500px"
    >
      <el-table-column show-overflow-tooltip min-width="200" label="序号" align="center" prop="id" />
      <el-table-column show-overflow-tooltip min-width="200" label="物料" align="center" prop="materialName" />
      <el-table-column show-overflow-tooltip min-width="200" label="物料编码" align="center" prop="materialCode" />
      <el-table-column show-overflow-tooltip min-width="200" label="客户" align="center" prop="customerName" />
      <el-table-column show-overflow-tooltip min-width="200" label="客户编码" align="center" prop="customerCode" />
      <el-table-column
        width="250"
        label="操作"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="btnUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" @click="btnDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="paginationClass">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改任务对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form
        size="mini"
        ref="form"
        :model="form"
        label-width="80px"
        :rules="formRules"
      >
        <el-form-item label="物料" prop="materialName">
          <DrPopoverSelectV2
            size="mini"
            v-model="form.materialName"
            valueKey= "name"
            referName="MATERIAL_PARAM"
            :dataMapping="{
              material: 'id',
              materialCode: 'code',
            }"
            :source.sync="form"
          >
          </DrPopoverSelectV2>
        </el-form-item>
        <el-form-item label="客户" prop="customerName">
          <DrPopoverSelectV2
            size="mini"
            v-model="form.customerName"
            valueKey= "name"
            referName="CUSTOMER_PARAM"
            :dataMapping="{
              customer: 'id',
              customerCode: 'code',
            }"
            :source.sync="form"
          >
          </DrPopoverSelectV2>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button
          size="mini"
          type="primary"
          @click="submitForm"
          >确 定</el-button
        >
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import {list,detail,insert,update,del} from '@/api/purchase/ownershipLot.js'

export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 列表数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        params:{
          materialNameOrCode: null,
          customerNameOrCode: null,
        },
      },
      // 表单参数
      form: {
      },
      //校验规则
      formRules:{
        materialName: [
          { required: true, message: "物料不能为空", trigger: "blur" },
        ],
        customerName: [
          { required: true, message: "客户不能为空", trigger: "blur" },
        ],
      },
    };
  },
  async created() {
    await this.getList();
  },
  methods: {
    /** 查询任务列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.form = {};
      this.open = false;
    },
    /** 搜索按钮操作 */
    btnQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    btnResetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        code: null,
        params:{
          materialNameOrCode: null,
          customerNameOrCode: null,
        },
      };
      this.btnQuery();
    },
    /** 修改按钮操作 */
    btnUpdate(row) {
      const id = row.id;
      detail(id).then(response => {
        this.form = {...this.form,...response.data};
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交表单 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.id){
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.form = {};
              this.getList();
            });
          }else{
            insert(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.form = {};
              this.getList();
            });
          }
        }
      });
    },
    btnAdd(){
      this.open = true;
      this.title = "新增";
    },
    btnDelete(row){
      this.$modal.confirm('是否确认删除"' + row.materialName + "+" + row.customerName + '"数据？').then(function() {
        return del(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>

<style lang="scss" scoped>
.btn_grooup {
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-end;
}
.paginationClass {
  z-index: 500;
  position: fixed;
  bottom: 10px;
  right: 10px;
  width: 100%;
  line-height: var(--footer-height);
  color: #fff;
}
</style>
