<template>
  <div :class="listConfig.containerClass">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <search-form-item
        v-for="item of searchColumns"
        :key="item.key"
        :query-params="queryParams"
        :label="item.label"
        :prop="item.key"
        :type="item.type"
        :dict-type="item.dictType"
        :dict="dict"
        @query="handleQuery"
      />
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <template v-if="listConfig.showTableOpera">
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          plain-->
        <!--          icon="el-icon-plus"-->
        <!--          size="mini"-->
        <!--          @click="handleAdd"-->
        <!--          v-has-permi="['yh:reportProd:add']"-->
        <!--        >新增</el-button>-->
        <!--      </el-col>-->
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="danger"-->
        <!--          plain-->
        <!--          icon="el-icon-delete"-->
        <!--          size="mini"-->
        <!--          :disabled="multiple"-->
        <!--          @click="handleDelete"-->
        <!--          v-has-permi="['yh:reportProd:remove']"-->
        <!--        >删除</el-button>-->
        <!--      </el-col>-->
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-has-permi="['yh:reportProd:export']"
          >导出</el-button>
        </el-col>
      </template>
      <right-toolbar
        :show-refresh="listConfig.showRefresh"
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      />
    </el-row>

    <el-table
      v-loading="loading"
      :data="reportProdList"
      @selection-change="handleSelectionChange"
      :default-sort="defaultSort"
      @sort-change="onSortChange"
      @expand-change="onExpandChange"
    >
      <el-table-column type="selection" width="55" align="center" v-if="listConfig.showSelection"/>
      <el-table-column type="expand" width="55" align="left">
        <template slot-scope="d">
          <div class="expand-container">
            <div class="edit-title" v-has-permi="['yh:reportUserProd:list']">员工产量</div>
<!--            <report-user-prod-->
<!--              v-has-permi="['yh:reportUserProd:list']"-->
<!--              :query="{prodId: d.row.id, pageSize: 99999}"-->
<!--              :hide-columns="['reportDate', 'reportStatus', 'priceName', 'pricePrice']"-->
<!--              :custom-config="expandCustomConfig"-->
<!--            />-->
            <report-user-prod-descriptions :data="d.row.userProdList"/>

          </div>
        </template>
      </el-table-column>
      <template v-for="column of showColumns">
        <el-table-column
            :key="column.key"
            :label="column.label"
            :prop="column.key"
            :align="column.align"
            :min-width="column.minWidth"
            :sort-orders="orderSorts"
            :sortable="column.sortable"
            :show-overflow-tooltip="column.overflow"
            :width="column.width"
        >
          <template slot-scope="d">
            <template v-if="column.key === 'id'">
              {{d.row[column.key]}}
            </template>
            <template v-else-if="column.key === 'priceType'">
              <dict-tag :value="d.row.priceType" :options="dict.type.price_type"/>
            </template>
            <template v-else-if="column.key === 'pricePrice'">
              {{d.row.pricePrice | dv}} 元
<!--              <template v-if="d.row.priceUnit">/ {{d.row.priceUnit}}</template>-->
            </template>
            <template v-else-if="column.key === 'totalAmount'">
              {{calcMulDecimal(d.row.pricePrice, d.row.num)}} 元
            </template>
            <template v-else-if="['num','defectNum'].includes(column.key)">
              {{d.row[column.key] | dv}}
