<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"  @submit.native.prevent>
      <!--      <el-form-item label="删除人" prop="deleteBy">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.deleteBy"-->
      <!--          placeholder="请输入删除人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="删除时间" prop="deleteTime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.deleteTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择删除时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <el-form-item label="物资名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物资名称"
          clearable
          @keyup.enter.native="handleQuery"
          maxlength="50"
        />
      </el-form-item>
      <!--      <el-form-item label="品牌型号主要参数" prop="materialParams">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.materialParams"-->
      <!--          placeholder="请输入品牌型号主要参数"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="数量" prop="materialNumber">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.materialNumber"-->
      <!--          placeholder="请输入数量"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="主要功能和用途" prop="materialPurpose">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.materialPurpose"-->
      <!--          placeholder="请输入主要功能和用途"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="生产日期" prop="prrodTime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.prrodTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择生产日期">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->

      <!--      <el-form-item label="负责人或联络人" prop="charger">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.charger"-->
      <!--          placeholder="请输入负责人或联络人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="联系电话" prop="connPhone">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.connPhone"-->
      <!--          placeholder="请输入联系电话"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="备注" prop="materialRemark">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.materialRemark"-->
      <!--          placeholder="请输入备注"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="归属仓库" prop="warehouseId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.warehouseId"-->
      <!--          placeholder="请输入归属仓库"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="归属仓库" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入归属仓库"
          clearable
          @keyup.enter.native="handleQuery"
          maxlength="50"
        />
      </el-form-item>
      <el-form-item label="存放地点" prop="depositAddress">
        <el-input
          v-model="queryParams.depositAddress"
          placeholder="请输入存放地点"
          clearable
          @keyup.enter.native="handleQuery"
          maxlength="50"
        />
      </el-form-item>
      <!--      <el-form-item label="${comment}" prop="owndeptid">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.owndeptid"-->
      <!--          placeholder="请输入${comment}"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="数据所属部门(政或党的id)" prop="dataDeptId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.dataDeptId"-->
      <!--          placeholder="请输入数据所属部门(政或党的id)"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item prop="deptId">
        <template slot="label">
          <span @click="changeQueryType" v-if="queryParams.deptName == 0">本级及下级</span>
          <span @click="changeQueryType" v-if="queryParams.deptName == 1">只查询本级</span>
        </template>
        <treeselect v-model="queryParams.deptId" :options="deptOptions" multiple:false :show-count="true"
                    placeholder="请选择部门" @select="hx" :noResultsText="'空'" :noOptionsText="'空'" style="width: 240px"/>
        <el-input v-model="queryParams.deptName" v-if="false"/>
      </el-form-item>
      <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">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['material:material:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['material:material:edit']"
        >修改
        </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-hasPermi="['material:material:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['material:material:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--<el-table-column label="主键ID" align="center" prop="id"/>-->
      <el-table-column label="物资名称" align="center" prop="materialName"/>
      <!--<el-table-column label="归属仓库" align="center" prop="warehouseId"/>-->
      <el-table-column label="归属仓库" align="center" prop="warehouseName"/>
      <el-table-column label="品牌型号参数" align="center" prop="materialParams"/>
      <el-table-column label="生产日期" align="center" prop="prrodTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.prrodTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主要功能用途" align="center" prop="materialPurpose"/>
      <el-table-column label="物资类别" align="center" prop="materialType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.emergency_material_type" :value="scope.row.materialType"/>
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="materialNumber"/>
      <el-table-column label="存放地点" align="center" prop="depositAddress"/>
      <el-table-column label="联络人" align="center" prop="charger"/>
      <el-table-column label="联系电话" align="center" prop="connPhone"/>
      <el-table-column label="所属部门" align="center" prop="deptName"/>
      <el-table-column label="创建人" align="center" prop="createName"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="修改人" align="center" prop="updateName"/>
      <el-table-column label="修改时间" align="center" prop="updateTime"/>
      <!--<el-table-column label="技术状况" align="center" prop="techStatus"/>
      <el-table-column label="备注" align="center" prop="materialRemark"/>
      <el-table-column label="${comment}" align="center" prop="owndeptid"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['material:material:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['material:material: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="1000px" class="form-style">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="物资名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="请输入物资名称" maxlength="50"/>
            </el-form-item>
            <el-form-item label="品牌型号主要参数" prop="materialParams">
              <el-input v-model="form.materialParams" placeholder="请输入品牌型号主要参数" maxlength="50"/>
            </el-form-item>
            <el-form-item label="主要功能和用途" prop="materialPurpose">
              <el-input v-model="form.materialPurpose" placeholder="请输入主要功能和用途" maxlength="50"/>
            </el-form-item>
            <el-form-item label="数量" prop="materialNumber">
<!--              <el-input v-model="form.materialNumber" placeholder="请输入数量"  maxlength="11"/>-->
              <el-input-number v-model="form.materialNumber" :min = "0" :max = "100000000" controls-position="right" :step="1" style="width: 100%"/>
            </el-form-item>
            <el-form-item label="联络人" prop="charger">
              <el-input v-model="form.charger" placeholder="请输入联络人" maxlength="50"/>
            </el-form-item>
            <el-form-item label="所属部门" prop="deptId">
              <treeselect v-model="form.deptId"
                          :noResultsText="'空'" :noOptionsText="'空'"
                          :options="deptOptions" multiple:false :show-count="true"
                          placeholder="请选择部门" @select="hx"/>
            </el-form-item>
<!--            <el-form-item label="所属部门" prop="deptId">-->
<!--              <Deptselector :setValue=setDataDeptId :dataDeptId="form.deptId" maxlength="50"/>-->
<!--            </el-form-item>-->
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属仓库" prop="warehouseId">
              <el-select v-model="form.warehouseId" placeholder="请选择归属仓库">
                <el-option v-for="item in warehouseIdsOptions"
                           :key="item.id" :label="item.name" :value="item.id"  @click.native = handleClick(item.name)></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="生产日期" prop="prrodTime">
              <el-date-picker v-model="form.prrodTime" clearable
                              type="date" value-format="yyyy-MM-dd" placeholder="请选择生产日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="物资类别" prop="materialType">
              <el-select v-model="form.materialType" placeholder="请选择物资类别" clearable filterable>
                <el-option v-for="dict in dict.type.emergency_material_type"
                           :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="存放地点" prop="depositAddress">
              <el-input v-model="form.depositAddress" placeholder="请输入存放地点" maxlength="50"/>
            </el-form-item>
            <el-form-item label="联系电话" prop="connPhone">
              <el-input v-model="form.connPhone" placeholder="请输入联系电话" maxlength="13"/>
            </el-form-item>
          </el-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 {
    addMaterial,
    delMaterial,
    getMaterial,
    listMaterial,
    updateMaterial
  } from "@/api/data/digitalemergency/material";
  import { checkNonnegativeInteger,validPhoneMobile} from "@/api/rules/rules";
  import {warehouseIds} from "@/api/data/digitalemergency/warehouse";
  import {format_date} from "@/views/data/common/dateExport";
  import {treeselect} from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "Material",
    components: {Treeselect},
    dicts: ['emergency_material_type'],
    data() {
      return {
        deptOptions: undefined,
        dataDeptId: null,
        deptId: null,
        deptName: null,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 应急物资表格数据
        materialList: [],
        //归属仓库选择
        warehouseIdsOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          dataStatus: null,
          materialName: null,
          materialParams: null,
          materialNumber: null,
          materialPurpose: null,
          techStatus: null,
          prrodTime: null,
          depositAddress: null,
          charger: null,
          connPhone: null,
          materialRemark: null,
          warehouseId: null,
          warehouseName: null,
          materialType: null,
          owndeptid: null,
          dataDeptId: null,
          createName:null,
          createTime:null,
          updateName:null,
          updateTime:null,
          deptId: null,
          deptName: 0,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          materialName: [
            {required: true, message: '物资名称不能为空', trigger: 'change'}
          ],
          charger: [
            {required: true, message: '联系人不能为空', trigger: 'change'}
          ],
          warehouseId: [
            {required: true, message: '归属仓库不能为空', trigger: 'change'}
          ],
          materialType: [
            {required: true, message: '物资类别不能为空', trigger: 'change'}
          ],
          materialNumber: [
            {required: true, message: '数量不能为空', trigger: 'change'},
            {validator: checkNonnegativeInteger,trigger: 'blur'}
          ],
          deptId: [
            {required: true, message: '所属部门不能为空', trigger: 'change'}
          ],
          connPhone:[
            {required: true, message: '联系电话不能为空', trigger: 'change'},
            {validator:validPhoneMobile ,trigger: 'blur'}
          ],
          prrodTime:[
            {required: true, message: '生产日期不能为空', trigger: 'change'}
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getWarehouseIds();
      this.getTreeselect();
    },
    methods: {
      // setDataDeptId(e) {
      //   this.form.deptId = e.deptId
      //   this.form.deptName = e.deptName
      // },
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data
        })
      },
      hx(node) {
        this.form.dataDeptId = node.id
        this.form.deptId = node.id
        this.form.deptName = node.label
        this.$refs.form.validateField('deptId');
      },
      /** 查询应急物资列表 */
      getList() {
        this.loading = true;
        listMaterial(this.queryParams).then(response => {
          this.materialList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          createBy: null,
          createName:null,
          createTime: null,
          updateBy: null,
          updateName: null,
          updateTime: null,
          deleteBy: null,
          deleteTime: null,
          dataStatus: "0",
          materialName: null,
          materialParams: null,
          materialNumber: null,
          materialPurpose: null,
          techStatus: "0",
          prrodTime: null,
          depositAddress: null,
          charger: null,
          connPhone: null,
          materialRemark: null,
          warehouseId: null,
          warehouseName: null,
          materialType: null,
          owndeptid: null,
          deptId: null,
          deptName: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.deptName = 0;
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 点击按钮修改是否只查询本级部门用户
      changeQueryType() {
        this.queryParams.deptName = this.queryParams.deptName == 0 ? 1 : 0;
      },
      // 多选框选中数据
      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
        getMaterial(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) {
              updateMaterial(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addMaterial(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除选中的数据项？').then(function () {
          return delMaterial(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('sooka-sponest-center-data/emergency/material/export', {
          ...this.queryParams
        }, `应急物资_${format_date(new Date())}.xlsx`)
      },
      getWarehouseIds() {
        warehouseIds().then(response => {
          this.warehouseIdsOptions = response.data;
        });
      },
      handleClick(x){
        this.form.warehouseName = x;
      }
    }
  };
</script>