<!--              <template v-if="d.row.priceUnit">{{d.row.priceUnit}}</template>-->
            </template>
            <template v-else>
              {{d.row[column.key] | dv}}
            </template>
          </template>
        </el-table-column>
      </template>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-has-permi="['yh:reportProd:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-has-permi="['yh:reportProd:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报表产量对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <form-col :span="span"  label="报表ID" prop="reportId">
            <el-input v-model="form.reportId" placeholder="请输入报表ID" />
          </form-col>
          <form-col :span="span"  label="工序ID" prop="priceId">
            <el-input v-model="form.priceId" placeholder="请输入工序ID" />
          </form-col>
          <form-col :span="span"  label="良品数" prop="num">
            <el-input v-model="form.num" placeholder="请输入良品数" />
          </form-col>
          <form-col :span="span"  label="不良品数" prop="defectNum">
            <el-input v-model="form.defectNum" placeholder="请输入不良品数" />
          </form-col>
          <form-col :span="span"  label="工序类别" prop="priceCategory">
            <el-input v-model="form.priceCategory" placeholder="请输入工序类别" />
          </form-col>
          <form-col :span="span"  label="工序大小" prop="priceSize">
            <el-input v-model="form.priceSize" placeholder="请输入工序大小" />
          </form-col>
          <form-col :span="span"  label="工序名称" prop="priceName">
            <el-input v-model="form.priceName" placeholder="请输入工序名称" />
          </form-col>
          <form-col :span="span"  label="子工序名称" prop="priceSubName">
            <el-input v-model="form.priceSubName" placeholder="请输入子工序名称" />
          </form-col>
          <form-col :span="span"  label="工序图案" prop="pricePattern">
            <el-input v-model="form.pricePattern" placeholder="请输入工序图案" />
          </form-col>
          <form-col :span="span"  label="工序规格" prop="priceSpec">
            <el-input v-model="form.priceSpec" placeholder="请输入工序规格" />
          </form-col>
          <form-col :span="span"  label="工序单价(元)" prop="pricePrice">
            <el-input v-model="form.pricePrice" placeholder="请输入工序单价(元)" />
          </form-col>
          <form-col :span="span"  label="工序单位" prop="priceUnit">
            <el-input v-model="form.priceUnit" placeholder="请输入工序单位" />
          </form-col>
          <form-col :span="span"  label="工序分类" prop="priceClassify">
            <el-input v-model="form.priceClassify" placeholder="请输入工序分类" />
          </form-col>
          <form-col :span="span"  label="工序生产数量倍数(个)" prop="priceQuantity">
            <el-input v-model="form.priceQuantity" placeholder="请输入工序生产数量倍数(个)" />
          </form-col>
          <form-col :span="span"  label="删除标志" prop="deleted">
            <el-input v-model="form.deleted" placeholder="请输入删除标志" />
          </form-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReportProd, getReportProd, delReportProd, addReportProd, updateReportProd } from "@/api/yh/reportProd";
import {$listConfig, $showColumns, $showSearch} from '@/utils/mixins';
import FormCol from "@/components/FormCol/index.vue";
import SearchFormItem from "@/components/SearchFormItem/index.vue";
import ReportUserProd from "@/views/yh/reportUserProd/index.vue";
import {calcMulDecimal} from "../../../utils/money";
import ReportUserProdDescriptions from "@/views/yh/reportProd/components/ReportUserProdDescriptions.vue";

// 默认排序字段
const defaultSort = {
  prop: "id",
  order: "descending"
}

export default {
  name: "ReportProd",
  mixins: [$showColumns, $listConfig, $showSearch],
  dicts: ['price_type'],
  components: {ReportUserProdDescriptions, ReportUserProd, SearchFormItem, FormCol},
  props: {
    query: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      expandCustomConfig: {
        containerClass: null,
        showSearch: false,
        showRowOpera: false,
        showTableOpera: false,
        showTools: false,
        showPage: false,
      },
      searchColumns: [
        {key: 'reportId', label: "报表编号", type: 'input', dictType: null},
        {key: 'priceType', label: "类型", type: 'dict', dictType: 'price_type'},
        {key: 'priceName', label: "工序", type: 'input', dictType: null},
        {key: 'priceSubName', label: "子工序", type: 'input', dictType: null},
        {key: 'priceCategory', label: "类别", type: 'input', dictType: null},
        {key: 'priceSize', label: "大小", type: 'input', dictType: null},
        {key: 'pricePattern', label: "图案", type: 'input', dictType: null},
        {key: 'priceSpec', label: "规格", type: 'input', dictType: null},
        {key: 'priceClassify', label: "分类", type: 'input', dictType: null},
        {key: 'priceUnit', label: "单位", type: 'input', dictType: null},
      ],
      span: 24,
      // 字段列表
      columns: [
        {key: 'id', visible: true, label: '编号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'reportId', visible: true, label: '报表编号', minWidth: null, sortable: true, overflow: false, align: 'center', width: "100"},
        {key: 'priceType', visible: true, label: '类型', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'priceName', visible: true, label: '工序', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'priceSubName', visible: true, label: '子工序', minWidth: null, sortable: true, overflow: false, align: 'center', width: "100"},
        {key: 'priceCategory', visible: true, label: '类别', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'priceSize', visible: true, label: '大小', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'pricePattern', visible: true, label: '图案', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'priceSpec', visible: true, label: '规格', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'priceClassify', visible: true, label: '分类', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'priceQuantity', visible: true, label: '倍数', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'num', visible: true, label: '良品', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'defectNum', visible: true, label: '不良品', minWidth: null, sortable: true, overflow: false, align: 'center', width: "100"},
        {key: 'pricePrice', visible: true, label: '单价', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'priceUnit', visible: true, label: '单位', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'totalAmount', visible: true, label: '总价', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报表产量表格数据
      reportProdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        id: null,
        reportId: null,
        priceId: null,
        num: null,
        defectNum: null,
        priceCategory: null,
        priceSize: null,
        priceName: null,
        priceSubName: null,
        pricePattern: null,
        priceSpec: null,
        pricePrice: null,
        priceUnit: null,
        priceClassify: null,
        priceQuantity: null,
        deleted: null,
        priceType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        reportId: [
          { required: true, message: "报表ID不能为空", trigger: "blur" }
        ],
        priceId: [
          { required: true, message: "工序ID不能为空", trigger: "blur" }
        ],
        num: [
          { required: true, message: "良品数不能为空", trigger: "blur" }
        ],
        defectNum: [
          { required: true, message: "不良品数不能为空", trigger: "blur" }
        ],
        priceName: [
          { required: true, message: "工序名称不能为空", trigger: "blur" }
        ],
        pricePrice: [
          { required: true, message: "工序单价(元)不能为空", trigger: "blur" }
        ],
        deleted: [
          { required: true, message: "删除标志不能为空", trigger: "blur" }
        ],
        priceType: [
          { required: true, message: "工序类型不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.queryParams = {
      ...this.queryParams,
      ...this.query,
    }
    this.showSearch = this.listConfig.showSearch;
    this.initSearchColumns();
    this.initColumns();
    this.getList();
  },
  methods: {
    calcMulDecimal,
    // 展开行
    onExpandChange(row, expandedRows) {

    },
    /** 当排序按钮被点击时触发 **/
    onSortChange(column) {
      if (column.order == null) {
        this.queryParams.orderByColumn = defaultSort.prop;
        this.queryParams.isAsc = defaultSort.order;
      } else {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
      }
      this.getList();
    },
    /** 查询报表产量列表 */
    getList() {
      this.loading = true;
      listReportProd(this.queryParams).then(response => {
        this.reportProdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        reportId: null,
        priceId: null,
        num: null,
        defectNum: null,
        priceCategory: null,
        priceSize: null,
        priceName: null,
        priceSubName: null,
        pricePattern: null,
        priceSpec: null,
        pricePrice: null,
        priceUnit: null,
        priceClassify: null,
        priceQuantity: null,
        deleted: null,
        priceType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报表产量";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReportProd(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报表产量";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReportProd(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReportProd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除报表产量编号为"' + ids + '"的数据项？').then(function() {
        return delReportProd(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yh/reportProd/export', {
        ...this.queryParams
      }, `reportProd_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
